"""curses12The main package for curses support for Python. Normally used by importing3the package, and perhaps a particular module inside it.45import curses6from curses import textpad7curses.initscr()8...910"""1112from _curses import *13import os as _os14import sys as _sys1516# Some constants, most notably the ACS_* ones, are only added to the C17# _curses module's dictionary after initscr() is called. (Some18# versions of SGI's curses don't define values for those constants19# until initscr() has been called.) This wrapper function calls the20# underlying C initscr(), and then copies the constants from the21# _curses module to the curses package's dictionary. Don't do 'from22# curses import *' if you'll be needing the ACS_* constants.2324def initscr():25import _curses, curses26# we call setupterm() here because it raises an error27# instead of calling exit() in error cases.28setupterm(term=_os.environ.get("TERM", "unknown"),29fd=_sys.__stdout__.fileno())30stdscr = _curses.initscr()31for key, value in _curses.__dict__.items():32if key[0:4] == 'ACS_' or key in ('LINES', 'COLS'):33setattr(curses, key, value)3435return stdscr3637# This is a similar wrapper for start_color(), which adds the COLORS and38# COLOR_PAIRS variables which are only available after start_color() is39# called.4041def start_color():42import _curses, curses43retval = _curses.start_color()44if hasattr(_curses, 'COLORS'):45curses.COLORS = _curses.COLORS46if hasattr(_curses, 'COLOR_PAIRS'):47curses.COLOR_PAIRS = _curses.COLOR_PAIRS48return retval4950# Import Python has_key() implementation if _curses doesn't contain has_key()5152try:53has_key54except NameError:55from .has_key import has_key5657# Wrapper for the entire curses-based application. Runs a function which58# should be the rest of your curses-based application. If the application59# raises an exception, wrapper() will restore the terminal to a sane state so60# you can read the resulting traceback.6162def wrapper(func, /, *args, **kwds):63"""Wrapper function that initializes curses and calls another function,64restoring normal keyboard/screen behavior on error.65The callable object 'func' is then passed the main window 'stdscr'66as its first argument, followed by any other arguments passed to67wrapper().68"""6970try:71# Initialize curses72stdscr = initscr()7374# Turn off echoing of keys, and enter cbreak mode,75# where no buffering is performed on keyboard input76noecho()77cbreak()7879# In keypad mode, escape sequences for special keys80# (like the cursor keys) will be interpreted and81# a special value like curses.KEY_LEFT will be returned82stdscr.keypad(1)8384# Start color, too. Harmless if the terminal doesn't have85# color; user can test with has_color() later on. The try/catch86# works around a minor bit of over-conscientiousness in the curses87# module -- the error return from C start_color() is ignorable.88try:89start_color()90except:91pass9293return func(stdscr, *args, **kwds)94finally:95# Set everything back to normal96if 'stdscr' in locals():97stdscr.keypad(0)98echo()99nocbreak()100endwin()101102103