On 20Dec2020 15:48, Christopher Barker email@example.com wrote:
On Sun, Dec 20, 2020 at 1:23 PM Cameron Simpson firstname.lastname@example.org wrote:
My anger at programmes which gratuitously clear the screen is large.
There are a LOT of bad things one can do with Python, I don't think we need to make something difficult simply because it can be abused.
One problem is: what does it mean? On a terminal, easy. But in a GUI?
Clear the screen (possibly forbidden)? A window? The "main" window? Etc.
It would be meaningless outside of a terminal -- call it `clear_term` if you want.
Anyway, I think it should be in curses (or be loaded via curses on demand),
That would be great, though I just looked at the 3.9 docs and saw: "The Windows version of Python doesn’t include the curses module."
So we're pretty much back to square one.
No, for Windows we shell out to its command line unless a Windows person knows what to do. We could hardwire the ANSI clear screen sequence as a fallback when there's no curses module, that's easy with a try/import.
I think the idea here is that we want a simple way, out of the box that people can use to clear the terminal, that will work on most commonly configured systems out of the box, and can be overridden (monkey patched) to work in IDEs (e.g. Idle), custom terminal emulators (e.g. iPython Qtconsole), etc. We don't want to require a full curses implementation.
Ok. I still think on UNIX we want curses as the preferred choice since to accomodates all terminal types. But a fallback assuming ANSI seems sensible to me, unless there's a Windows counter example.
cls_bs = None
def clear_screen(): if cls_bs is None: try: import curses except ImportError: cls_bs=curses.tigetstr('clear') print(cls_bs.encode(), end='', flush=True)
I just tried that on my mac and tigetstr('cl') simply returns None with no effect.
Yeah. 'clear' works though.
The tutorial seems to indicate there is a clear method available, so I did try that, and it does nothing on my Mac with the default terminal app:
def clear_screen(): from curses import wrapper
def clr(stdscr): # Clear screen stdscr.clear() wrapper(clr) clear_screen()
That requires a full curses init, "full screen curses mode programme" etc. We just want the terminal escape sequence. setupterm() inits the terminal db access, then off we go. As above.
is it so bad to use a subprocess?
Yes. It is _really slow_,
could it possible be slow enough to matter? not the kind of thing that's in a tight loop.
In your imagined use case. Guarenteed one day this will be in a tight loop. In a simulation or something.
depends on external reaources which might not
oh, like the curses lib ;-)
Hahaha. Batteries supplied.
Cheers, Cameron Simpson email@example.com