[Tutor] Clearing the screen from the terminal

Cameron Simpson cs at cskk.id.au
Sat Oct 24 18:02:47 EDT 2020


On 18Oct2020 20:18, Alan Gauld <alan.gauld at yahoo.co.uk> wrote:
[...]
>The "correct" way of doing screen control in a terminal
>(as used by vim and the top commands etc) is to use the
>curses module. Curses provides screen and cursor control
>regardless of terminal type. But that introduces a
>whole extra layer of complexity (along with power).
>For example regular print() and input() fuctions won't
>work, you need to use curses own equivalents.
>
>A minimal curses program to clear the screen is:
>
>import curses
>
>def main(win):   # win refers to the terminal screen.
>   win.clear()
>   # your program here....
>
>curses.wrapper(main)

And if the OP wants a lower level approach, still in Python:

    import curses

   curses.setupterm()
   print(curses.tigetstr('clear_screen'), end='')
   # your program here....

which sidesteps the "initialise curses and prepare a full screen 
environment". All this one does is get curses to load the terminfo db 
and then queries it for the "clear_screen" sequence.

I'll also take a moment to iterate my dislike of programmes which clear 
the screen. One has but to boot a PC and see the many screen redraws 
obliterating the previous "what happened" display to understand my ire.

I can even be pushed to pining for the days of the paper terminal...

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list