Andrew M. Kuchling | d0939fa | 2000-06-10 23:06:53 +0000 | [diff] [blame] | 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.initwin() |
| 9 | ... |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 10 | |
Andrew M. Kuchling | d0939fa | 2000-06-10 23:06:53 +0000 | [diff] [blame] | 11 | """ |
| 12 | |
| 13 | __revision__ = "$Id$" |
| 14 | |
| 15 | from _curses import * |
| 16 | from curses.wrapper import wrapper |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 17 | import os as _os |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 18 | import sys as _sys |
Andrew M. Kuchling | d0939fa | 2000-06-10 23:06:53 +0000 | [diff] [blame] | 19 | |
Andrew M. Kuchling | 289d9d4 | 2000-06-26 23:54:03 +0000 | [diff] [blame] | 20 | # Some constants, most notably the ACS_* ones, are only added to the C |
| 21 | # _curses module's dictionary after initscr() is called. (Some |
| 22 | # versions of SGI's curses don't define values for those constants |
| 23 | # until initscr() has been called.) This wrapper function calls the |
| 24 | # underlying C initscr(), and then copies the constants from the |
| 25 | # _curses module to the curses package's dictionary. Don't do 'from |
| 26 | # curses import *' if you'll be needing the ACS_* constants. |
| 27 | |
| 28 | def initscr(): |
| 29 | import _curses, curses |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 30 | # we call setupterm() here because it raises an error |
| 31 | # instead of calling exit() in error cases. |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 32 | setupterm(term=_os.environ.get("TERM", "unknown"), |
| 33 | fd=_sys.__stdout__.fileno()) |
Andrew M. Kuchling | 289d9d4 | 2000-06-26 23:54:03 +0000 | [diff] [blame] | 34 | stdscr = _curses.initscr() |
| 35 | for key, value in _curses.__dict__.items(): |
| 36 | if key[0:4] == 'ACS_' or key in ('LINES', 'COLS'): |
| 37 | setattr(curses, key, value) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 38 | |
Andrew M. Kuchling | 289d9d4 | 2000-06-26 23:54:03 +0000 | [diff] [blame] | 39 | return stdscr |
Andrew M. Kuchling | d0939fa | 2000-06-10 23:06:53 +0000 | [diff] [blame] | 40 | |
Andrew M. Kuchling | 37f0263 | 2001-04-05 16:08:41 +0000 | [diff] [blame] | 41 | # This is a similar wrapper for start_color(), which adds the COLORS and |
| 42 | # COLOR_PAIRS variables which are only available after start_color() is |
| 43 | # called. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 44 | |
Andrew M. Kuchling | 37f0263 | 2001-04-05 16:08:41 +0000 | [diff] [blame] | 45 | def start_color(): |
| 46 | import _curses, curses |
| 47 | retval = _curses.start_color() |
| 48 | if hasattr(_curses, 'COLORS'): |
| 49 | curses.COLORS = _curses.COLORS |
| 50 | if hasattr(_curses, 'COLOR_PAIRS'): |
| 51 | curses.COLOR_PAIRS = _curses.COLOR_PAIRS |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 52 | return retval |
Andrew M. Kuchling | 37f0263 | 2001-04-05 16:08:41 +0000 | [diff] [blame] | 53 | |
Andrew M. Kuchling | b45bd32 | 2000-08-01 01:21:11 +0000 | [diff] [blame] | 54 | # Import Python has_key() implementation if _curses doesn't contain has_key() |
| 55 | |
| 56 | try: |
| 57 | has_key |
| 58 | except NameError: |
| 59 | from has_key import has_key |