On Sun, Dec 20, 2020 at 4:12 PM Cameron Simpson <[email protected]> wrote:

> Untested:
>     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)
>

darn that untested code -- a lot wrong here :-(

But unpacking it, I've come up with this, which seems to work.

import os
import sys

# Define clear_screen function:
try:
    import curses
    curses.setupterm()
    CLS_BS = curses.tigetstr('clear')

    def clear_screen():
        sys.stdout.buffer.write(CLS_BS)
except:  # if anything went wrong, fall back to os.system call
    if os.name == 'posix':
        def clear_screen():
            os.system('clear')
    elif sys.platform == 'win32':
        # ideally something lower level on Windows
        def clear_screen():
            os.system('cls')
    else:
        def clear_screen():
            pass

On Sun, Dec 20, 2020 at 8:06 PM Eryk Sun <[email protected]> wrote:

> A C or ctypes implementation is required in Windows.

<snip>

so that could be plugged in to the above, with (or not) a fallback to the
system calls.

Anyway, this has been interesting enough to distract me from other things I
should be doing, but I'm moving on now ...

-CHB


-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/ANCVBHBM5DGJ42RKODYZFAAZEE5FAKQ3/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to