Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
__init__.py102 linesDownload Raw Back to curses
1"""curses2 3The main package for curses support for Python.  Normally used by importing4the package, and perhaps a particular module inside it.5 6   import curses7   from curses import textpad8   curses.initscr()9   ...10 11"""12 13from _curses import *14import os as _os15import sys as _sys16 17# Some constants, most notably the ACS_* ones, are only added to the C18# _curses module's dictionary after initscr() is called.  (Some19# versions of SGI's curses don't define values for those constants20# until initscr() has been called.)  This wrapper function calls the21# underlying C initscr(), and then copies the constants from the22# _curses module to the curses package's dictionary.  Don't do 'from23# curses import *' if you'll be needing the ACS_* constants.24 25def initscr():26    import _curses, curses27    # we call setupterm() here because it raises an error28    # instead of calling exit() in error cases.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 stdscr37 38# This is a similar wrapper for start_color(), which adds the COLORS and39# COLOR_PAIRS variables which are only available after start_color() is40# called.41 42def start_color():43    import _curses, curses44    retval = _curses.start_color()45    if hasattr(_curses, 'COLORS'):46        curses.COLORS = _curses.COLORS47    if hasattr(_curses, 'COLOR_PAIRS'):48        curses.COLOR_PAIRS = _curses.COLOR_PAIRS49    return retval50 51# Import Python has_key() implementation if _curses doesn't contain has_key()52 53try:54    has_key55except NameError:56    from .has_key import has_key  # noqa: F40157 58# Wrapper for the entire curses-based application.  Runs a function which59# should be the rest of your curses-based application.  If the application60# raises an exception, wrapper() will restore the terminal to a sane state so61# you can read the resulting traceback.62 63def 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 to68    wrapper().69    """70 71    try:72        # Initialize curses73        stdscr = initscr()74 75        # Turn off echoing of keys, and enter cbreak mode,76        # where no buffering is performed on keyboard input77        noecho()78        cbreak()79 80        # In keypad mode, escape sequences for special keys81        # (like the cursor keys) will be interpreted and82        # a special value like curses.KEY_LEFT will be returned83        stdscr.keypad(1)84 85        # Start color, too.  Harmless if the terminal doesn't have86        # color; user can test with has_color() later on.  The try/catch87        # works around a minor bit of over-conscientiousness in the curses88        # module -- the error return from C start_color() is ignorable.89        try:90            start_color()91        except:92            pass93 94        return func(stdscr, *args, **kwds)95    finally:96        # Set everything back to normal97        if 'stdscr' in locals():98            stdscr.keypad(0)99            echo()100            nocbreak()101            endwin()102 
codekingpro/portable-devtools · Team Ai