On Sun, Dec 20, 2020 at 4:12 PM Cameron Simpson <
cs@cskk.id.au> 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
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