PageRenderTime 55ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Doc/library/curses.rst

http://unladen-swallow.googlecode.com/
ReStructuredText | 1693 lines | 1159 code | 534 blank | 0 comment | 0 complexity | 674a04f7a5475070edd90ae3cd22d8af MD5 | raw file
Possible License(s): 0BSD, BSD-3-Clause
  1. :mod:`curses` --- Terminal handling for character-cell displays
  2. ===============================================================
  3. .. module:: curses
  4. :synopsis: An interface to the curses library, providing portable terminal handling.
  5. .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
  6. .. sectionauthor:: Eric Raymond <esr@thyrsus.com>
  7. .. versionchanged:: 1.6
  8. Added support for the ``ncurses`` library and converted to a package.
  9. The :mod:`curses` module provides an interface to the curses library, the
  10. de-facto standard for portable advanced terminal handling.
  11. While curses is most widely used in the Unix environment, versions are available
  12. for DOS, OS/2, and possibly other systems as well. This extension module is
  13. designed to match the API of ncurses, an open-source curses library hosted on
  14. Linux and the BSD variants of Unix.
  15. .. note::
  16. Since version 5.4, the ncurses library decides how to interpret non-ASCII data
  17. using the ``nl_langinfo`` function. That means that you have to call
  18. :func:`locale.setlocale` in the application and encode Unicode strings
  19. using one of the system's available encodings. This example uses the
  20. system's default encoding::
  21. import locale
  22. locale.setlocale(locale.LC_ALL, '')
  23. code = locale.getpreferredencoding()
  24. Then use *code* as the encoding for :meth:`str.encode` calls.
  25. .. seealso::
  26. Module :mod:`curses.ascii`
  27. Utilities for working with ASCII characters, regardless of your locale settings.
  28. Module :mod:`curses.panel`
  29. A panel stack extension that adds depth to curses windows.
  30. Module :mod:`curses.textpad`
  31. Editable text widget for curses supporting :program:`Emacs`\ -like bindings.
  32. Module :mod:`curses.wrapper`
  33. Convenience function to ensure proper terminal setup and resetting on
  34. application entry and exit.
  35. :ref:`curses-howto`
  36. Tutorial material on using curses with Python, by Andrew Kuchling and Eric
  37. Raymond.
  38. The :file:`Demo/curses/` directory in the Python source distribution contains
  39. some example programs using the curses bindings provided by this module.
  40. .. _curses-functions:
  41. Functions
  42. ---------
  43. The module :mod:`curses` defines the following exception:
  44. .. exception:: error
  45. Exception raised when a curses library function returns an error.
  46. .. note::
  47. Whenever *x* or *y* arguments to a function or a method are optional, they
  48. default to the current cursor location. Whenever *attr* is optional, it defaults
  49. to :const:`A_NORMAL`.
  50. The module :mod:`curses` defines the following functions:
  51. .. function:: baudrate()
  52. Returns the output speed of the terminal in bits per second. On software
  53. terminal emulators it will have a fixed high value. Included for historical
  54. reasons; in former times, it was used to write output loops for time delays and
  55. occasionally to change interfaces depending on the line speed.
  56. .. function:: beep()
  57. Emit a short attention sound.
  58. .. function:: can_change_color()
  59. Returns true or false, depending on whether the programmer can change the colors
  60. displayed by the terminal.
  61. .. function:: cbreak()
  62. Enter cbreak mode. In cbreak mode (sometimes called "rare" mode) normal tty
  63. line buffering is turned off and characters are available to be read one by one.
  64. However, unlike raw mode, special characters (interrupt, quit, suspend, and flow
  65. control) retain their effects on the tty driver and calling program. Calling
  66. first :func:`raw` then :func:`cbreak` leaves the terminal in cbreak mode.
  67. .. function:: color_content(color_number)
  68. Returns the intensity of the red, green, and blue (RGB) components in the color
  69. *color_number*, which must be between ``0`` and :const:`COLORS`. A 3-tuple is
  70. returned, containing the R,G,B values for the given color, which will be between
  71. ``0`` (no component) and ``1000`` (maximum amount of component).
  72. .. function:: color_pair(color_number)
  73. Returns the attribute value for displaying text in the specified color. This
  74. attribute value can be combined with :const:`A_STANDOUT`, :const:`A_REVERSE`,
  75. and the other :const:`A_\*` attributes. :func:`pair_number` is the counterpart
  76. to this function.
  77. .. function:: curs_set(visibility)
  78. Sets the cursor state. *visibility* can be set to 0, 1, or 2, for invisible,
  79. normal, or very visible. If the terminal supports the visibility requested, the
  80. previous cursor state is returned; otherwise, an exception is raised. On many
  81. terminals, the "visible" mode is an underline cursor and the "very visible" mode
  82. is a block cursor.
  83. .. function:: def_prog_mode()
  84. Saves the current terminal mode as the "program" mode, the mode when the running
  85. program is using curses. (Its counterpart is the "shell" mode, for when the
  86. program is not in curses.) Subsequent calls to :func:`reset_prog_mode` will
  87. restore this mode.
  88. .. function:: def_shell_mode()
  89. Saves the current terminal mode as the "shell" mode, the mode when the running
  90. program is not using curses. (Its counterpart is the "program" mode, when the
  91. program is using curses capabilities.) Subsequent calls to
  92. :func:`reset_shell_mode` will restore this mode.
  93. .. function:: delay_output(ms)
  94. Inserts an *ms* millisecond pause in output.
  95. .. function:: doupdate()
  96. Update the physical screen. The curses library keeps two data structures, one
  97. representing the current physical screen contents and a virtual screen
  98. representing the desired next state. The :func:`doupdate` ground updates the
  99. physical screen to match the virtual screen.
  100. The virtual screen may be updated by a :meth:`noutrefresh` call after write
  101. operations such as :meth:`addstr` have been performed on a window. The normal
  102. :meth:`refresh` call is simply :meth:`noutrefresh` followed by :func:`doupdate`;
  103. if you have to update multiple windows, you can speed performance and perhaps
  104. reduce screen flicker by issuing :meth:`noutrefresh` calls on all windows,
  105. followed by a single :func:`doupdate`.
  106. .. function:: echo()
  107. Enter echo mode. In echo mode, each character input is echoed to the screen as
  108. it is entered.
  109. .. function:: endwin()
  110. De-initialize the library, and return terminal to normal status.
  111. .. function:: erasechar()
  112. Returns the user's current erase character. Under Unix operating systems this
  113. is a property of the controlling tty of the curses program, and is not set by
  114. the curses library itself.
  115. .. function:: filter()
  116. The :func:`filter` routine, if used, must be called before :func:`initscr` is
  117. called. The effect is that, during those calls, LINES is set to 1; the
  118. capabilities clear, cup, cud, cud1, cuu1, cuu, vpa are disabled; and the home
  119. string is set to the value of cr. The effect is that the cursor is confined to
  120. the current line, and so are screen updates. This may be used for enabling
  121. character-at-a-time line editing without touching the rest of the screen.
  122. .. function:: flash()
  123. Flash the screen. That is, change it to reverse-video and then change it back
  124. in a short interval. Some people prefer such as 'visible bell' to the audible
  125. attention signal produced by :func:`beep`.
  126. .. function:: flushinp()
  127. Flush all input buffers. This throws away any typeahead that has been typed
  128. by the user and has not yet been processed by the program.
  129. .. function:: getmouse()
  130. After :meth:`getch` returns :const:`KEY_MOUSE` to signal a mouse event, this
  131. method should be call to retrieve the queued mouse event, represented as a
  132. 5-tuple ``(id, x, y, z, bstate)``. *id* is an ID value used to distinguish
  133. multiple devices, and *x*, *y*, *z* are the event's coordinates. (*z* is
  134. currently unused.). *bstate* is an integer value whose bits will be set to
  135. indicate the type of event, and will be the bitwise OR of one or more of the
  136. following constants, where *n* is the button number from 1 to 4:
  137. :const:`BUTTONn_PRESSED`, :const:`BUTTONn_RELEASED`, :const:`BUTTONn_CLICKED`,
  138. :const:`BUTTONn_DOUBLE_CLICKED`, :const:`BUTTONn_TRIPLE_CLICKED`,
  139. :const:`BUTTON_SHIFT`, :const:`BUTTON_CTRL`, :const:`BUTTON_ALT`.
  140. .. function:: getsyx()
  141. Returns the current coordinates of the virtual screen cursor in y and x. If
  142. leaveok is currently true, then -1,-1 is returned.
  143. .. function:: getwin(file)
  144. Reads window related data stored in the file by an earlier :func:`putwin` call.
  145. The routine then creates and initializes a new window using that data, returning
  146. the new window object.
  147. .. function:: has_colors()
  148. Returns true if the terminal can display colors; otherwise, it returns false.
  149. .. function:: has_ic()
  150. Returns true if the terminal has insert- and delete- character capabilities.
  151. This function is included for historical reasons only, as all modern software
  152. terminal emulators have such capabilities.
  153. .. function:: has_il()
  154. Returns true if the terminal has insert- and delete-line capabilities, or can
  155. simulate them using scrolling regions. This function is included for
  156. historical reasons only, as all modern software terminal emulators have such
  157. capabilities.
  158. .. function:: has_key(ch)
  159. Takes a key value *ch*, and returns true if the current terminal type recognizes
  160. a key with that value.
  161. .. function:: halfdelay(tenths)
  162. Used for half-delay mode, which is similar to cbreak mode in that characters
  163. typed by the user are immediately available to the program. However, after
  164. blocking for *tenths* tenths of seconds, an exception is raised if nothing has
  165. been typed. The value of *tenths* must be a number between 1 and 255. Use
  166. :func:`nocbreak` to leave half-delay mode.
  167. .. function:: init_color(color_number, r, g, b)
  168. Changes the definition of a color, taking the number of the color to be changed
  169. followed by three RGB values (for the amounts of red, green, and blue
  170. components). The value of *color_number* must be between ``0`` and
  171. :const:`COLORS`. Each of *r*, *g*, *b*, must be a value between ``0`` and
  172. ``1000``. When :func:`init_color` is used, all occurrences of that color on the
  173. screen immediately change to the new definition. This function is a no-op on
  174. most terminals; it is active only if :func:`can_change_color` returns ``1``.
  175. .. function:: init_pair(pair_number, fg, bg)
  176. Changes the definition of a color-pair. It takes three arguments: the number of
  177. the color-pair to be changed, the foreground color number, and the background
  178. color number. The value of *pair_number* must be between ``1`` and
  179. ``COLOR_PAIRS - 1`` (the ``0`` color pair is wired to white on black and cannot
  180. be changed). The value of *fg* and *bg* arguments must be between ``0`` and
  181. :const:`COLORS`. If the color-pair was previously initialized, the screen is
  182. refreshed and all occurrences of that color-pair are changed to the new
  183. definition.
  184. .. function:: initscr()
  185. Initialize the library. Returns a :class:`WindowObject` which represents the
  186. whole screen.
  187. .. note::
  188. If there is an error opening the terminal, the underlying curses library may
  189. cause the interpreter to exit.
  190. .. function:: isendwin()
  191. Returns true if :func:`endwin` has been called (that is, the curses library has
  192. been deinitialized).
  193. .. function:: keyname(k)
  194. Return the name of the key numbered *k*. The name of a key generating printable
  195. ASCII character is the key's character. The name of a control-key combination
  196. is a two-character string consisting of a caret followed by the corresponding
  197. printable ASCII character. The name of an alt-key combination (128-255) is a
  198. string consisting of the prefix 'M-' followed by the name of the corresponding
  199. ASCII character.
  200. .. function:: killchar()
  201. Returns the user's current line kill character. Under Unix operating systems
  202. this is a property of the controlling tty of the curses program, and is not set
  203. by the curses library itself.
  204. .. function:: longname()
  205. Returns a string containing the terminfo long name field describing the current
  206. terminal. The maximum length of a verbose description is 128 characters. It is
  207. defined only after the call to :func:`initscr`.
  208. .. function:: meta(yes)
  209. If *yes* is 1, allow 8-bit characters to be input. If *yes* is 0, allow only
  210. 7-bit chars.
  211. .. function:: mouseinterval(interval)
  212. Sets the maximum time in milliseconds that can elapse between press and release
  213. events in order for them to be recognized as a click, and returns the previous
  214. interval value. The default value is 200 msec, or one fifth of a second.
  215. .. function:: mousemask(mousemask)
  216. Sets the mouse events to be reported, and returns a tuple ``(availmask,
  217. oldmask)``. *availmask* indicates which of the specified mouse events can be
  218. reported; on complete failure it returns 0. *oldmask* is the previous value of
  219. the given window's mouse event mask. If this function is never called, no mouse
  220. events are ever reported.
  221. .. function:: napms(ms)
  222. Sleep for *ms* milliseconds.
  223. .. function:: newpad(nlines, ncols)
  224. Creates and returns a pointer to a new pad data structure with the given number
  225. of lines and columns. A pad is returned as a window object.
  226. A pad is like a window, except that it is not restricted by the screen size, and
  227. is not necessarily associated with a particular part of the screen. Pads can be
  228. used when a large window is needed, and only a part of the window will be on the
  229. screen at one time. Automatic refreshes of pads (such as from scrolling or
  230. echoing of input) do not occur. The :meth:`refresh` and :meth:`noutrefresh`
  231. methods of a pad require 6 arguments to specify the part of the pad to be
  232. displayed and the location on the screen to be used for the display. The
  233. arguments are pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol; the p
  234. arguments refer to the upper left corner of the pad region to be displayed and
  235. the s arguments define a clipping box on the screen within which the pad region
  236. is to be displayed.
  237. .. function:: newwin([nlines, ncols,] begin_y, begin_x)
  238. Return a new window, whose left-upper corner is at ``(begin_y, begin_x)``, and
  239. whose height/width is *nlines*/*ncols*.
  240. By default, the window will extend from the specified position to the lower
  241. right corner of the screen.
  242. .. function:: nl()
  243. Enter newline mode. This mode translates the return key into newline on input,
  244. and translates newline into return and line-feed on output. Newline mode is
  245. initially on.
  246. .. function:: nocbreak()
  247. Leave cbreak mode. Return to normal "cooked" mode with line buffering.
  248. .. function:: noecho()
  249. Leave echo mode. Echoing of input characters is turned off.
  250. .. function:: nonl()
  251. Leave newline mode. Disable translation of return into newline on input, and
  252. disable low-level translation of newline into newline/return on output (but this
  253. does not change the behavior of ``addch('\n')``, which always does the
  254. equivalent of return and line feed on the virtual screen). With translation
  255. off, curses can sometimes speed up vertical motion a little; also, it will be
  256. able to detect the return key on input.
  257. .. function:: noqiflush()
  258. When the noqiflush routine is used, normal flush of input and output queues
  259. associated with the INTR, QUIT and SUSP characters will not be done. You may
  260. want to call :func:`noqiflush` in a signal handler if you want output to
  261. continue as though the interrupt had not occurred, after the handler exits.
  262. .. function:: noraw()
  263. Leave raw mode. Return to normal "cooked" mode with line buffering.
  264. .. function:: pair_content(pair_number)
  265. Returns a tuple ``(fg, bg)`` containing the colors for the requested color pair.
  266. The value of *pair_number* must be between ``1`` and ``COLOR_PAIRS - 1``.
  267. .. function:: pair_number(attr)
  268. Returns the number of the color-pair set by the attribute value *attr*.
  269. :func:`color_pair` is the counterpart to this function.
  270. .. function:: putp(string)
  271. Equivalent to ``tputs(str, 1, putchar)``; emits the value of a specified
  272. terminfo capability for the current terminal. Note that the output of putp
  273. always goes to standard output.
  274. .. function:: qiflush( [flag] )
  275. If *flag* is false, the effect is the same as calling :func:`noqiflush`. If
  276. *flag* is true, or no argument is provided, the queues will be flushed when
  277. these control characters are read.
  278. .. function:: raw()
  279. Enter raw mode. In raw mode, normal line buffering and processing of
  280. interrupt, quit, suspend, and flow control keys are turned off; characters are
  281. presented to curses input functions one by one.
  282. .. function:: reset_prog_mode()
  283. Restores the terminal to "program" mode, as previously saved by
  284. :func:`def_prog_mode`.
  285. .. function:: reset_shell_mode()
  286. Restores the terminal to "shell" mode, as previously saved by
  287. :func:`def_shell_mode`.
  288. .. function:: setsyx(y, x)
  289. Sets the virtual screen cursor to *y*, *x*. If *y* and *x* are both -1, then
  290. leaveok is set.
  291. .. function:: setupterm([termstr, fd])
  292. Initializes the terminal. *termstr* is a string giving the terminal name; if
  293. omitted, the value of the TERM environment variable will be used. *fd* is the
  294. file descriptor to which any initialization sequences will be sent; if not
  295. supplied, the file descriptor for ``sys.stdout`` will be used.
  296. .. function:: start_color()
  297. Must be called if the programmer wants to use colors, and before any other color
  298. manipulation routine is called. It is good practice to call this routine right
  299. after :func:`initscr`.
  300. :func:`start_color` initializes eight basic colors (black, red, green, yellow,
  301. blue, magenta, cyan, and white), and two global variables in the :mod:`curses`
  302. module, :const:`COLORS` and :const:`COLOR_PAIRS`, containing the maximum number
  303. of colors and color-pairs the terminal can support. It also restores the colors
  304. on the terminal to the values they had when the terminal was just turned on.
  305. .. function:: termattrs()
  306. Returns a logical OR of all video attributes supported by the terminal. This
  307. information is useful when a curses program needs complete control over the
  308. appearance of the screen.
  309. .. function:: termname()
  310. Returns the value of the environment variable TERM, truncated to 14 characters.
  311. .. function:: tigetflag(capname)
  312. Returns the value of the Boolean capability corresponding to the terminfo
  313. capability name *capname*. The value ``-1`` is returned if *capname* is not a
  314. Boolean capability, or ``0`` if it is canceled or absent from the terminal
  315. description.
  316. .. function:: tigetnum(capname)
  317. Returns the value of the numeric capability corresponding to the terminfo
  318. capability name *capname*. The value ``-2`` is returned if *capname* is not a
  319. numeric capability, or ``-1`` if it is canceled or absent from the terminal
  320. description.
  321. .. function:: tigetstr(capname)
  322. Returns the value of the string capability corresponding to the terminfo
  323. capability name *capname*. ``None`` is returned if *capname* is not a string
  324. capability, or is canceled or absent from the terminal description.
  325. .. function:: tparm(str[,...])
  326. Instantiates the string *str* with the supplied parameters, where *str* should
  327. be a parameterized string obtained from the terminfo database. E.g.
  328. ``tparm(tigetstr("cup"), 5, 3)`` could result in ``'\033[6;4H'``, the exact
  329. result depending on terminal type.
  330. .. function:: typeahead(fd)
  331. Specifies that the file descriptor *fd* be used for typeahead checking. If *fd*
  332. is ``-1``, then no typeahead checking is done.
  333. The curses library does "line-breakout optimization" by looking for typeahead
  334. periodically while updating the screen. If input is found, and it is coming
  335. from a tty, the current update is postponed until refresh or doupdate is called
  336. again, allowing faster response to commands typed in advance. This function
  337. allows specifying a different file descriptor for typeahead checking.
  338. .. function:: unctrl(ch)
  339. Returns a string which is a printable representation of the character *ch*.
  340. Control characters are displayed as a caret followed by the character, for
  341. example as ``^C``. Printing characters are left as they are.
  342. .. function:: ungetch(ch)
  343. Push *ch* so the next :meth:`getch` will return it.
  344. .. note::
  345. Only one *ch* can be pushed before :meth:`getch` is called.
  346. .. function:: ungetmouse(id, x, y, z, bstate)
  347. Push a :const:`KEY_MOUSE` event onto the input queue, associating the given
  348. state data with it.
  349. .. function:: use_env(flag)
  350. If used, this function should be called before :func:`initscr` or newterm are
  351. called. When *flag* is false, the values of lines and columns specified in the
  352. terminfo database will be used, even if environment variables :envvar:`LINES`
  353. and :envvar:`COLUMNS` (used by default) are set, or if curses is running in a
  354. window (in which case default behavior would be to use the window size if
  355. :envvar:`LINES` and :envvar:`COLUMNS` are not set).
  356. .. function:: use_default_colors()
  357. Allow use of default values for colors on terminals supporting this feature. Use
  358. this to support transparency in your application. The default color is assigned
  359. to the color number -1. After calling this function, ``init_pair(x,
  360. curses.COLOR_RED, -1)`` initializes, for instance, color pair *x* to a red
  361. foreground color on the default background.
  362. .. _curses-window-objects:
  363. Window Objects
  364. --------------
  365. Window objects, as returned by :func:`initscr` and :func:`newwin` above, have
  366. the following methods:
  367. .. method:: window.addch([y, x,] ch[, attr])
  368. .. note::
  369. A *character* means a C character (an ASCII code), rather then a Python
  370. character (a string of length 1). (This note is true whenever the documentation
  371. mentions a character.) The builtin :func:`ord` is handy for conveying strings to
  372. codes.
  373. Paint character *ch* at ``(y, x)`` with attributes *attr*, overwriting any
  374. character previously painter at that location. By default, the character
  375. position and attributes are the current settings for the window object.
  376. .. method:: window.addnstr([y, x,] str, n[, attr])
  377. Paint at most *n* characters of the string *str* at ``(y, x)`` with attributes
  378. *attr*, overwriting anything previously on the display.
  379. .. method:: window.addstr([y, x,] str[, attr])
  380. Paint the string *str* at ``(y, x)`` with attributes *attr*, overwriting
  381. anything previously on the display.
  382. .. method:: window.attroff(attr)
  383. Remove attribute *attr* from the "background" set applied to all writes to the
  384. current window.
  385. .. method:: window.attron(attr)
  386. Add attribute *attr* from the "background" set applied to all writes to the
  387. current window.
  388. .. method:: window.attrset(attr)
  389. Set the "background" set of attributes to *attr*. This set is initially 0 (no
  390. attributes).
  391. .. method:: window.bkgd(ch[, attr])
  392. Sets the background property of the window to the character *ch*, with
  393. attributes *attr*. The change is then applied to every character position in
  394. that window:
  395. * The attribute of every character in the window is changed to the new
  396. background attribute.
  397. * Wherever the former background character appears, it is changed to the new
  398. background character.
  399. .. method:: window.bkgdset(ch[, attr])
  400. Sets the window's background. A window's background consists of a character and
  401. any combination of attributes. The attribute part of the background is combined
  402. (OR'ed) with all non-blank characters that are written into the window. Both
  403. the character and attribute parts of the background are combined with the blank
  404. characters. The background becomes a property of the character and moves with
  405. the character through any scrolling and insert/delete line/character operations.
  406. .. method:: window.border([ls[, rs[, ts[, bs[, tl[, tr[, bl[, br]]]]]]]])
  407. Draw a border around the edges of the window. Each parameter specifies the
  408. character to use for a specific part of the border; see the table below for more
  409. details. The characters can be specified as integers or as one-character
  410. strings.
  411. .. note::
  412. A ``0`` value for any parameter will cause the default character to be used for
  413. that parameter. Keyword parameters can *not* be used. The defaults are listed
  414. in this table:
  415. +-----------+---------------------+-----------------------+
  416. | Parameter | Description | Default value |
  417. +===========+=====================+=======================+
  418. | *ls* | Left side | :const:`ACS_VLINE` |
  419. +-----------+---------------------+-----------------------+
  420. | *rs* | Right side | :const:`ACS_VLINE` |
  421. +-----------+---------------------+-----------------------+
  422. | *ts* | Top | :const:`ACS_HLINE` |
  423. +-----------+---------------------+-----------------------+
  424. | *bs* | Bottom | :const:`ACS_HLINE` |
  425. +-----------+---------------------+-----------------------+
  426. | *tl* | Upper-left corner | :const:`ACS_ULCORNER` |
  427. +-----------+---------------------+-----------------------+
  428. | *tr* | Upper-right corner | :const:`ACS_URCORNER` |
  429. +-----------+---------------------+-----------------------+
  430. | *bl* | Bottom-left corner | :const:`ACS_LLCORNER` |
  431. +-----------+---------------------+-----------------------+
  432. | *br* | Bottom-right corner | :const:`ACS_LRCORNER` |
  433. +-----------+---------------------+-----------------------+
  434. .. method:: window.box([vertch, horch])
  435. Similar to :meth:`border`, but both *ls* and *rs* are *vertch* and both *ts* and
  436. bs are *horch*. The default corner characters are always used by this function.
  437. .. method:: window.chgat([y, x, ] [num,] attr)
  438. Sets the attributes of *num* characters at the current cursor position, or at
  439. position ``(y, x)`` if supplied. If no value of *num* is given or *num* = -1,
  440. the attribute will be set on all the characters to the end of the line. This
  441. function does not move the cursor. The changed line will be touched using the
  442. :meth:`touchline` method so that the contents will be redisplayed by the next
  443. window refresh.
  444. .. method:: window.clear()
  445. Like :meth:`erase`, but also causes the whole window to be repainted upon next
  446. call to :meth:`refresh`.
  447. .. method:: window.clearok(yes)
  448. If *yes* is 1, the next call to :meth:`refresh` will clear the window
  449. completely.
  450. .. method:: window.clrtobot()
  451. Erase from cursor to the end of the window: all lines below the cursor are
  452. deleted, and then the equivalent of :meth:`clrtoeol` is performed.
  453. .. method:: window.clrtoeol()
  454. Erase from cursor to the end of the line.
  455. .. method:: window.cursyncup()
  456. Updates the current cursor position of all the ancestors of the window to
  457. reflect the current cursor position of the window.
  458. .. method:: window.delch([y, x])
  459. Delete any character at ``(y, x)``.
  460. .. method:: window.deleteln()
  461. Delete the line under the cursor. All following lines are moved up by 1 line.
  462. .. method:: window.derwin([nlines, ncols,] begin_y, begin_x)
  463. An abbreviation for "derive window", :meth:`derwin` is the same as calling
  464. :meth:`subwin`, except that *begin_y* and *begin_x* are relative to the origin
  465. of the window, rather than relative to the entire screen. Returns a window
  466. object for the derived window.
  467. .. method:: window.echochar(ch[, attr])
  468. Add character *ch* with attribute *attr*, and immediately call :meth:`refresh`
  469. on the window.
  470. .. method:: window.enclose(y, x)
  471. Tests whether the given pair of screen-relative character-cell coordinates are
  472. enclosed by the given window, returning true or false. It is useful for
  473. determining what subset of the screen windows enclose the location of a mouse
  474. event.
  475. .. method:: window.erase()
  476. Clear the window.
  477. .. method:: window.getbegyx()
  478. Return a tuple ``(y, x)`` of co-ordinates of upper-left corner.
  479. .. method:: window.getch([y, x])
  480. Get a character. Note that the integer returned does *not* have to be in ASCII
  481. range: function keys, keypad keys and so on return numbers higher than 256. In
  482. no-delay mode, -1 is returned if there is no input.
  483. .. method:: window.getkey([y, x])
  484. Get a character, returning a string instead of an integer, as :meth:`getch`
  485. does. Function keys, keypad keys and so on return a multibyte string containing
  486. the key name. In no-delay mode, an exception is raised if there is no input.
  487. .. method:: window.getmaxyx()
  488. Return a tuple ``(y, x)`` of the height and width of the window.
  489. .. method:: window.getparyx()
  490. Returns the beginning coordinates of this window relative to its parent window
  491. into two integer variables y and x. Returns ``-1,-1`` if this window has no
  492. parent.
  493. .. method:: window.getstr([y, x])
  494. Read a string from the user, with primitive line editing capacity.
  495. .. method:: window.getyx()
  496. Return a tuple ``(y, x)`` of current cursor position relative to the window's
  497. upper-left corner.
  498. .. method:: window.hline([y, x,] ch, n)
  499. Display a horizontal line starting at ``(y, x)`` with length *n* consisting of
  500. the character *ch*.
  501. .. method:: window.idcok(flag)
  502. If *flag* is false, curses no longer considers using the hardware insert/delete
  503. character feature of the terminal; if *flag* is true, use of character insertion
  504. and deletion is enabled. When curses is first initialized, use of character
  505. insert/delete is enabled by default.
  506. .. method:: window.idlok(yes)
  507. If called with *yes* equal to 1, :mod:`curses` will try and use hardware line
  508. editing facilities. Otherwise, line insertion/deletion are disabled.
  509. .. method:: window.immedok(flag)
  510. If *flag* is true, any change in the window image automatically causes the
  511. window to be refreshed; you no longer have to call :meth:`refresh` yourself.
  512. However, it may degrade performance considerably, due to repeated calls to
  513. wrefresh. This option is disabled by default.
  514. .. method:: window.inch([y, x])
  515. Return the character at the given position in the window. The bottom 8 bits are
  516. the character proper, and upper bits are the attributes.
  517. .. method:: window.insch([y, x,] ch[, attr])
  518. Paint character *ch* at ``(y, x)`` with attributes *attr*, moving the line from
  519. position *x* right by one character.
  520. .. method:: window.insdelln(nlines)
  521. Inserts *nlines* lines into the specified window above the current line. The
  522. *nlines* bottom lines are lost. For negative *nlines*, delete *nlines* lines
  523. starting with the one under the cursor, and move the remaining lines up. The
  524. bottom *nlines* lines are cleared. The current cursor position remains the
  525. same.
  526. .. method:: window.insertln()
  527. Insert a blank line under the cursor. All following lines are moved down by 1
  528. line.
  529. .. method:: window.insnstr([y, x,] str, n [, attr])
  530. Insert a character string (as many characters as will fit on the line) before
  531. the character under the cursor, up to *n* characters. If *n* is zero or
  532. negative, the entire string is inserted. All characters to the right of the
  533. cursor are shifted right, with the rightmost characters on the line being lost.
  534. The cursor position does not change (after moving to *y*, *x*, if specified).
  535. .. method:: window.insstr([y, x, ] str [, attr])
  536. Insert a character string (as many characters as will fit on the line) before
  537. the character under the cursor. All characters to the right of the cursor are
  538. shifted right, with the rightmost characters on the line being lost. The cursor
  539. position does not change (after moving to *y*, *x*, if specified).
  540. .. method:: window.instr([y, x] [, n])
  541. Returns a string of characters, extracted from the window starting at the
  542. current cursor position, or at *y*, *x* if specified. Attributes are stripped
  543. from the characters. If *n* is specified, :meth:`instr` returns return a string
  544. at most *n* characters long (exclusive of the trailing NUL).
  545. .. method:: window.is_linetouched(line)
  546. Returns true if the specified line was modified since the last call to
  547. :meth:`refresh`; otherwise returns false. Raises a :exc:`curses.error`
  548. exception if *line* is not valid for the given window.
  549. .. method:: window.is_wintouched()
  550. Returns true if the specified window was modified since the last call to
  551. :meth:`refresh`; otherwise returns false.
  552. .. method:: window.keypad(yes)
  553. If *yes* is 1, escape sequences generated by some keys (keypad, function keys)
  554. will be interpreted by :mod:`curses`. If *yes* is 0, escape sequences will be
  555. left as is in the input stream.
  556. .. method:: window.leaveok(yes)
  557. If *yes* is 1, cursor is left where it is on update, instead of being at "cursor
  558. position." This reduces cursor movement where possible. If possible the cursor
  559. will be made invisible.
  560. If *yes* is 0, cursor will always be at "cursor position" after an update.
  561. .. method:: window.move(new_y, new_x)
  562. Move cursor to ``(new_y, new_x)``.
  563. .. method:: window.mvderwin(y, x)
  564. Moves the window inside its parent window. The screen-relative parameters of
  565. the window are not changed. This routine is used to display different parts of
  566. the parent window at the same physical position on the screen.
  567. .. method:: window.mvwin(new_y, new_x)
  568. Move the window so its upper-left corner is at ``(new_y, new_x)``.
  569. .. method:: window.nodelay(yes)
  570. If *yes* is ``1``, :meth:`getch` will be non-blocking.
  571. .. method:: window.notimeout(yes)
  572. If *yes* is ``1``, escape sequences will not be timed out.
  573. If *yes* is ``0``, after a few milliseconds, an escape sequence will not be
  574. interpreted, and will be left in the input stream as is.
  575. .. method:: window.noutrefresh()
  576. Mark for refresh but wait. This function updates the data structure
  577. representing the desired state of the window, but does not force an update of
  578. the physical screen. To accomplish that, call :func:`doupdate`.
  579. .. method:: window.overlay(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])
  580. Overlay the window on top of *destwin*. The windows need not be the same size,
  581. only the overlapping region is copied. This copy is non-destructive, which means
  582. that the current background character does not overwrite the old contents of
  583. *destwin*.
  584. To get fine-grained control over the copied region, the second form of
  585. :meth:`overlay` can be used. *sminrow* and *smincol* are the upper-left
  586. coordinates of the source window, and the other variables mark a rectangle in
  587. the destination window.
  588. .. method:: window.overwrite(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])
  589. Overwrite the window on top of *destwin*. The windows need not be the same size,
  590. in which case only the overlapping region is copied. This copy is destructive,
  591. which means that the current background character overwrites the old contents of
  592. *destwin*.
  593. To get fine-grained control over the copied region, the second form of
  594. :meth:`overwrite` can be used. *sminrow* and *smincol* are the upper-left
  595. coordinates of the source window, the other variables mark a rectangle in the
  596. destination window.
  597. .. method:: window.putwin(file)
  598. Writes all data associated with the window into the provided file object. This
  599. information can be later retrieved using the :func:`getwin` function.
  600. .. method:: window.redrawln(beg, num)
  601. Indicates that the *num* screen lines, starting at line *beg*, are corrupted and
  602. should be completely redrawn on the next :meth:`refresh` call.
  603. .. method:: window.redrawwin()
  604. Touches the entire window, causing it to be completely redrawn on the next
  605. :meth:`refresh` call.
  606. .. method:: window.refresh([pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol])
  607. Update the display immediately (sync actual screen with previous
  608. drawing/deleting methods).
  609. The 6 optional arguments can only be specified when the window is a pad created
  610. with :func:`newpad`. The additional parameters are needed to indicate what part
  611. of the pad and screen are involved. *pminrow* and *pmincol* specify the upper
  612. left-hand corner of the rectangle to be displayed in the pad. *sminrow*,
  613. *smincol*, *smaxrow*, and *smaxcol* specify the edges of the rectangle to be
  614. displayed on the screen. The lower right-hand corner of the rectangle to be
  615. displayed in the pad is calculated from the screen coordinates, since the
  616. rectangles must be the same size. Both rectangles must be entirely contained
  617. within their respective structures. Negative values of *pminrow*, *pmincol*,
  618. *sminrow*, or *smincol* are treated as if they were zero.
  619. .. method:: window.scroll([lines=1])
  620. Scroll the screen or scrolling region upward by *lines* lines.
  621. .. method:: window.scrollok(flag)
  622. Controls what happens when the cursor of a window is moved off the edge of the
  623. window or scrolling region, either as a result of a newline action on the bottom
  624. line, or typing the last character of the last line. If *flag* is false, the
  625. cursor is left on the bottom line. If *flag* is true, the window is scrolled up
  626. one line. Note that in order to get the physical scrolling effect on the
  627. terminal, it is also necessary to call :meth:`idlok`.
  628. .. method:: window.setscrreg(top, bottom)
  629. Set the scrolling region from line *top* to line *bottom*. All scrolling actions
  630. will take place in this region.
  631. .. method:: window.standend()
  632. Turn off the standout attribute. On some terminals this has the side effect of
  633. turning off all attributes.
  634. .. method:: window.standout()
  635. Turn on attribute *A_STANDOUT*.
  636. .. method:: window.subpad([nlines, ncols,] begin_y, begin_x)
  637. Return a sub-window, whose upper-left corner is at ``(begin_y, begin_x)``, and
  638. whose width/height is *ncols*/*nlines*.
  639. .. method:: window.subwin([nlines, ncols,] begin_y, begin_x)
  640. Return a sub-window, whose upper-left corner is at ``(begin_y, begin_x)``, and
  641. whose width/height is *ncols*/*nlines*.
  642. By default, the sub-window will extend from the specified position to the lower
  643. right corner of the window.
  644. .. method:: window.syncdown()
  645. Touches each location in the window that has been touched in any of its ancestor
  646. windows. This routine is called by :meth:`refresh`, so it should almost never
  647. be necessary to call it manually.
  648. .. method:: window.syncok(flag)
  649. If called with *flag* set to true, then :meth:`syncup` is called automatically
  650. whenever there is a change in the window.
  651. .. method:: window.syncup()
  652. Touches all locations in ancestors of the window that have been changed in the
  653. window.
  654. .. method:: window.timeout(delay)
  655. Sets blocking or non-blocking read behavior for the window. If *delay* is
  656. negative, blocking read is used (which will wait indefinitely for input). If
  657. *delay* is zero, then non-blocking read is used, and -1 will be returned by
  658. :meth:`getch` if no input is waiting. If *delay* is positive, then
  659. :meth:`getch` will block for *delay* milliseconds, and return -1 if there is
  660. still no input at the end of that time.
  661. .. method:: window.touchline(start, count[, changed])
  662. Pretend *count* lines have been changed, starting with line *start*. If
  663. *changed* is supplied, it specifies whether the affected lines are marked as
  664. having been changed (*changed*\ =1) or unchanged (*changed*\ =0).
  665. .. method:: window.touchwin()
  666. Pretend the whole window has been changed, for purposes of drawing
  667. optimizations.
  668. .. method:: window.untouchwin()
  669. Marks all lines in the window as unchanged since the last call to
  670. :meth:`refresh`.
  671. .. method:: window.vline([y, x,] ch, n)
  672. Display a vertical line starting at ``(y, x)`` with length *n* consisting of the
  673. character *ch*.
  674. Constants
  675. ---------
  676. The :mod:`curses` module defines the following data members:
  677. .. data:: ERR
  678. Some curses routines that return an integer, such as :func:`getch`, return
  679. :const:`ERR` upon failure.
  680. .. data:: OK
  681. Some curses routines that return an integer, such as :func:`napms`, return
  682. :const:`OK` upon success.
  683. .. data:: version
  684. A string representing the current version of the module. Also available as
  685. :const:`__version__`.
  686. Several constants are available to specify character cell attributes:
  687. +------------------+-------------------------------+
  688. | Attribute | Meaning |
  689. +==================+===============================+
  690. | ``A_ALTCHARSET`` | Alternate character set mode. |
  691. +------------------+-------------------------------+
  692. | ``A_BLINK`` | Blink mode. |
  693. +------------------+-------------------------------+
  694. | ``A_BOLD`` | Bold mode. |
  695. +------------------+-------------------------------+
  696. | ``A_DIM`` | Dim mode. |
  697. +------------------+-------------------------------+
  698. | ``A_NORMAL`` | Normal attribute. |
  699. +------------------+-------------------------------+
  700. | ``A_STANDOUT`` | Standout mode. |
  701. +------------------+-------------------------------+
  702. | ``A_UNDERLINE`` | Underline mode. |
  703. +------------------+-------------------------------+
  704. Keys are referred to by integer constants with names starting with ``KEY_``.
  705. The exact keycaps available are system dependent.
  706. .. XXX this table is far too large! should it be alphabetized?
  707. +-------------------+--------------------------------------------+
  708. | Key constant | Key |
  709. +===================+============================================+
  710. | ``KEY_MIN`` | Minimum key value |
  711. +-------------------+--------------------------------------------+
  712. | ``KEY_BREAK`` | Break key (unreliable) |
  713. +-------------------+--------------------------------------------+
  714. | ``KEY_DOWN`` | Down-arrow |
  715. +-------------------+--------------------------------------------+
  716. | ``KEY_UP`` | Up-arrow |
  717. +-------------------+--------------------------------------------+
  718. | ``KEY_LEFT`` | Left-arrow |
  719. +-------------------+--------------------------------------------+
  720. | ``KEY_RIGHT`` | Right-arrow |
  721. +-------------------+--------------------------------------------+
  722. | ``KEY_HOME`` | Home key (upward+left arrow) |
  723. +-------------------+--------------------------------------------+
  724. | ``KEY_BACKSPACE`` | Backspace (unreliable) |
  725. +-------------------+--------------------------------------------+
  726. | ``KEY_F0`` | Function keys. Up to 64 function keys are |
  727. | | supported. |
  728. +-------------------+--------------------------------------------+
  729. | ``KEY_Fn`` | Value of function key *n* |
  730. +-------------------+--------------------------------------------+
  731. | ``KEY_DL`` | Delete line |
  732. +-------------------+--------------------------------------------+
  733. | ``KEY_IL`` | Insert line |
  734. +-------------------+--------------------------------------------+
  735. | ``KEY_DC`` | Delete character |
  736. +-------------------+--------------------------------------------+
  737. | ``KEY_IC`` | Insert char or enter insert mode |
  738. +-------------------+--------------------------------------------+
  739. | ``KEY_EIC`` | Exit insert char mode |
  740. +-------------------+--------------------------------------------+
  741. | ``KEY_CLEAR`` | Clear screen |
  742. +-------------------+--------------------------------------------+
  743. | ``KEY_EOS`` | Clear to end of screen |
  744. +-------------------+--------------------------------------------+
  745. | ``KEY_EOL`` | Clear to end of line |
  746. +-------------------+--------------------------------------------+
  747. | ``KEY_SF`` | Scroll 1 line forward |
  748. +-------------------+--------------------------------------------+
  749. | ``KEY_SR`` | Scroll 1 line backward (reverse) |
  750. +-------------------+--------------------------------------------+
  751. | ``KEY_NPAGE`` | Next page |
  752. +-------------------+--------------------------------------------+
  753. | ``KEY_PPAGE`` | Previous page |
  754. +-------------------+--------------------------------------------+
  755. | ``KEY_STAB`` | Set tab |
  756. +-------------------+--------------------------------------------+
  757. | ``KEY_CTAB`` | Clear tab |
  758. +-------------------+--------------------------------------------+
  759. | ``KEY_CATAB`` | Clear all tabs |
  760. +-------------------+--------------------------------------------+
  761. | ``KEY_ENTER`` | Enter or send (unreliable) |
  762. +-------------------+--------------------------------------------+
  763. | ``KEY_SRESET`` | Soft (partial) reset (unreliable) |
  764. +-------------------+--------------------------------------------+
  765. | ``KEY_RESET`` | Reset or hard reset (unreliable) |
  766. +-------------------+--------------------------------------------+
  767. | ``KEY_PRINT`` | Print |
  768. +-------------------+--------------------------------------------+
  769. | ``KEY_LL`` | Home down or bottom (lower left) |
  770. +-------------------+--------------------------------------------+
  771. | ``KEY_A1`` | Upper left of keypad |
  772. +-------------------+--------------------------------------------+
  773. | ``KEY_A3`` | Upper right of keypad |
  774. +-------------------+--------------------------------------------+
  775. | ``KEY_B2`` | Center of keypad |
  776. +-------------------+--------------------------------------------+
  777. | ``KEY_C1`` | Lower left of keypad |
  778. +-------------------+--------------------------------------------+
  779. | ``KEY_C3`` | Lower right of keypad |
  780. +-------------------+--------------------------------------------+
  781. | ``KEY_BTAB`` | Back tab |
  782. +-------------------+--------------------------------------------+
  783. | ``KEY_BEG`` | Beg (beginning) |
  784. +-------------------+--------------------------------------------+
  785. | ``KEY_CANCEL`` | Cancel |
  786. +-------------------+--------------------------------------------+
  787. | ``KEY_CLOSE`` | Close |
  788. +-------------------+--------------------------------------------+
  789. | ``KEY_COMMAND`` | Cmd (command) |
  790. +-------------------+--------------------------------------------+
  791. | ``KEY_COPY`` | Copy |
  792. +-------------------+--------------------------------------------+
  793. | ``KEY_CREATE`` | Create |
  794. +-------------------+--------------------------------------------+
  795. | ``KEY_END`` | End |
  796. +-------------------+--------------------------------------------+
  797. | ``KEY_EXIT`` | Exit |
  798. +-------------------+--------------------------------------------+
  799. | ``KEY_FIND`` | Find |
  800. +-------------------+--------------------------------------------+
  801. | ``KEY_HELP`` | Help |
  802. +-------------------+--------------------------------------------+
  803. | ``KEY_MARK`` | Mark |
  804. +-------------------+--------------------------------------------+
  805. | ``KEY_MESSAGE`` | Message |
  806. +-------------------+--------------------------------------------+
  807. | ``KEY_MOVE`` | Move |
  808. +-------------------+--------------------------------------------+
  809. | ``KEY_NEXT`` | Next |
  810. +-------------------+--------------------------------------------+
  811. | ``KEY_OPEN`` | Open |
  812. +-------------------+--------------------------------------------+
  813. | ``KEY_OPTIONS`` | Options |
  814. +-------------------+--------------------------------------------+
  815. | ``KEY_PREVIOUS`` | Prev (previous) |
  816. +-------------------+--------------------------------------------+
  817. | ``KEY_REDO`` | Redo |
  818. +-------------------+--------------------------------------------+
  819. | ``KEY_REFERENCE`` | Ref (reference) |
  820. +-------------------+--------------------------------------------+
  821. | ``KEY_REFRESH`` | Refresh |
  822. +-------------------+--------------------------------------------+
  823. | ``KEY_REPLACE`` | Replace |
  824. +-------------------+--------------------------------------------+
  825. | ``KEY_RESTART`` | Restart |
  826. +-------------------+--------------------------------------------+
  827. | ``KEY_RESUME`` | Resume |
  828. +-------------------+--------------------------------------------+
  829. | ``KEY_SAVE`` | Save |
  830. +-------------------+--------------------------------------------+
  831. | ``KEY_SBEG`` | Shifted Beg (beginning) |
  832. +-------------------+--------------------------------------------+
  833. | ``KEY_SCANCEL`` | Shifted Cancel |
  834. +-------------------+--------------------------------------------+
  835. | ``KEY_SCOMMAND`` | Shifted Command |
  836. +-------------------+--------------------------------------------+
  837. | ``KEY_SCOPY`` | Shifted Copy |
  838. +-------------------+--------------------------------------------+
  839. | ``KEY_SCREATE`` | Shifted Create |
  840. +-------------------+--------------------------------------------+
  841. | ``KEY_SDC`` | Shifted Delete char |
  842. +-------------------+--------------------------------------------+
  843. | ``KEY_SDL`` | Shifted Delete line |
  844. +-------------------+--------------------------------------------+
  845. | ``KEY_SELECT`` | Select |
  846. +-------------------+--------------------------------------------+
  847. | ``KEY_SEND`` | Shifted End |
  848. +-------------------+--------------------------------------------+
  849. | ``KEY_SEOL`` | Shifted Clear line |
  850. +-------------------+--------------------------------------------+
  851. | ``KEY_SEXIT`` | Shifted Dxit |
  852. +-------------------+--------------------------------------------+
  853. | ``KEY_SFIND`` | Shifted Find |
  854. +-------------------+--------------------------------------------+
  855. | ``KEY_SHELP`` | Shifted Help |
  856. +-------------------+--------------------------------------------+
  857. | ``KEY_SHOME`` | Shifted Home |
  858. +-------------------+--------------------------------------------+
  859. | ``KEY_SIC`` | Shifted Input |
  860. +-------------------+--------------------------------------------+
  861. | ``KEY_SLEFT`` | Shifted Left arrow |
  862. +-------------------+--------------------------------------------+
  863. | ``KEY_SMESSAGE`` | Shifted Message |
  864. +-------------------+--------------------------------------------+
  865. | ``KEY_SMOVE`` | Shifted Move |
  866. +-------------------+--------------------------------------------+
  867. | ``KEY_SNEXT`` | Shifted Next |
  868. +-------------------+--------------------------------------------+
  869. | ``KEY_SOPTIONS`` | Shifted Options |
  870. +-------------------+--------------------------------------------+
  871. | ``KEY_SPREVIOUS`` | Shifted Prev |
  872. +-------------------+--------------------------------------------+
  873. | ``KEY_SPRINT`` | Shifted Print |
  874. +-------------------+--------------------------------------------+
  875. | ``KEY_SREDO`` | Shifted Redo |
  876. +-------------------+--------------------------------------------+
  877. | ``KEY_SREPLACE`` | Shifted Replace |
  878. +-------------------+--------------------------------------------+
  879. | ``KEY_SRIGHT`` | Shifted Right arrow |
  880. +-------------------+--------------------------------------------+
  881. | ``KEY_SRSUME`` | Shifted Resume |
  882. +-------------------+--------------------------------------------+
  883. | ``KEY_SSAVE`` | Shifted Save |
  884. +-------------------+--------------------------------------------+
  885. | ``KEY_SSUSPEND`` | Shifted Suspend |
  886. +-------------------+--------------------------------------------+
  887. | ``KEY_SUNDO`` | Shifted Undo |
  888. +-------------------+--------------------------------------------+
  889. | ``KEY_SUSPEND`` | Suspend |
  890. +-------------------+--------------------------------------------+
  891. | ``KEY_UNDO`` | Undo |
  892. +-------------------+--------------------------------------------+
  893. | ``KEY_MOUSE`` | Mouse event has occurred |
  894. +-------------------+--------------------------------------------+
  895. | ``KEY_RESIZE`` | Terminal resize event |
  896. +-------------------+--------------------------------------------+
  897. | ``KEY_MAX`` | Maximum key value |
  898. +-------------------+--------------------------------------------+
  899. On VT100s and their software emulations, such as X terminal emulators, there are
  900. normally at least four function keys (:const:`KEY_F1`, :const:`KEY_F2`,
  901. :const:`KEY_F3`, :const:`KEY_F4`) available, and the arrow keys mapped to
  902. :const:`KEY_UP`, :const:`KEY_DOWN`, :const:`KEY_LEFT` and :const:`KEY_RIGHT` in
  903. the obvious way. If your machine has a PC keyboard, it is safe to expect arrow
  904. keys and twelve function keys (older PC keyboards may have only ten function
  905. keys); also, the following keypad mappings are standard:
  906. +------------------+-----------+
  907. | Keycap | Constant |
  908. +==================+===========+
  909. | :kbd:`Insert` | KEY_IC |
  910. +------------------+-----------+
  911. | :kbd:`Delete` | KEY_DC |
  912. +------------------+-----------+
  913. | :kbd:`Home` | KEY_HOME |
  914. +------------------+-----------+
  915. | :kbd:`End` | KEY_END |
  916. +------------------+-----------+
  917. | :kbd:`Page Up` | KEY_NPAGE |
  918. +------------------+-----------+
  919. | :kbd:`Page Down` | KEY_PPAGE |
  920. +------------------+-----------+
  921. The following table lists characters from the alternate character set. These are
  922. inherited from the VT100 terminal, and will generally be available on software
  923. emulations such as X terminals. When there is no graphic available, curses
  924. falls back on a crude printable ASCII approximation.
  925. .. note::
  926. These are available only after :func:`initscr` has been called.
  927. +------------------+------------------------------------------+
  928. | ACS code | Meaning |
  929. +==================+==========================================+
  930. | ``ACS_BBSS`` | alternate name for upper right corner |
  931. +------------------+------------------------------------------+
  932. | ``ACS_BLOCK`` | solid square block |
  933. +------------------+------------------------------------------+
  934. | ``ACS_BOARD`` | board of squares |
  935. +------------------+------------------------------------------+
  936. | ``ACS_BSBS`` | alternate name for horizontal line |
  937. +------------------+------------------------------------------+
  938. | ``ACS_BSSB`` | alternate name for upper left corner |
  939. +------------------+------------------------------------------+
  940. | ``ACS_BSSS`` | alternate name for top tee |
  941. +------------------+------------------------------------------+
  942. | ``ACS_BTEE`` | bottom tee |
  943. +------------------+------------------------------------------+
  944. | ``ACS_BULLET`` | bullet |
  945. +------------------+------------------------------------------+
  946. | ``ACS_CKBOARD`` | checker board (stipple) |
  947. +------------------+------------------------------------------+
  948. | ``ACS_DARROW`` | arrow pointing down |
  949. +------------------+------------------------------------------+
  950. | ``ACS_DEGREE`` | degree symbol |
  951. +------------------+------------------------------------------+
  952. | ``ACS_DIAMOND`` | diamond |
  953. +------------------+------------------------------------------+
  954. | ``ACS_GEQUAL`` | greater-than-or-equal-to |
  955. +------------------+------------------------------------------+
  956. | ``ACS_HLINE`` | horizontal line |
  957. +------------------+------------------------------------------+
  958. | ``ACS_LANTERN`` | lantern symbol |
  959. +------------------+------------------------------------------+
  960. | ``ACS_LARROW`` | left arrow |
  961. +------------------+------------------------------------------+
  962. | ``ACS_LEQUAL`` | less-than-or-equal-to |
  963. +------------------+------------------------------------------+
  964. | ``ACS_LLCORNER`` | lower left-hand corner |
  965. +------------------+------------------------------------------+
  966. | ``ACS_LRCORNER`` | lower right-hand corner |
  967. +------------------+------------------------------------------+
  968. | ``ACS_LTEE`` | left tee |
  969. +------------------+------------------------------------------+
  970. | ``ACS_NEQUAL`` | not-equal sign |
  971. +------------------+------------------------------------------+
  972. | ``ACS_PI`` | letter pi |
  973. +------------------+------------------------------------------+
  974. | ``ACS_PLMINUS`` | plus-or-minus sign |
  975. +------------------+------------------------------------------+
  976. | ``ACS_PLUS`` | big plus sign |
  977. +------------------+------------------------------------------+
  978. | ``ACS_RARROW`` | right arrow |
  979. +------------------+------------------------------------------+
  980. | ``ACS_RTEE`` | right tee |
  981. +------------------+------------------------------------------+
  982. | ``ACS_S1`` | scan line 1 |
  983. +------------------+------------------------------------------+
  984. | ``ACS_S3`` | scan line 3 |
  985. +------------------+------------------------------------------+
  986. | ``ACS_S7`` | scan line 7 |
  987. +------------------+------------------------------------------+
  988. | ``ACS_S9`` | scan line 9 |
  989. +------------------+------------------------------------------+
  990. | ``ACS_SBBS`` | alternate name for lower right corner |
  991. +------------------+------------------------------------------+
  992. | ``ACS_SBSB`` | alternate name for vertical line |
  993. +------------------+------------------------------------------+
  994. | ``ACS_SBSS`` | alternate name for right tee |
  995. +------------------+------------------------------------------+
  996. | ``ACS_SSBB`` | alternate name for lower left corner |
  997. +------------------+------------------------------------------+
  998. | ``ACS_SSBS`` | alternate name for bottom tee |
  999. +------------------+------------------------------------------+
  1000. | ``ACS_SSSB`` | alternate name for left tee |
  1001. +------------------+------------------------------------------+
  1002. | ``ACS_SSSS`` | alternate name for crossover or big plus |
  1003. +------------------+------------------------------------------+
  1004. | ``ACS_STERLING`` | pound sterling |
  1005. +------------------+------------------------------------------+
  1006. | ``ACS_TTEE`` | top tee |
  1007. +------------------+------------------------------------------+
  1008. | ``ACS_UARROW`` | up arrow |
  1009. +------------------+------------------------------------------+
  1010. | ``ACS_ULCORNER`` | upper left corner |
  1011. +------------------+------------------------------------------+
  1012. | ``ACS_URCORNER`` | upper right corner |
  1013. +------------------+------------------------------------------+
  1014. | ``ACS_VLINE`` | vertical line |
  1015. +------------------+------------------------------------------+
  1016. The following table lists the predefined colors:
  1017. +-------------------+----------------------------+
  1018. | Constant | Color |
  1019. +===================+============================+
  1020. | ``COLOR_BLACK`` | Black |
  1021. +-------------------+----------------------------+
  1022. | ``COLOR_BLUE`` | Blue |
  1023. +-------------------+----------------------------+
  1024. | ``COLOR_CYAN`` | Cyan (light greenish blue) |
  1025. +-------------------+----------------------------+
  1026. | ``COLOR_GREEN`` | Green |
  1027. +-------------------+----------------------------+
  1028. | ``COLOR_MAGENTA`` | Magenta (purplish red) |
  1029. +-------------------+----------------------------+
  1030. | ``COLOR_RED`` | Red |
  1031. +-------------------+----------------------------+
  1032. | ``COLOR_WHITE`` | White |
  1033. +-------------------+----------------------------+
  1034. | ``COLOR_YELLOW`` | Yellow |
  1035. +-------------------+----------------------------+
  1036. :mod:`curses.textpad` --- Text input widget for curses programs
  1037. ===============================================================
  1038. .. module:: curses.textpad
  1039. :synopsis: Emacs-like input editing in a curses window.
  1040. .. moduleauthor:: Eric Raymond <esr@thyrsus.com>
  1041. .. sectionauthor:: Eric Raymond <esr@thyrsus.com>
  1042. .. versionadded:: 1.6
  1043. The :mod:`curses.textpad` module provides a :class:`Textbox` class that handles
  1044. elementary text editing in a curses window, supporting a set of keybindings
  1045. resembling those of Emacs (thus, also of Netscape Navigator, BBedit 6.x,
  1046. FrameMaker, and many other programs). The module also provides a
  1047. rectangle-drawing function useful for framing text boxes or for other purposes.
  1048. The module :mod:`curses.textpad` defines the following function:
  1049. .. function:: rectangle(win, uly, ulx, lry, lrx)
  1050. Draw a rectangle. The first argument must be a window object; the remaining
  1051. arguments are coordinates relative to that window. The second and third
  1052. arguments are the y and x coordinates of the upper left hand corner of the
  1053. rectangle to be drawn; the fourth and fifth arguments are the y and x
  1054. coordinates of the lower right hand corner. The rectangle will be drawn using
  1055. VT100/IBM PC forms characters on terminals that make this possible (including
  1056. xterm and most other software terminal emulators). Otherwise it will be drawn
  1057. with ASCII dashes, vertical bars, and plus signs.
  1058. .. _curses-textpad-objects:
  1059. Textbox objects
  1060. ---------------
  1061. You can instantiate a :class:`Textbox` object as follows:
  1062. .. class:: Textbox(win)
  1063. Return a textbox widget object. The *win* argument should be a curses
  1064. :class:`WindowObject` in which the textbox is to be contained. The edit cursor
  1065. of the textbox is initially located at the upper left hand corner of the
  1066. containing window, with coordinates ``(0, 0)``. The instance's
  1067. :attr:`stripspaces` flag is initially on.
  1068. :class:`Textbox` objects have the following methods:
  1069. .. method:: edit([validator])
  1070. This is the entry point you will normally use. It accepts editing
  1071. keystrokes until one of the termination keystrokes is entered. If
  1072. *validator* is supplied, it must be a function. It will be called for
  1073. each keystroke entered with the keystroke as a parameter; command dispatch
  1074. is done on the result. This method returns the window contents as a
  1075. string; whether blanks in the window are included is affected by the
  1076. :attr:`stripspaces` member.
  1077. .. method:: do_command(ch)
  1078. Process a single command keystroke. Here are the supported special
  1079. keystrokes:
  1080. +------------------+-------------------------------------------+
  1081. | Keystroke | Action |
  1082. +==================+===========================================+
  1083. | :kbd:`Control-A` | Go to left edge of window. |
  1084. +------------------+-------------------------------------------+
  1085. | :kbd:`Control-B` | Cursor left, wrapping to previous line if |
  1086. | | appropriate. |
  1087. +------------------+-------------------------------------------+
  1088. | :kbd:`Control-D` | Delete character under cursor. |
  1089. +------------------+-------------------------------------------+
  1090. | :kbd:`Control-E` | Go to right edge (stripspaces off) or end |
  1091. | | of line (stripspaces on). |
  1092. +------------------+-------------------------------------------+
  1093. | :kbd:`Control-F` | Cursor right, wrapping to next line when |
  1094. | | appropriate. |
  1095. +------------------+-------------------------------------------+
  1096. | :kbd:`Control-G` | Terminate, returning the window contents. |
  1097. +------------------+-------------------------------------------+
  1098. | :kbd:`Control-H` | Delete character backward. |
  1099. +------------------+-------------------------------------------+
  1100. | :kbd:`Control-J` | Terminate if the window is 1 line, |
  1101. | | otherwise insert newline. |
  1102. +------------------+-------------------------------------------+
  1103. | :kbd:`Control-K` | If line is blank, delete it, otherwise |
  1104. | | clear to end of line. |
  1105. +------------------+-------------------------------------------+
  1106. | :kbd:`Control-L` | Refresh screen. |
  1107. +------------------+-------------------------------------------+
  1108. | :kbd:`Control-N` | Cursor down; move down one line. |
  1109. +------------------+-------------------------------------------+
  1110. | :kbd:`Control-O` | Insert a blank line at cursor location. |
  1111. +------------------+-------------------------------------------+
  1112. | :kbd:`Control-P` | Cursor up; move up one line. |
  1113. +------------------+-------------------------------------------+
  1114. Move operations do nothing if the cursor is at an edge where the movement
  1115. is not possible. The following synonyms are supported where possible:
  1116. +------------------------+------------------+
  1117. | Constant | Keystroke |
  1118. +========================+==================+
  1119. | :const:`KEY_LEFT` | :kbd:`Control-B` |
  1120. +------------------------+------------------+
  1121. | :const:`KEY_RIGHT` | :kbd:`Control-F` |
  1122. +------------------------+------------------+
  1123. | :const:`KEY_UP` | :kbd:`Control-P` |
  1124. +------------------------+------------------+
  1125. | :const:`KEY_DOWN` | :kbd:`Control-N` |
  1126. +------------------------+------------------+
  1127. | :const:`KEY_BACKSPACE` | :kbd:`Control-h` |
  1128. +------------------------+------------------+
  1129. All other keystrokes are treated as a command to insert the given
  1130. character and move right (with line wrapping).
  1131. .. method:: gather()
  1132. This method returns the window contents as a string; whether blanks in the
  1133. window are included is affected by the :attr:`stripspaces` member.
  1134. .. attribute:: stripspaces
  1135. This data member is a flag which controls the interpretation of blanks in
  1136. the window. When it is on, trailing blanks on each line are ignored; any
  1137. cursor motion that would land the cursor on a trailing blank goes to the
  1138. end of that line instead, and trailing blanks are stripped when the window
  1139. contents are gathered.
  1140. :mod:`curses.wrapper` --- Terminal handler for curses programs
  1141. ==============================================================
  1142. .. module:: curses.wrapper
  1143. :synopsis: Terminal configuration wrapper for curses programs.
  1144. .. moduleauthor:: Eric Raymond <esr@thyrsus.com>
  1145. .. sectionauthor:: Eric Raymond <esr@thyrsus.com>
  1146. .. versionadded:: 1.6
  1147. This module supplies one function, :func:`wrapper`, which runs another function
  1148. which should be the rest of your curses-using application. If the application
  1149. raises an exception, :func:`wrapper` will restore the terminal to a sane state
  1150. before re-raising the exception and generating a traceback.
  1151. .. function:: wrapper(func, ...)
  1152. Wrapper function that initializes curses and calls another function, *func*,
  1153. restoring normal keyboard/screen behavior on error. The callable object *func*
  1154. is then passed the main window 'stdscr' as its first argument, followed by any
  1155. other arguments passed to :func:`wrapper`.
  1156. Before calling the hook function, :func:`wrapper` turns on cbreak mode, turns
  1157. off echo, enables the terminal keypad, and initializes colors if the terminal
  1158. has color support. On exit (whether normally or by exception) it restores
  1159. cooked mode, turns on echo, and disables the terminal keypad.