blob: fd8804d9f2c18be1fd34aacfc2e463285a85b7bd [file] [log] [blame]
Andrew M. Kuchlingd0939fa2000-06-10 23:06:53 +00001"""curses.wrapper
2
3Contains one function, wrapper(), which runs another function which
4should be the rest of your curses-based application. If the
5application raises an exception, wrapper() will restore the terminal
6to a sane state so you can read the resulting traceback.
7
8"""
9
10import sys, curses
11
12def wrapper(func, *rest):
13 """Wrapper function that initializes curses and calls another function,
14 restoring normal keyboard/screen behavior on error.
15 The callable object 'func' is then passed the main window 'stdscr'
16 as its first argument, followed by any other arguments passed to
17 wrapper().
18 """
19
Andrew M. Kuchlingd0939fa2000-06-10 23:06:53 +000020 try:
21 # Initialize curses
22 stdscr=curses.initscr()
23 # Turn off echoing of keys, and enter cbreak mode,
24 # where no buffering is performed on keyboard input
25 curses.noecho() ; curses.cbreak()
26
27 # In keypad mode, escape sequences for special keys
28 # (like the cursor keys) will be interpreted and
29 # a special value like curses.KEY_LEFT will be returned
30 stdscr.keypad(1)
31
Andrew M. Kuchling5185a082000-06-10 23:39:05 +000032 return apply(func, (stdscr,) + rest)
33
34 finally:
35 # Restore the terminal to a sane state on the way out.
Andrew M. Kuchlingd0939fa2000-06-10 23:06:53 +000036 stdscr.keypad(0)
37 curses.echo() ; curses.nocbreak()
38 curses.endwin()
Andrew M. Kuchlingd0939fa2000-06-10 23:06:53 +000039