__init__.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """curses
  2. The main package for curses support for Python. Normally used by importing
  3. the package, and perhaps a particular module inside it.
  4. import curses
  5. from curses import textpad
  6. curses.initscr()
  7. ...
  8. """
  9. from _curses import *
  10. import os as _os
  11. import sys as _sys
  12. # Some constants, most notably the ACS_* ones, are only added to the C
  13. # _curses module's dictionary after initscr() is called. (Some
  14. # versions of SGI's curses don't define values for those constants
  15. # until initscr() has been called.) This wrapper function calls the
  16. # underlying C initscr(), and then copies the constants from the
  17. # _curses module to the curses package's dictionary. Don't do 'from
  18. # curses import *' if you'll be needing the ACS_* constants.
  19. def initscr():
  20. import _curses, curses
  21. # we call setupterm() here because it raises an error
  22. # instead of calling exit() in error cases.
  23. setupterm(term=_os.environ.get("TERM", "unknown"),
  24. fd=_sys.__stdout__.fileno())
  25. stdscr = _curses.initscr()
  26. for key, value in _curses.__dict__.items():
  27. if key[0:4] == 'ACS_' or key in ('LINES', 'COLS'):
  28. setattr(curses, key, value)
  29. return stdscr
  30. # This is a similar wrapper for start_color(), which adds the COLORS and
  31. # COLOR_PAIRS variables which are only available after start_color() is
  32. # called.
  33. def start_color():
  34. import _curses, curses
  35. retval = _curses.start_color()
  36. if hasattr(_curses, 'COLORS'):
  37. curses.COLORS = _curses.COLORS
  38. if hasattr(_curses, 'COLOR_PAIRS'):
  39. curses.COLOR_PAIRS = _curses.COLOR_PAIRS
  40. return retval
  41. # Import Python has_key() implementation if _curses doesn't contain has_key()
  42. try:
  43. has_key
  44. except NameError:
  45. from .has_key import has_key
  46. # Wrapper for the entire curses-based application. Runs a function which
  47. # should be the rest of your curses-based application. If the application
  48. # raises an exception, wrapper() will restore the terminal to a sane state so
  49. # you can read the resulting traceback.
  50. def wrapper(func, /, *args, **kwds):
  51. """Wrapper function that initializes curses and calls another function,
  52. restoring normal keyboard/screen behavior on error.
  53. The callable object 'func' is then passed the main window 'stdscr'
  54. as its first argument, followed by any other arguments passed to
  55. wrapper().
  56. """
  57. try:
  58. # Initialize curses
  59. stdscr = initscr()
  60. # Turn off echoing of keys, and enter cbreak mode,
  61. # where no buffering is performed on keyboard input
  62. noecho()
  63. cbreak()
  64. # In keypad mode, escape sequences for special keys
  65. # (like the cursor keys) will be interpreted and
  66. # a special value like curses.KEY_LEFT will be returned
  67. stdscr.keypad(1)
  68. # Start color, too. Harmless if the terminal doesn't have
  69. # color; user can test with has_color() later on. The try/catch
  70. # works around a minor bit of over-conscientiousness in the curses
  71. # module -- the error return from C start_color() is ignorable.
  72. try:
  73. start_color()
  74. except:
  75. pass
  76. return func(stdscr, *args, **kwds)
  77. finally:
  78. # Set everything back to normal
  79. if 'stdscr' in locals():
  80. stdscr.keypad(0)
  81. echo()
  82. nocbreak()
  83. endwin()