| 1 |
"""curses |
| 2 |
|
| 3 |
The main package for curses support for Python. Normally used by importing |
| 4 |
the package, and perhaps a particular module inside it. |
| 5 |
|
| 6 |
import curses |
| 7 |
from curses import textpad |
| 8 |
curses.initscr() |
| 9 |
... |
| 10 |
|
| 11 |
""" |
| 12 |
|
| 13 |
from _curses import * |
| 14 |
import os as _os |
| 15 |
import sys as _sys |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
def initscr(): |
| 26 |
import _curses, curses |
| 27 |
|
| 28 |
|
| 29 |
setupterm(term=_os.environ.get("TERM", "unknown"), |
| 30 |
fd=_sys.__stdout__.fileno()) |
| 31 |
stdscr = _curses.initscr() |
| 32 |
for key, value in _curses.__dict__.items(): |
| 33 |
if key[0:4] == 'ACS_' or key in ('LINES', 'COLS'): |
| 34 |
setattr(curses, key, value) |
| 35 |
|
| 36 |
return stdscr |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
def start_color(): |
| 43 |
import _curses, curses |
| 44 |
retval = _curses.start_color() |
| 45 |
if hasattr(_curses, 'COLORS'): |
| 46 |
curses.COLORS = _curses.COLORS |
| 47 |
if hasattr(_curses, 'COLOR_PAIRS'): |
| 48 |
curses.COLOR_PAIRS = _curses.COLOR_PAIRS |
| 49 |
return retval |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
try: |
| 54 |
has_key |
| 55 |
except NameError: |
| 56 |
from .has_key import has_key |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
def wrapper(func, /, *args, **kwds): |
| 64 |
"""Wrapper function that initializes curses and calls another function, |
| 65 |
restoring normal keyboard/screen behavior on error. |
| 66 |
The callable object 'func' is then passed the main window 'stdscr' |
| 67 |
as its first argument, followed by any other arguments passed to |
| 68 |
wrapper(). |
| 69 |
""" |
| 70 |
|
| 71 |
try: |
| 72 |
|
| 73 |
stdscr = initscr() |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
noecho() |
| 78 |
cbreak() |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
stdscr.keypad(1) |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
try: |
| 90 |
start_color() |
| 91 |
except: |
| 92 |
pass |
| 93 |
|
| 94 |
return func(stdscr, *args, **kwds) |
| 95 |
finally: |
| 96 |
|
| 97 |
if 'stdscr' in locals(): |
| 98 |
stdscr.keypad(0) |
| 99 |
echo() |
| 100 |
nocbreak() |
| 101 |
endwin() |