On Sat, Oct 17, 2020 at 05:44:12AM +1100, Chris Angelico wrote:
> On Sat, Oct 17, 2020 at 5:40 AM Jonathan Crall <erote...@gmail.com> wrote:
> >
> > I just want to point out that I can think of a valid use case for `clf`. 
> > I'm not sure if it was mentioned.
> >
> > In the case where you have a script that produces a lot of output, a common 
> > task might be scrolling to the beginning to check an output. If your screen 
> > was not fresh, and you had a lot of previous output (say from running the 
> > script multiple times), then it is hard to find the beginning unless you 
> > have previously cleared the screen.
> >
> 
> This is exactly why I think this should NOT be made too easy. That's a
> perfect example of an attractive nuisance: instead of creating a
> divider (say, a half a dozen blank lines, or a row of hyphens with a
> blank or two each side), you're throwing away all information from the
> previous output, making it impossible to compare the two.

Why are we assuming that our "clear screen" command flushes the 
scrollback buffer as well as clearing the screen?

I do not want any cls command to flush the scrollback buffer by default 
(although I wouldn't object to an option to do so). I would want it to 
be more like ^L although that's probably dependent on your terminal.

In other words, I want the default to be like the command `clear -x` 
(bash), not `clear`. I also like the idea given here:

https://askubuntu.com/a/997893

of printing a divider first so that when scrolling back the clear screen 
is obvious.

So here's my first attempt (untested!) of a "cls" pseudo-builtin 
command, similar to the help and quit builtins added by site.py. It 
assumes that os.clear exists and takes an appropriate optional flag.


```
import builtins
import os

class ScreenClearer:
    if os.name == 'posix':
        def __repr__(self):
            return 'Use cls() or Ctrl-L to clear the screen.'
    else:
         def __repr__(self):
            return 'Use cls() to clear the screen.'
    def __call__(self, flush_scrollback=False):
        print('\n', '-'*os.get_terminal_size()[0], '\n', sep='')
        os.clear(flush_scrollback)

builtins.cls = ScreenClearer()
```

Of course I'm glossing over the hard part -- actually clearing the 
screen and/or scrollback.

I think that doing both is easy:

- on Windows: `os.system('cls')`

- on Posix: `os.system('clear')`

To *not* clear the scrollback is easy on modern Linuxes:

- `os.system('clear -x')`

but I don't know how you can recognise in advance whether it is 
supported or not. On terminals that support it, this should work:

- `print('\33[H\33[2J')`

but I have no idea how to avoid clearing the scrollback buffer on 
Windows, or other posix systems with unusual terminals.

Of course IDLE will require something different.

The ultimate fallback for clearing the visible screen but not the 
scrollback is just `print('\n'*os.get_terminal_size()[1])`.

So we have plenty of solutions for this, but none of them are platform 
independent, and picking the right option for your specific terminal may 
be tricky for the more unusual terminals.


I don't have access to a Windows machine to experiment, but I found 
these issues which may be relevant:

https://github.com/microsoft/terminal/issues/1305

https://github.com/microsoft/terminal/issues/1882



-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MF5NJI3MWYKYCCRII6WNP7EOLJQHEE46/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to