Andrew M. Kuchling | d0939fa | 2000-06-10 23:06:53 +0000 | [diff] [blame] | 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 sys, curses |
| 11 | |
| 12 | def 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. Kuchling | d0939fa | 2000-06-10 23:06:53 +0000 | [diff] [blame] | 20 | 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. Kuchling | 5185a08 | 2000-06-10 23:39:05 +0000 | [diff] [blame] | 32 | return apply(func, (stdscr,) + rest) |
| 33 | |
| 34 | finally: |
| 35 | # Restore the terminal to a sane state on the way out. |
Andrew M. Kuchling | d0939fa | 2000-06-10 23:06:53 +0000 | [diff] [blame] | 36 | stdscr.keypad(0) |
| 37 | curses.echo() ; curses.nocbreak() |
| 38 | curses.endwin() |
Andrew M. Kuchling | d0939fa | 2000-06-10 23:06:53 +0000 | [diff] [blame] | 39 | |