0001 """curses.wrapper 0002 0003 Contains one function, wrapper(), which runs another function which 0004 should be the rest of your curses-based application. If the 0005 application raises an exception, wrapper() will restore the terminal 0006 to a sane state so you can read the resulting traceback. 0007 0008 """ 0009 0010 import sys, curses 0011 0012 def wrapper(func, *args, **kwds): 0013 """Wrapper function that initializes curses and calls another function, 0014 restoring normal keyboard/screen behavior on error. 0015 The callable object 'func' is then passed the main window 'stdscr' 0016 as its first argument, followed by any other arguments passed to 0017 wrapper(). 0018 """ 0019 0020 res = None 0021 try: 0022 # Initialize curses 0023 stdscr=curses.initscr() 0024 0025 # Turn off echoing of keys, and enter cbreak mode, 0026 # where no buffering is performed on keyboard input 0027 curses.noecho() 0028 curses.cbreak() 0029 0030 # In keypad mode, escape sequences for special keys 0031 # (like the cursor keys) will be interpreted and 0032 # a special value like curses.KEY_LEFT will be returned 0033 stdscr.keypad(1) 0034 0035 # Start color, too. Harmless if the terminal doesn't have 0036 # color; user can test with has_color() later on. The try/catch 0037 # works around a minor bit of over-conscientiousness in the curses 0038 # module -- the error return from C start_color() is ignorable. 0039 try: 0040 curses.start_color() 0041 except: 0042 pass 0043 0044 return func(stdscr, *args, **kwds) 0045 finally: 0046 # Set everything back to normal 0047 stdscr.keypad(0) 0048 curses.echo() 0049 curses.nocbreak() 0050 curses.endwin() 0051
Generated by PyXR 0.9.4