|
1 """curses.wrapper |
|
2 |
|
3 Contains one function, wrapper(), which runs another function which |
|
4 should be the rest of your curses-based application. If the |
|
5 application raises an exception, wrapper() will restore the terminal |
|
6 to a sane state so you can read the resulting traceback. |
|
7 |
|
8 """ |
|
9 |
|
10 import curses |
|
11 |
|
12 def wrapper(func, *args, **kwds): |
|
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 |
|
20 res = None |
|
21 try: |
|
22 # Initialize curses |
|
23 stdscr=curses.initscr() |
|
24 |
|
25 # Turn off echoing of keys, and enter cbreak mode, |
|
26 # where no buffering is performed on keyboard input |
|
27 curses.noecho() |
|
28 curses.cbreak() |
|
29 |
|
30 # In keypad mode, escape sequences for special keys |
|
31 # (like the cursor keys) will be interpreted and |
|
32 # a special value like curses.KEY_LEFT will be returned |
|
33 stdscr.keypad(1) |
|
34 |
|
35 # Start color, too. Harmless if the terminal doesn't have |
|
36 # color; user can test with has_color() later on. The try/catch |
|
37 # works around a minor bit of over-conscientiousness in the curses |
|
38 # module -- the error return from C start_color() is ignorable. |
|
39 try: |
|
40 curses.start_color() |
|
41 except: |
|
42 pass |
|
43 |
|
44 return func(stdscr, *args, **kwds) |
|
45 finally: |
|
46 # Set everything back to normal |
|
47 stdscr.keypad(0) |
|
48 curses.echo() |
|
49 curses.nocbreak() |
|
50 curses.endwin() |