def using_curses(func):
def inner(*largs, **kwargs):
"""
Calls decorated function with initial curses screen inserted as
first argument.
Content mostly taken from curses.wrapper, modified to function as a
decorator, and to introduce cursor removal
"""
try:
# Initialize curses
stdscr = curses.initscr()
# Turn off echoing of keys, and enter cbreak mode,
# where no buffering is performed on keyboard input
curses.noecho()
curses.cbreak()
# In keypad mode, escape sequences for special keys
# (like the cursor keys) will be interpreted and
# a special value like curses.KEY_LEFT will be returned
stdscr.keypad(1)
# Start color, too. Harmless if the terminal doesn't have
# color; user can test with has_color() later on. The try/catch
# works around a minor bit of over-conscientiousness in the curses
# module -- the error return from C start_color() is ignorable.
try:
curses.start_color()
curses.use_default_colors()
except:
pass
# Set colour pallet
curses.init_pair(CPI_GOOD, curses.COLOR_GREEN, -1)
curses.init_pair(CPI_ERROR, curses.COLOR_RED, -1)
curses.init_pair(CPI_WARNING, curses.COLOR_YELLOW, -1)
# Remove blinking cursor
curses.curs_set(0)
func(stdscr, *largs, **kwargs)
finally:
# Set everything back to normal
if 'stdscr' in locals():
curses.curs_set(1)
stdscr.keypad(0)
curses.echo() # revert curses.noecho()
curses.nocbreak() # revert curses.cbreak()
curses.endwin()
return inner
# ================== Status (header) Window ==================
评论列表
文章目录