1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
# Curses functions
import signal
import curses
import atexit
_Stdscr = None
def _finishCurses():
curses.nocbreak()
curses.echo()
curses.endwin()
def _initCurses():
global _Stdscr
atexit.register(_finishCurses)
_Stdscr = curses.initscr()
def enterCurses():
if not _Stdscr:
_initCurses()
curses.cbreak()
curses.noecho()
_Stdscr.nodelay(1)
signal.signal(signal.SIGWINCH, signal.SIG_IGN)
def leaveCurses():
curses.reset_shell_mode()
signal.signal(signal.SIGWINCH, signal.SIG_DFL)
# Check non-blockingly for a key press and returns it, or return None if no
# key is found. enter/leaveCurses must surround the getc() call.
def getCh():
ch = _Stdscr.getch()
if ch < 0:
return None
return chr(ch)
def clearScreen():
if not _Stdscr:
_initCurses()
_Stdscr.clear()
def printLines(lines):
y = 0
for line in lines:
try:
_Stdscr.insnstr(y, 0, line, len(line))
except:
pass
y += 1
try:
_Stdscr.insnstr(y, 0, "", 0)
except:
pass
|