curses the main package for curses support for python normally used by importing the package and perhaps a particular module inside it import curses from curses import textpad curses initscr some constants most notably the acs ones are only added to the c curses module s dictionary after initscr is called some versions of sgi s curses don t define values for those constants until initscr has been called this wrapper function calls the underlying c initscr and then copies the constants from the curses module to the curses package s dictionary don t do from curses import if you ll be needing the acs constants we call setupterm here because it raises an error instead of calling exit in error cases this is a similar wrapper for startcolor which adds the colors and colorpairs variables which are only available after startcolor is called import python haskey implementation if curses doesn t contain haskey wrapper for the entire cursesbased application runs a function which should be the rest of your cursesbased application if the application raises an exception wrapper will restore the terminal to a sane state so you can read the resulting traceback wrapper function that initializes curses and calls another function restoring normal keyboardscreen behavior on error the callable object func is then passed the main window stdscr as its first argument followed by any other arguments passed to wrapper initialize curses turn off echoing of keys and enter cbreak mode where no buffering is performed on keyboard input in keypad mode escape sequences for special keys like the cursor keys will be interpreted and a special value like curses keyleft will be returned start color too harmless if the terminal doesn t have color user can test with hascolor later on the trycatch works around a minor bit of overconscientiousness in the curses module the error return from c startcolor is ignorable set everything back to normal some constants most notably the acs_ ones are only added to the c _curses module s dictionary after initscr is called some versions of sgi s curses don t define values for those constants until initscr has been called this wrapper function calls the underlying c initscr and then copies the constants from the _curses module to the curses package s dictionary don t do from curses import if you ll be needing the acs_ constants we call setupterm here because it raises an error instead of calling exit in error cases this is a similar wrapper for start_color which adds the colors and color_pairs variables which are only available after start_color is called import python has_key implementation if _curses doesn t contain has_key wrapper for the entire curses based application runs a function which should be the rest of your curses based application if the application raises an exception wrapper will restore the terminal to a sane state so you can read the resulting traceback wrapper function that initializes curses and calls another function restoring normal keyboard screen behavior on error the callable object func is then passed the main window stdscr as its first argument followed by any other arguments passed to wrapper initialize curses turn off echoing of keys and enter cbreak mode where no buffering is performed on keyboard input in keypad mode escape sequences for special keys like the cursor keys will be interpreted and a special value like curses key_left will be returned start color too harmless if the terminal doesn t have color user can test with has_color later on the try catch works around a minor bit of over conscientiousness in the curses module the error return from c start_color is ignorable set everything back to normal
from _curses import *
import os as _os
import sys as _sys









def initscr():
    import _curses, curses
    
    
    setupterm(term=_os.environ.get("TERM", "unknown"),
              fd=_sys.__stdout__.fileno())
    stdscr = _curses.initscr()
    for key, value in _curses.__dict__.items():
        if key[0:4] == 'ACS_' or key in ('LINES', 'COLS'):
            setattr(curses, key, value)

    return stdscr





def start_color():
    import _curses, curses
    retval = _curses.start_color()
    if hasattr(_curses, 'COLORS'):
        curses.COLORS = _curses.COLORS
    if hasattr(_curses, 'COLOR_PAIRS'):
        curses.COLOR_PAIRS = _curses.COLOR_PAIRS
    return retval



try:
    has_key
except NameError:
    from .has_key import has_key






def wrapper(func, /, *args, **kwds):
    

    try:
        
        stdscr = initscr()

        
        
        noecho()
        cbreak()

        
        
        
        stdscr.keypad(1)

        
        
        
        
        try:
            start_color()
        except:
            pass

        return func(stdscr, *args, **kwds)
    finally:
        
        if 'stdscr' in locals():
            stdscr.keypad(0)
            echo()
            nocbreak()
            endwin()