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