1:mod:`curses` --- Terminal handling for character-cell displays
2===============================================================
3
4.. module:: curses
5   :synopsis: An interface to the curses library, providing portable terminal
6              handling.
7   :platform: Unix
8.. sectionauthor:: Moshe Zadka <[email protected]>
9.. sectionauthor:: Eric Raymond <[email protected]>
10
11.. versionchanged:: 1.6
12   Added support for the ``ncurses`` library and converted to a package.
13
14The :mod:`curses` module provides an interface to the curses library, the
15de-facto standard for portable advanced terminal handling.
16
17While curses is most widely used in the Unix environment, versions are available
18for DOS, OS/2, and possibly other systems as well.  This extension module is
19designed to match the API of ncurses, an open-source curses library hosted on
20Linux and the BSD variants of Unix.
21
22.. note::
23
24   Since version 5.4, the ncurses library decides how to interpret non-ASCII data
25   using the ``nl_langinfo`` function.  That means that you have to call
26   :func:`locale.setlocale` in the application and encode Unicode strings
27   using one of the system's available encodings.  This example uses the
28   system's default encoding::
29
30      import locale
31      locale.setlocale(locale.LC_ALL, '')
32      code = locale.getpreferredencoding()
33
34   Then use *code* as the encoding for :meth:`str.encode` calls.
35
36.. seealso::
37
38   Module :mod:`curses.ascii`
39      Utilities for working with ASCII characters, regardless of your locale settings.
40
41   Module :mod:`curses.panel`
42      A panel stack extension that adds depth to  curses windows.
43
44   Module :mod:`curses.textpad`
45      Editable text widget for curses supporting  :program:`Emacs`\ -like bindings.
46
47   :ref:`curses-howto`
48      Tutorial material on using curses with Python, by Andrew Kuchling and Eric
49      Raymond.
50
51   The :source:`Demo/curses/` directory in the Python source distribution contains
52   some example programs using the curses bindings provided by this module.
53
54
55.. _curses-functions:
56
57Functions
58---------
59
60The module :mod:`curses` defines the following exception:
61
62
63.. exception:: error
64
65   Exception raised when a curses library function returns an error.
66
67.. note::
68
69   Whenever *x* or *y* arguments to a function or a method are optional, they
70   default to the current cursor location. Whenever *attr* is optional, it defaults
71   to :const:`A_NORMAL`.
72
73The module :mod:`curses` defines the following functions:
74
75
76.. function:: baudrate()
77
78   Return the output speed of the terminal in bits per second.  On software
79   terminal emulators it will have a fixed high value. Included for historical
80   reasons; in former times, it was used to  write output loops for time delays and
81   occasionally to change interfaces depending on the line speed.
82
83
84.. function:: beep()
85
86   Emit a short attention sound.
87
88
89.. function:: can_change_color()
90
91   Return ``True`` or ``False``, depending on whether the programmer can change the colors
92   displayed by the terminal.
93
94
95.. function:: cbreak()
96
97   Enter cbreak mode.  In cbreak mode (sometimes called "rare" mode) normal tty
98   line buffering is turned off and characters are available to be read one by one.
99   However, unlike raw mode, special characters (interrupt, quit, suspend, and flow
100   control) retain their effects on the tty driver and calling program.  Calling
101   first :func:`raw` then :func:`cbreak` leaves the terminal in cbreak mode.
102
103
104.. function:: color_content(color_number)
105
106   Return the intensity of the red, green, and blue (RGB) components in the color
107   *color_number*, which must be between ``0`` and :const:`COLORS`.  A 3-tuple is
108   returned, containing the R,G,B values for the given color, which will be between
109   ``0`` (no component) and ``1000`` (maximum amount of component).
110
111
112.. function:: color_pair(color_number)
113
114   Return the attribute value for displaying text in the specified color.  This
115   attribute value can be combined with :const:`A_STANDOUT`, :const:`A_REVERSE`,
116   and the other :const:`A_\*` attributes.  :func:`pair_number` is the counterpart
117   to this function.
118
119
120.. function:: curs_set(visibility)
121
122   Set the cursor state.  *visibility* can be set to 0, 1, or 2, for invisible,
123   normal, or very visible.  If the terminal supports the visibility requested, the
124   previous cursor state is returned; otherwise, an exception is raised.  On many
125   terminals, the "visible" mode is an underline cursor and the "very visible" mode
126   is a block cursor.
127
128
129.. function:: def_prog_mode()
130
131   Save the current terminal mode as the "program" mode, the mode when the running
132   program is using curses.  (Its counterpart is the "shell" mode, for when the
133   program is not in curses.)  Subsequent calls to :func:`reset_prog_mode` will
134   restore this mode.
135
136
137.. function:: def_shell_mode()
138
139   Save the current terminal mode as the "shell" mode, the mode when the running
140   program is not using curses.  (Its counterpart is the "program" mode, when the
141   program is using curses capabilities.) Subsequent calls to
142   :func:`reset_shell_mode` will restore this mode.
143
144
145.. function:: delay_output(ms)
146
147   Insert an *ms* millisecond pause in output.
148
149
150.. function:: doupdate()
151
152   Update the physical screen.  The curses library keeps two data structures, one
153   representing the current physical screen contents and a virtual screen
154   representing the desired next state.  The :func:`doupdate` ground updates the
155   physical screen to match the virtual screen.
156
157   The virtual screen may be updated by a :meth:`noutrefresh` call after write
158   operations such as :meth:`addstr` have been performed on a window.  The normal
159   :meth:`refresh` call is simply :meth:`noutrefresh` followed by :func:`doupdate`;
160   if you have to update multiple windows, you can speed performance and perhaps
161   reduce screen flicker by issuing :meth:`noutrefresh` calls on all windows,
162   followed by a single :func:`doupdate`.
163
164
165.. function:: echo()
166
167   Enter echo mode.  In echo mode, each character input is echoed to the screen as
168   it is entered.
169
170
171.. function:: endwin()
172
173   De-initialize the library, and return terminal to normal status.
174
175
176.. function:: erasechar()
177
178   Return the user's current erase character.  Under Unix operating systems this
179   is a property of the controlling tty of the curses program, and is not set by
180   the curses library itself.
181
182
183.. function:: filter()
184
185   The :func:`.filter` routine, if used, must be called before :func:`initscr` is
186   called.  The effect is that, during those calls, :envvar:`LINES` is set to 1; the
187   capabilities clear, cup, cud, cud1, cuu1, cuu, vpa are disabled; and the home
188   string is set to the value of cr. The effect is that the cursor is confined to
189   the current line, and so are screen updates.  This may be used for enabling
190   character-at-a-time  line editing without touching the rest of the screen.
191
192
193.. function:: flash()
194
195   Flash the screen.  That is, change it to reverse-video and then change it back
196   in a short interval.  Some people prefer such as 'visible bell' to the audible
197   attention signal produced by :func:`beep`.
198
199
200.. function:: flushinp()
201
202   Flush all input buffers.  This throws away any  typeahead  that  has been typed
203   by the user and has not yet been processed by the program.
204
205
206.. function:: getmouse()
207
208   After :meth:`getch` returns :const:`KEY_MOUSE` to signal a mouse event, this
209   method should be call to retrieve the queued mouse event, represented as a
210   5-tuple ``(id, x, y, z, bstate)``. *id* is an ID value used to distinguish
211   multiple devices, and *x*, *y*, *z* are the event's coordinates.  (*z* is
212   currently unused.)  *bstate* is an integer value whose bits will be set to
213   indicate the type of event, and will be the bitwise OR of one or more of the
214   following constants, where *n* is the button number from 1 to 4:
215   :const:`BUTTONn_PRESSED`, :const:`BUTTONn_RELEASED`, :const:`BUTTONn_CLICKED`,
216   :const:`BUTTONn_DOUBLE_CLICKED`, :const:`BUTTONn_TRIPLE_CLICKED`,
217   :const:`BUTTON_SHIFT`, :const:`BUTTON_CTRL`, :const:`BUTTON_ALT`.
218
219
220.. function:: getsyx()
221
222   Return the current coordinates of the virtual screen cursor in y and x.  If
223   leaveok is currently true, then -1,-1 is returned.
224
225
226.. function:: getwin(file)
227
228   Read window related data stored in the file by an earlier :func:`putwin` call.
229   The routine then creates and initializes a new window using that data, returning
230   the new window object.
231
232
233.. function:: has_colors()
234
235   Return ``True`` if the terminal can display colors; otherwise, return ``False``.
236
237
238.. function:: has_ic()
239
240   Return ``True`` if the terminal has insert- and delete-character capabilities.
241   This function is included for historical reasons only, as all modern software
242   terminal emulators have such capabilities.
243
244
245.. function:: has_il()
246
247   Return ``True`` if the terminal has insert- and delete-line capabilities, or can
248   simulate  them  using scrolling regions. This function is included for
249   historical reasons only, as all modern software terminal emulators have such
250   capabilities.
251
252
253.. function:: has_key(ch)
254
255   Take a key value *ch*, and return ``True`` if the current terminal type recognizes
256   a key with that value.
257
258
259.. function:: halfdelay(tenths)
260
261   Used for half-delay mode, which is similar to cbreak mode in that characters
262   typed by the user are immediately available to the program. However, after
263   blocking for *tenths* tenths of seconds, an exception is raised if nothing has
264   been typed.  The value of *tenths* must be a number between ``1`` and ``255``.  Use
265   :func:`nocbreak` to leave half-delay mode.
266
267
268.. function:: init_color(color_number, r, g, b)
269
270   Change the definition of a color, taking the number of the color to be changed
271   followed by three RGB values (for the amounts of red, green, and blue
272   components).  The value of *color_number* must be between ``0`` and
273   :const:`COLORS`.  Each of *r*, *g*, *b*, must be a value between ``0`` and
274   ``1000``.  When :func:`init_color` is used, all occurrences of that color on the
275   screen immediately change to the new definition.  This function is a no-op on
276   most terminals; it is active only if :func:`can_change_color` returns ``1``.
277
278
279.. function:: init_pair(pair_number, fg, bg)
280
281   Change the definition of a color-pair.  It takes three arguments: the number of
282   the color-pair to be changed, the foreground color number, and the background
283   color number.  The value of *pair_number* must be between ``1`` and
284   ``COLOR_PAIRS - 1`` (the ``0`` color pair is wired to white on black and cannot
285   be changed).  The value of *fg* and *bg* arguments must be between ``0`` and
286   :const:`COLORS`.  If the color-pair was previously initialized, the screen is
287   refreshed and all occurrences of that color-pair are changed to the new
288   definition.
289
290
291.. function:: initscr()
292
293   Initialize the library. Return a :class:`WindowObject` which represents the
294   whole screen.
295
296   .. note::
297
298      If there is an error opening the terminal, the underlying curses library may
299      cause the interpreter to exit.
300
301
302.. function:: is_term_resized(nlines, ncols)
303
304   Return ``True`` if :func:`resize_term` would modify the window structure,
305   ``False`` otherwise.
306
307
308.. function:: isendwin()
309
310   Return ``True`` if :func:`endwin` has been called (that is, the  curses library has
311   been deinitialized).
312
313
314.. function:: keyname(k)
315
316   Return the name of the key numbered *k*.  The name of a key generating printable
317   ASCII character is the key's character.  The name of a control-key combination
318   is a two-character string consisting of a caret followed by the corresponding
319   printable ASCII character.  The name of an alt-key combination (128--255) is a
320   string consisting of the prefix 'M-' followed by the name of the corresponding
321   ASCII character.
322
323
324.. function:: killchar()
325
326   Return the user's current line kill character. Under Unix operating systems
327   this is a property of the controlling tty of the curses program, and is not set
328   by the curses library itself.
329
330
331.. function:: longname()
332
333   Return a string containing the terminfo long name field describing the current
334   terminal.  The maximum length of a verbose description is 128 characters.  It is
335   defined only after the call to :func:`initscr`.
336
337
338.. function:: meta(yes)
339
340   If *yes* is 1, allow 8-bit characters to be input. If *yes* is 0,  allow only
341   7-bit chars.
342
343
344.. function:: mouseinterval(interval)
345
346   Set the maximum time in milliseconds that can elapse between press and release
347   events in order for them to be recognized as a click, and return the previous
348   interval value.  The default value is 200 msec, or one fifth of a second.
349
350
351.. function:: mousemask(mousemask)
352
353   Set the mouse events to be reported, and return a tuple ``(availmask,
354   oldmask)``.   *availmask* indicates which of the specified mouse events can be
355   reported; on complete failure it returns 0.  *oldmask* is the previous value of
356   the given window's mouse event mask.  If this function is never called, no mouse
357   events are ever reported.
358
359
360.. function:: napms(ms)
361
362   Sleep for *ms* milliseconds.
363
364
365.. function:: newpad(nlines, ncols)
366
367   Create and return a pointer to a new pad data structure with the given number
368   of lines and columns.  A pad is returned as a window object.
369
370   A pad is like a window, except that it is not restricted by the screen size, and
371   is not necessarily associated with a particular part of the screen.  Pads can be
372   used when a large window is needed, and only a part of the window will be on the
373   screen at one time.  Automatic refreshes of pads (such as from scrolling or
374   echoing of input) do not occur.  The :meth:`refresh` and :meth:`noutrefresh`
375   methods of a pad require 6 arguments to specify the part of the pad to be
376   displayed and the location on the screen to be used for the display. The
377   arguments are *pminrow*, *pmincol*, *sminrow*, *smincol*, *smaxrow*, *smaxcol*; the *p*
378   arguments refer to the upper left corner of the pad region to be displayed and
379   the *s* arguments define a clipping box on the screen within which the pad region
380   is to be displayed.
381
382
383.. function:: newwin(nlines, ncols)
384              newwin(nlines, ncols, begin_y, begin_x)
385
386   Return a new window, whose left-upper corner is at  ``(begin_y, begin_x)``, and
387   whose height/width is  *nlines*/*ncols*.
388
389   By default, the window will extend from the  specified position to the lower
390   right corner of the screen.
391
392
393.. function:: nl()
394
395   Enter newline mode.  This mode translates the return key into newline on input,
396   and translates newline into return and line-feed on output. Newline mode is
397   initially on.
398
399
400.. function:: nocbreak()
401
402   Leave cbreak mode.  Return to normal "cooked" mode with line buffering.
403
404
405.. function:: noecho()
406
407   Leave echo mode.  Echoing of input characters is turned off.
408
409
410.. function:: nonl()
411
412   Leave newline mode.  Disable translation of return into newline on input, and
413   disable low-level translation of newline into newline/return on output (but this
414   does not change the behavior of ``addch('\n')``, which always does the
415   equivalent of return and line feed on the virtual screen).  With translation
416   off, curses can sometimes speed up vertical motion a little; also, it will be
417   able to detect the return key on input.
418
419
420.. function:: noqiflush()
421
422   When the :func:`noqiflush` routine is used, normal flush of input and output queues
423   associated with the INTR, QUIT and SUSP characters will not be done.  You may
424   want to call :func:`noqiflush` in a signal handler if you want output to
425   continue as though the interrupt had not occurred, after the handler exits.
426
427
428.. function:: noraw()
429
430   Leave raw mode. Return to normal "cooked" mode with line buffering.
431
432
433.. function:: pair_content(pair_number)
434
435   Return a tuple ``(fg, bg)`` containing the colors for the requested color pair.
436   The value of *pair_number* must be between ``1`` and ``COLOR_PAIRS - 1``.
437
438
439.. function:: pair_number(attr)
440
441   Return the number of the color-pair set by the attribute value *attr*.
442   :func:`color_pair` is the counterpart to this function.
443
444
445.. function:: putp(string)
446
447   Equivalent to ``tputs(str, 1, putchar)``; emit the value of a specified
448   terminfo capability for the current terminal.  Note that the output of :func:`putp`
449   always goes to standard output.
450
451
452.. function:: qiflush( [flag] )
453
454   If *flag* is ``False``, the effect is the same as calling :func:`noqiflush`. If
455   *flag* is ``True``, or no argument is provided, the queues will be flushed when
456   these control characters are read.
457
458
459.. function:: raw()
460
461   Enter raw mode.  In raw mode, normal line buffering and  processing of
462   interrupt, quit, suspend, and flow control keys are turned off; characters are
463   presented to curses input functions one by one.
464
465
466.. function:: reset_prog_mode()
467
468   Restore the  terminal  to "program" mode, as previously saved  by
469   :func:`def_prog_mode`.
470
471
472.. function:: reset_shell_mode()
473
474   Restore the  terminal  to "shell" mode, as previously saved  by
475   :func:`def_shell_mode`.
476
477
478.. function:: resetty()
479
480   Restore the state of the terminal modes to what it was at the last call to
481   :func:`savetty`.
482
483
484.. function:: resize_term(nlines, ncols)
485
486   Backend function used by :func:`resizeterm`, performing most of the work;
487   when resizing the windows, :func:`resize_term` blank-fills the areas that are
488   extended.  The calling application should fill in these areas with
489   appropriate data.  The :func:`resize_term` function attempts to resize all
490   windows.  However, due to the calling convention of pads, it is not possible
491   to resize these without additional interaction with the application.
492
493
494.. function:: resizeterm(nlines, ncols)
495
496   Resize the standard and current windows to the specified dimensions, and
497   adjusts other bookkeeping data used by the curses library that record the
498   window dimensions (in particular the SIGWINCH handler).
499
500
501.. function:: savetty()
502
503   Save the current state of the terminal modes in a buffer, usable by
504   :func:`resetty`.
505
506
507.. function:: setsyx(y, x)
508
509   Set the virtual screen cursor to *y*, *x*. If *y* and *x* are both -1, then
510   leaveok is set.
511
512
513.. function:: setupterm([termstr, fd])
514
515   Initialize the terminal.  *termstr* is a string giving the terminal name; if
516   omitted, the value of the :envvar:`TERM` environment variable will be used.  *fd* is the
517   file descriptor to which any initialization sequences will be sent; if not
518   supplied, the file descriptor for ``sys.stdout`` will be used.
519
520
521.. function:: start_color()
522
523   Must be called if the programmer wants to use colors, and before any other color
524   manipulation routine is called.  It is good practice to call this routine right
525   after :func:`initscr`.
526
527   :func:`start_color` initializes eight basic colors (black, red,  green, yellow,
528   blue, magenta, cyan, and white), and two global variables in the :mod:`curses`
529   module, :const:`COLORS` and :const:`COLOR_PAIRS`, containing the maximum number
530   of colors and color-pairs the terminal can support.  It also restores the colors
531   on the terminal to the values they had when the terminal was just turned on.
532
533
534.. function:: termattrs()
535
536   Return a logical OR of all video attributes supported by the terminal.  This
537   information is useful when a curses program needs complete control over the
538   appearance of the screen.
539
540
541.. function:: termname()
542
543   Return the value of the environment variable :envvar:`TERM`, truncated to 14 characters.
544
545
546.. function:: tigetflag(capname)
547
548   Return the value of the Boolean capability corresponding to the terminfo
549   capability name *capname*.  The value ``-1`` is returned if *capname* is not a
550   Boolean capability, or ``0`` if it is canceled or absent from the terminal
551   description.
552
553
554.. function:: tigetnum(capname)
555
556   Return the value of the numeric capability corresponding to the terminfo
557   capability name *capname*.  The value ``-2`` is returned if *capname* is not a
558   numeric capability, or ``-1`` if it is canceled or absent from the terminal
559   description.
560
561
562.. function:: tigetstr(capname)
563
564   Return the value of the string capability corresponding to the terminfo
565   capability name *capname*.  ``None`` is returned if *capname* is not a string
566   capability, or is canceled or absent from the terminal description.
567
568
569.. function:: tparm(str[,...])
570
571   Instantiate the string *str* with the supplied parameters, where *str* should
572   be a parameterized string obtained from the terminfo database.  E.g.
573   ``tparm(tigetstr("cup"), 5, 3)`` could result in ``'\033[6;4H'``, the exact
574   result depending on terminal type.
575
576
577.. function:: typeahead(fd)
578
579   Specify that the file descriptor *fd* be used for typeahead checking.  If *fd*
580   is ``-1``, then no typeahead checking is done.
581
582   The curses library does "line-breakout optimization" by looking for typeahead
583   periodically while updating the screen.  If input is found, and it is coming
584   from a tty, the current update is postponed until refresh or doupdate is called
585   again, allowing faster response to commands typed in advance. This function
586   allows specifying a different file descriptor for typeahead checking.
587
588
589.. function:: unctrl(ch)
590
591   Return a string which is a printable representation of the character *ch*.
592   Control characters are displayed as a caret followed by the character, for
593   example as ``^C``. Printing characters are left as they are.
594
595
596.. function:: ungetch(ch)
597
598   Push *ch* so the next :meth:`getch` will return it.
599
600   .. note::
601
602      Only one *ch* can be pushed before :meth:`getch` is called.
603
604
605.. function:: ungetmouse(id, x, y, z, bstate)
606
607   Push a :const:`KEY_MOUSE` event onto the input queue, associating the given
608   state data with it.
609
610
611.. function:: use_env(flag)
612
613   If used, this function should be called before :func:`initscr` or newterm are
614   called.  When *flag* is ``False``, the values of lines and columns specified in the
615   terminfo database will be used, even if environment variables :envvar:`LINES`
616   and :envvar:`COLUMNS` (used by default) are set, or if curses is running in a
617   window (in which case default behavior would be to use the window size if
618   :envvar:`LINES` and :envvar:`COLUMNS` are not set).
619
620
621.. function:: use_default_colors()
622
623   Allow use of default values for colors on terminals supporting this feature. Use
624   this to support transparency in your application.  The default color is assigned
625   to the color number -1. After calling this function,  ``init_pair(x,
626   curses.COLOR_RED, -1)`` initializes, for instance, color pair *x* to a red
627   foreground color on the default background.
628
629
630.. function:: wrapper(func, ...)
631
632   Initialize curses and call another callable object, *func*, which should be the
633   rest of your curses-using application.  If the application raises an exception,
634   this function will restore the terminal to a sane state before re-raising the
635   exception and generating a traceback.  The callable object *func* is then passed
636   the main window 'stdscr' as its first argument, followed by any other arguments
637   passed to :func:`wrapper`.  Before calling *func*, :func:`wrapper` turns on
638   cbreak mode, turns off echo, enables the terminal keypad, and initializes colors
639   if the terminal has color support.  On exit (whether normally or by exception)
640   it restores cooked mode, turns on echo, and disables the terminal keypad.
641
642
643.. _curses-window-objects:
644
645Window Objects
646--------------
647
648Window objects, as returned by :func:`initscr` and :func:`newwin` above, have
649the following methods:
650
651
652.. method:: window.addch(ch[, attr])
653            window.addch(y, x, ch[, attr])
654
655   .. note::
656
657      A *character* means a C character (an ASCII code), rather than a Python
658      character (a string of length 1). (This note is true whenever the
659      documentation mentions a character.) The built-in :func:`ord` is handy for
660      conveying strings to codes.
661
662   Paint character *ch* at ``(y, x)`` with attributes *attr*, overwriting any
663   character previously painter at that location.  By default, the character
664   position and attributes are the current settings for the window object.
665
666   .. note::
667
668      Writing outside the window, subwindow, or pad raises a :exc:`curses.error`.
669      Attempting to write to the lower right corner of a window, subwindow,
670      or pad will cause an exception to be raised after the character is printed.
671
672
673.. method:: window.addnstr(str, n[, attr])
674            window.addnstr(y, x, str, n[, attr])
675
676   Paint at most *n* characters of the  string *str* at ``(y, x)`` with attributes
677   *attr*, overwriting anything previously on the display.
678
679
680.. method:: window.addstr(str[, attr])
681            window.addstr(y, x, str[, attr])
682
683   Paint the string *str* at ``(y, x)`` with attributes *attr*, overwriting
684   anything previously on the display.
685
686   .. note::
687
688      Writing outside the window, subwindow, or pad raises :exc:`curses.error`.
689      Attempting to write to the lower right corner of a window, subwindow,
690      or pad will cause an exception to be raised after the string is printed.
691
692
693.. method:: window.attroff(attr)
694
695   Remove attribute *attr* from the "background" set applied to all writes to the
696   current window.
697
698
699.. method:: window.attron(attr)
700
701   Add attribute *attr* from the "background" set applied to all writes to the
702   current window.
703
704
705.. method:: window.attrset(attr)
706
707   Set the "background" set of attributes to *attr*.  This set is initially 0 (no
708   attributes).
709
710
711.. method:: window.bkgd(ch[, attr])
712
713   Set the background property of the window to the character *ch*, with
714   attributes *attr*.  The change is then applied to every character position in
715   that window:
716
717   * The attribute of every character in the window  is changed to the new
718     background attribute.
719
720   * Wherever  the  former background character appears, it is changed to the new
721     background character.
722
723
724.. method:: window.bkgdset(ch[, attr])
725
726   Set the window's background.  A window's background consists of a character and
727   any combination of attributes.  The attribute part of the background is combined
728   (OR'ed) with all non-blank characters that are written into the window.  Both
729   the character and attribute parts of the background are combined with the blank
730   characters.  The background becomes a property of the character and moves with
731   the character through any scrolling and insert/delete line/character operations.
732
733
734.. method:: window.border([ls[, rs[, ts[, bs[, tl[, tr[, bl[, br]]]]]]]])
735
736   Draw a border around the edges of the window. Each parameter specifies  the
737   character to use for a specific part of the border; see the table below for more
738   details.  The characters can be specified as integers or as one-character
739   strings.
740
741   .. note::
742
743      A ``0`` value for any parameter will cause the default character to be used for
744      that parameter.  Keyword parameters can *not* be used.  The defaults are listed
745      in this table:
746
747   +-----------+---------------------+-----------------------+
748   | Parameter | Description         | Default value         |
749   +===========+=====================+=======================+
750   | *ls*      | Left side           | :const:`ACS_VLINE`    |
751   +-----------+---------------------+-----------------------+
752   | *rs*      | Right side          | :const:`ACS_VLINE`    |
753   +-----------+---------------------+-----------------------+
754   | *ts*      | Top                 | :const:`ACS_HLINE`    |
755   +-----------+---------------------+-----------------------+
756   | *bs*      | Bottom              | :const:`ACS_HLINE`    |
757   +-----------+---------------------+-----------------------+
758   | *tl*      | Upper-left corner   | :const:`ACS_ULCORNER` |
759   +-----------+---------------------+-----------------------+
760   | *tr*      | Upper-right corner  | :const:`ACS_URCORNER` |
761   +-----------+---------------------+-----------------------+
762   | *bl*      | Bottom-left corner  | :const:`ACS_LLCORNER` |
763   +-----------+---------------------+-----------------------+
764   | *br*      | Bottom-right corner | :const:`ACS_LRCORNER` |
765   +-----------+---------------------+-----------------------+
766
767
768.. method:: window.box([vertch, horch])
769
770   Similar to :meth:`border`, but both *ls* and *rs* are *vertch* and both *ts* and
771   *bs* are *horch*.  The default corner characters are always used by this function.
772
773
774.. method:: window.chgat(attr)
775            window.chgat(num, attr)
776            window.chgat(y, x, attr)
777            window.chgat(y, x, num, attr)
778
779   Set the attributes of *num* characters at the current cursor position, or at
780   position ``(y, x)`` if supplied. If *num* is not given or is ``-1``,
781   the attribute will be set on all the characters to the end of the line.  This
782   function moves cursor to position ``(y, x)`` if supplied. The changed line
783   will be touched using the :meth:`touchline` method so that the contents will
784   be redisplayed by the next window refresh.
785
786
787.. method:: window.clear()
788
789   Like :meth:`erase`, but also cause the whole window to be repainted upon next
790   call to :meth:`refresh`.
791
792
793.. method:: window.clearok(yes)
794
795   If *yes* is 1, the next call to :meth:`refresh` will clear the window
796   completely.
797
798
799.. method:: window.clrtobot()
800
801   Erase from cursor to the end of the window: all lines below the cursor are
802   deleted, and then the equivalent of :meth:`clrtoeol` is performed.
803
804
805.. method:: window.clrtoeol()
806
807   Erase from cursor to the end of the line.
808
809
810.. method:: window.cursyncup()
811
812   Update the current cursor position of all the ancestors of the window to
813   reflect the current cursor position of the window.
814
815
816.. method:: window.delch([y, x])
817
818   Delete any character at ``(y, x)``.
819
820
821.. method:: window.deleteln()
822
823   Delete the line under the cursor. All following lines are moved up by one line.
824
825
826.. method:: window.derwin(begin_y, begin_x)
827            window.derwin(nlines, ncols, begin_y, begin_x)
828
829   An abbreviation for "derive window", :meth:`derwin` is the same as calling
830   :meth:`subwin`, except that *begin_y* and *begin_x* are relative to the origin
831   of the window, rather than relative to the entire screen.  Return a window
832   object for the derived window.
833
834
835.. method:: window.echochar(ch[, attr])
836
837   Add character *ch* with attribute *attr*, and immediately  call :meth:`refresh`
838   on the window.
839
840
841.. method:: window.enclose(y, x)
842
843   Test whether the given pair of screen-relative character-cell coordinates are
844   enclosed by the given window, returning ``True`` or ``False``.  It is useful for
845   determining what subset of the screen windows enclose the location of a mouse
846   event.
847
848
849.. method:: window.erase()
850
851   Clear the window.
852
853
854.. method:: window.getbegyx()
855
856   Return a tuple ``(y, x)`` of co-ordinates of upper-left corner.
857
858
859.. method:: window.getbkgd()
860
861   Return the given window's current background character/attribute pair.
862
863
864.. method:: window.getch([y, x])
865
866   Get a character. Note that the integer returned does *not* have to be in ASCII
867   range: function keys, keypad keys and so on return numbers higher than 256. In
868   no-delay mode, -1 is returned if there is no input, else :func:`getch` waits
869   until a key is pressed.
870
871
872.. method:: window.getkey([y, x])
873
874   Get a character, returning a string instead of an integer, as :meth:`getch`
875   does. Function keys, keypad keys and so on return a multibyte string containing
876   the key name.  In no-delay mode, an exception is raised if there is no input.
877
878
879.. method:: window.getmaxyx()
880
881   Return a tuple ``(y, x)`` of the height and width of the window.
882
883
884.. method:: window.getparyx()
885
886   Return the beginning coordinates of this window relative to its parent window
887   into two integer variables y and x.  Return ``-1, -1`` if this window has no
888   parent.
889
890
891.. method:: window.getstr([y, x])
892
893   Read a string from the user, with primitive line editing capacity.
894
895
896.. method:: window.getyx()
897
898   Return a tuple ``(y, x)`` of current cursor position  relative to the window's
899   upper-left corner.
900
901
902.. method:: window.hline(ch, n)
903            window.hline(y, x, ch, n)
904
905   Display a horizontal line starting at ``(y, x)`` with length *n* consisting of
906   the character *ch*.
907
908
909.. method:: window.idcok(flag)
910
911   If *flag* is ``False``, curses no longer considers using the hardware insert/delete
912   character feature of the terminal; if *flag* is ``True``, use of character insertion
913   and deletion is enabled.  When curses is first initialized, use of character
914   insert/delete is enabled by default.
915
916
917.. method:: window.idlok(yes)
918
919   If called with *yes* equal to 1, :mod:`curses` will try and use hardware line
920   editing facilities. Otherwise, line insertion/deletion are disabled.
921
922
923.. method:: window.immedok(flag)
924
925   If *flag* is ``True``, any change in the window image automatically causes the
926   window to be refreshed; you no longer have to call :meth:`refresh` yourself.
927   However, it may degrade performance considerably, due to repeated calls to
928   wrefresh.  This option is disabled by default.
929
930
931.. method:: window.inch([y, x])
932
933   Return the character at the given position in the window. The bottom 8 bits are
934   the character proper, and upper bits are the attributes.
935
936
937.. method:: window.insch(ch[, attr])
938            window.insch(y, x, ch[, attr])
939
940   Paint character *ch* at ``(y, x)`` with attributes *attr*, moving the line from
941   position *x* right by one character.
942
943
944.. method:: window.insdelln(nlines)
945
946   Insert *nlines* lines into the specified window above the current line.  The
947   *nlines* bottom lines are lost.  For negative *nlines*, delete *nlines* lines
948   starting with the one under the cursor, and move the remaining lines up.  The
949   bottom *nlines* lines are cleared.  The current cursor position remains the
950   same.
951
952
953.. method:: window.insertln()
954
955   Insert a blank line under the cursor. All following lines are moved down by one
956   line.
957
958
959.. method:: window.insnstr(str, n[, attr])
960            window.insnstr(y, x, str, n[, attr])
961
962   Insert a character string (as many characters as will fit on the line) before
963   the character under the cursor, up to *n* characters.   If *n* is zero or
964   negative, the entire string is inserted. All characters to the right of the
965   cursor are shifted right, with the rightmost characters on the line being lost.
966   The cursor position does not change (after moving to *y*, *x*, if specified).
967
968
969.. method:: window.insstr(str[, attr])
970            window.insstr(y, x, str[, attr])
971
972   Insert a character string (as many characters as will fit on the line) before
973   the character under the cursor.  All characters to the right of the cursor are
974   shifted right, with the rightmost characters on the line being lost.  The cursor
975   position does not change (after moving to *y*, *x*, if specified).
976
977
978.. method:: window.instr([n])
979            window.instr(y, x[, n])
980
981   Return a string of characters, extracted from the window starting at the
982   current cursor position, or at *y*, *x* if specified. Attributes are stripped
983   from the characters.  If *n* is specified, :meth:`instr` returns a string
984   at most *n* characters long (exclusive of the trailing NUL).
985
986
987.. method:: window.is_linetouched(line)
988
989   Return ``True`` if the specified line was modified since the last call to
990   :meth:`refresh`; otherwise return ``False``.  Raise a :exc:`curses.error`
991   exception if *line* is not valid for the given window.
992
993
994.. method:: window.is_wintouched()
995
996   Return ``True`` if the specified window was modified since the last call to
997   :meth:`refresh`; otherwise return ``False``.
998
999
1000.. method:: window.keypad(yes)
1001
1002   If *yes* is 1, escape sequences generated by some keys (keypad,  function keys)
1003   will be interpreted by :mod:`curses`. If *yes* is 0, escape sequences will be
1004   left as is in the input stream.
1005
1006
1007.. method:: window.leaveok(yes)
1008
1009   If *yes* is 1, cursor is left where it is on update, instead of being at "cursor
1010   position."  This reduces cursor movement where possible. If possible the cursor
1011   will be made invisible.
1012
1013   If *yes* is 0, cursor will always be at "cursor position" after an update.
1014
1015
1016.. method:: window.move(new_y, new_x)
1017
1018   Move cursor to ``(new_y, new_x)``.
1019
1020
1021.. method:: window.mvderwin(y, x)
1022
1023   Move the window inside its parent window.  The screen-relative parameters of
1024   the window are not changed.  This routine is used to display different parts of
1025   the parent window at the same physical position on the screen.
1026
1027
1028.. method:: window.mvwin(new_y, new_x)
1029
1030   Move the window so its upper-left corner is at ``(new_y, new_x)``.
1031
1032
1033.. method:: window.nodelay(yes)
1034
1035   If *yes* is ``1``, :meth:`getch` will be non-blocking.
1036
1037
1038.. method:: window.notimeout(yes)
1039
1040   If *yes* is ``1``, escape sequences will not be timed out.
1041
1042   If *yes* is ``0``, after a few milliseconds, an escape sequence will not be
1043   interpreted, and will be left in the input stream as is.
1044
1045
1046.. method:: window.noutrefresh()
1047
1048   Mark for refresh but wait.  This function updates the data structure
1049   representing the desired state of the window, but does not force an update of
1050   the physical screen.  To accomplish that, call  :func:`doupdate`.
1051
1052
1053.. method:: window.overlay(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])
1054
1055   Overlay the window on top of *destwin*. The windows need not be the same size,
1056   only the overlapping region is copied. This copy is non-destructive, which means
1057   that the current background character does not overwrite the old contents of
1058   *destwin*.
1059
1060   To get fine-grained control over the copied region, the second form of
1061   :meth:`overlay` can be used. *sminrow* and *smincol* are the upper-left
1062   coordinates of the source window, and the other variables mark a rectangle in
1063   the destination window.
1064
1065
1066.. method:: window.overwrite(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])
1067
1068   Overwrite the window on top of *destwin*. The windows need not be the same size,
1069   in which case only the overlapping region is copied. This copy is destructive,
1070   which means that the current background character overwrites the old contents of
1071   *destwin*.
1072
1073   To get fine-grained control over the copied region, the second form of
1074   :meth:`overwrite` can be used. *sminrow* and *smincol* are the upper-left
1075   coordinates of the source window, the other variables mark a rectangle in the
1076   destination window.
1077
1078
1079.. method:: window.putwin(file)
1080
1081   Write all data associated with the window into the provided file object.  This
1082   information can be later retrieved using the :func:`getwin` function.
1083
1084
1085.. method:: window.redrawln(beg, num)
1086
1087   Indicate that the *num* screen lines, starting at line *beg*, are corrupted and
1088   should be completely redrawn on the next :meth:`refresh` call.
1089
1090
1091.. method:: window.redrawwin()
1092
1093   Touch the entire window, causing it to be completely redrawn on the next
1094   :meth:`refresh` call.
1095
1096
1097.. method:: window.refresh([pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol])
1098
1099   Update the display immediately (sync actual screen with previous
1100   drawing/deleting methods).
1101
1102   The 6 optional arguments can only be specified when the window is a pad created
1103   with :func:`newpad`.  The additional parameters are needed to indicate what part
1104   of the pad and screen are involved. *pminrow* and *pmincol* specify the upper
1105   left-hand corner of the rectangle to be displayed in the pad.  *sminrow*,
1106   *smincol*, *smaxrow*, and *smaxcol* specify the edges of the rectangle to be
1107   displayed on the screen.  The lower right-hand corner of the rectangle to be
1108   displayed in the pad is calculated from the screen coordinates, since the
1109   rectangles must be the same size.  Both rectangles must be entirely contained
1110   within their respective structures.  Negative values of *pminrow*, *pmincol*,
1111   *sminrow*, or *smincol* are treated as if they were zero.
1112
1113
1114.. method:: window.resize(nlines, ncols)
1115
1116   Reallocate storage for a curses window to adjust its dimensions to the
1117   specified values.  If either dimension is larger than the current values, the
1118   window's data is filled with blanks that have the current background
1119   rendition (as set by :meth:`bkgdset`) merged into them.
1120
1121
1122.. method:: window.scroll([lines=1])
1123
1124   Scroll the screen or scrolling region upward by *lines* lines.
1125
1126
1127.. method:: window.scrollok(flag)
1128
1129   Control what happens when the cursor of a window is moved off the edge of the
1130   window or scrolling region, either as a result of a newline action on the bottom
1131   line, or typing the last character of the last line.  If *flag* is false, the
1132   cursor is left on the bottom line.  If *flag* is true, the window is scrolled up
1133   one line.  Note that in order to get the physical scrolling effect on the
1134   terminal, it is also necessary to call :meth:`idlok`.
1135
1136
1137.. method:: window.setscrreg(top, bottom)
1138
1139   Set the scrolling region from line *top* to line *bottom*. All scrolling actions
1140   will take place in this region.
1141
1142
1143.. method:: window.standend()
1144
1145   Turn off the standout attribute.  On some terminals this has the side effect of
1146   turning off all attributes.
1147
1148
1149.. method:: window.standout()
1150
1151   Turn on attribute *A_STANDOUT*.
1152
1153
1154.. method:: window.subpad(begin_y, begin_x)
1155            window.subpad(nlines, ncols, begin_y, begin_x)
1156
1157   Return a sub-window, whose upper-left corner is at ``(begin_y, begin_x)``, and
1158   whose width/height is *ncols*/*nlines*.
1159
1160
1161.. method:: window.subwin(begin_y, begin_x)
1162            window.subwin(nlines, ncols, begin_y, begin_x)
1163
1164   Return a sub-window, whose upper-left corner is at ``(begin_y, begin_x)``, and
1165   whose width/height is *ncols*/*nlines*.
1166
1167   By default, the sub-window will extend from the specified position to the lower
1168   right corner of the window.
1169
1170
1171.. method:: window.syncdown()
1172
1173   Touch each location in the window that has been touched in any of its ancestor
1174   windows.  This routine is called by :meth:`refresh`, so it should almost never
1175   be necessary to call it manually.
1176
1177
1178.. method:: window.syncok(flag)
1179
1180   If called with *flag* set to ``True``, then :meth:`syncup` is called automatically
1181   whenever there is a change in the window.
1182
1183
1184.. method:: window.syncup()
1185
1186   Touch all locations in ancestors of the window that have been changed in  the
1187   window.
1188
1189
1190.. method:: window.timeout(delay)
1191
1192   Set blocking or non-blocking read behavior for the window.  If *delay* is
1193   negative, blocking read is used (which will wait indefinitely for input).  If
1194   *delay* is zero, then non-blocking read is used, and -1 will be returned by
1195   :meth:`getch` if no input is waiting.  If *delay* is positive, then
1196   :meth:`getch` will block for *delay* milliseconds, and return -1 if there is
1197   still no input at the end of that time.
1198
1199
1200.. method:: window.touchline(start, count[, changed])
1201
1202   Pretend *count* lines have been changed, starting with line *start*.  If
1203   *changed* is supplied, it specifies whether the affected lines are marked as
1204   having been changed (*changed*\ =1) or unchanged (*changed*\ =0).
1205
1206
1207.. method:: window.touchwin()
1208
1209   Pretend the whole window has been changed, for purposes of drawing
1210   optimizations.
1211
1212
1213.. method:: window.untouchwin()
1214
1215   Mark all lines in  the  window  as unchanged since the last call to
1216   :meth:`refresh`.
1217
1218
1219.. method:: window.vline(ch, n)
1220            window.vline(y, x, ch, n)
1221
1222   Display a vertical line starting at ``(y, x)`` with length *n* consisting of the
1223   character *ch*.
1224
1225
1226Constants
1227---------
1228
1229The :mod:`curses` module defines the following data members:
1230
1231
1232.. data:: ERR
1233
1234   Some curses routines  that  return  an integer, such as  :func:`getch`, return
1235   :const:`ERR` upon failure.
1236
1237
1238.. data:: OK
1239
1240   Some curses routines  that  return  an integer, such as  :func:`napms`, return
1241   :const:`OK` upon success.
1242
1243
1244.. data:: version
1245
1246   A string representing the current version of the module.  Also available as
1247   :const:`__version__`.
1248
1249Some constants are available to specify character cell attributes.
1250The exact constants available are system dependent.
1251
1252+------------------+-------------------------------+
1253| Attribute        | Meaning                       |
1254+==================+===============================+
1255| ``A_ALTCHARSET`` | Alternate character set mode  |
1256+------------------+-------------------------------+
1257| ``A_BLINK``      | Blink mode                    |
1258+------------------+-------------------------------+
1259| ``A_BOLD``       | Bold mode                     |
1260+------------------+-------------------------------+
1261| ``A_DIM``        | Dim mode                      |
1262+------------------+-------------------------------+
1263| ``A_INVIS``      | Invisible or blank mode       |
1264+------------------+-------------------------------+
1265| ``A_NORMAL``     | Normal attribute              |
1266+------------------+-------------------------------+
1267| ``A_PROTECT``    | Protected mode                |
1268+------------------+-------------------------------+
1269| ``A_REVERSE``    | Reverse background and        |
1270|                  | foreground colors             |
1271+------------------+-------------------------------+
1272| ``A_STANDOUT``   | Standout mode                 |
1273+------------------+-------------------------------+
1274| ``A_UNDERLINE``  | Underline mode                |
1275+------------------+-------------------------------+
1276| ``A_HORIZONTAL`` | Horizontal highlight          |
1277+------------------+-------------------------------+
1278| ``A_LEFT``       | Left highlight                |
1279+------------------+-------------------------------+
1280| ``A_LOW``        | Low highlight                 |
1281+------------------+-------------------------------+
1282| ``A_RIGHT``      | Right highlight               |
1283+------------------+-------------------------------+
1284| ``A_TOP``        | Top highlight                 |
1285+------------------+-------------------------------+
1286| ``A_VERTICAL``   | Vertical highlight            |
1287+------------------+-------------------------------+
1288| ``A_CHARTEXT``   | Bit-mask to extract a         |
1289|                  | character                     |
1290+------------------+-------------------------------+
1291
1292Several constants are available to extract corresponding attributes returned
1293by some methods.
1294
1295+------------------+-------------------------------+
1296| Bit-mask         | Meaning                       |
1297+==================+===============================+
1298| ``A_ATTRIBUTES`` | Bit-mask to extract           |
1299|                  | attributes                    |
1300+------------------+-------------------------------+
1301| ``A_CHARTEXT``   | Bit-mask to extract a         |
1302|                  | character                     |
1303+------------------+-------------------------------+
1304| ``A_COLOR``      | Bit-mask to extract           |
1305|                  | color-pair field information  |
1306+------------------+-------------------------------+
1307
1308Keys are referred to by integer constants with names starting with  ``KEY_``.
1309The exact keycaps available are system dependent.
1310
1311.. XXX this table is far too large! should it be alphabetized?
1312
1313+-------------------+--------------------------------------------+
1314| Key constant      | Key                                        |
1315+===================+============================================+
1316| ``KEY_MIN``       | Minimum key value                          |
1317+-------------------+--------------------------------------------+
1318| ``KEY_BREAK``     | Break key (unreliable)                     |
1319+-------------------+--------------------------------------------+
1320| ``KEY_DOWN``      | Down-arrow                                 |
1321+-------------------+--------------------------------------------+
1322| ``KEY_UP``        | Up-arrow                                   |
1323+-------------------+--------------------------------------------+
1324| ``KEY_LEFT``      | Left-arrow                                 |
1325+-------------------+--------------------------------------------+
1326| ``KEY_RIGHT``     | Right-arrow                                |
1327+-------------------+--------------------------------------------+
1328| ``KEY_HOME``      | Home key (upward+left arrow)               |
1329+-------------------+--------------------------------------------+
1330| ``KEY_BACKSPACE`` | Backspace (unreliable)                     |
1331+-------------------+--------------------------------------------+
1332| ``KEY_F0``        | Function keys.  Up to 64 function keys are |
1333|                   | supported.                                 |
1334+-------------------+--------------------------------------------+
1335| ``KEY_Fn``        | Value of function key *n*                  |
1336+-------------------+--------------------------------------------+
1337| ``KEY_DL``        | Delete line                                |
1338+-------------------+--------------------------------------------+
1339| ``KEY_IL``        | Insert line                                |
1340+-------------------+--------------------------------------------+
1341| ``KEY_DC``        | Delete character                           |
1342+-------------------+--------------------------------------------+
1343| ``KEY_IC``        | Insert char or enter insert mode           |
1344+-------------------+--------------------------------------------+
1345| ``KEY_EIC``       | Exit insert char mode                      |
1346+-------------------+--------------------------------------------+
1347| ``KEY_CLEAR``     | Clear screen                               |
1348+-------------------+--------------------------------------------+
1349| ``KEY_EOS``       | Clear to end of screen                     |
1350+-------------------+--------------------------------------------+
1351| ``KEY_EOL``       | Clear to end of line                       |
1352+-------------------+--------------------------------------------+
1353| ``KEY_SF``        | Scroll 1 line forward                      |
1354+-------------------+--------------------------------------------+
1355| ``KEY_SR``        | Scroll 1 line backward (reverse)           |
1356+-------------------+--------------------------------------------+
1357| ``KEY_NPAGE``     | Next page                                  |
1358+-------------------+--------------------------------------------+
1359| ``KEY_PPAGE``     | Previous page                              |
1360+-------------------+--------------------------------------------+
1361| ``KEY_STAB``      | Set tab                                    |
1362+-------------------+--------------------------------------------+
1363| ``KEY_CTAB``      | Clear tab                                  |
1364+-------------------+--------------------------------------------+
1365| ``KEY_CATAB``     | Clear all tabs                             |
1366+-------------------+--------------------------------------------+
1367| ``KEY_ENTER``     | Enter or send (unreliable)                 |
1368+-------------------+--------------------------------------------+
1369| ``KEY_SRESET``    | Soft (partial) reset (unreliable)          |
1370+-------------------+--------------------------------------------+
1371| ``KEY_RESET``     | Reset or hard reset (unreliable)           |
1372+-------------------+--------------------------------------------+
1373| ``KEY_PRINT``     | Print                                      |
1374+-------------------+--------------------------------------------+
1375| ``KEY_LL``        | Home down or bottom (lower left)           |
1376+-------------------+--------------------------------------------+
1377| ``KEY_A1``        | Upper left of keypad                       |
1378+-------------------+--------------------------------------------+
1379| ``KEY_A3``        | Upper right of keypad                      |
1380+-------------------+--------------------------------------------+
1381| ``KEY_B2``        | Center of keypad                           |
1382+-------------------+--------------------------------------------+
1383| ``KEY_C1``        | Lower left of keypad                       |
1384+-------------------+--------------------------------------------+
1385| ``KEY_C3``        | Lower right of keypad                      |
1386+-------------------+--------------------------------------------+
1387| ``KEY_BTAB``      | Back tab                                   |
1388+-------------------+--------------------------------------------+
1389| ``KEY_BEG``       | Beg (beginning)                            |
1390+-------------------+--------------------------------------------+
1391| ``KEY_CANCEL``    | Cancel                                     |
1392+-------------------+--------------------------------------------+
1393| ``KEY_CLOSE``     | Close                                      |
1394+-------------------+--------------------------------------------+
1395| ``KEY_COMMAND``   | Cmd (command)                              |
1396+-------------------+--------------------------------------------+
1397| ``KEY_COPY``      | Copy                                       |
1398+-------------------+--------------------------------------------+
1399| ``KEY_CREATE``    | Create                                     |
1400+-------------------+--------------------------------------------+
1401| ``KEY_END``       | End                                        |
1402+-------------------+--------------------------------------------+
1403| ``KEY_EXIT``      | Exit                                       |
1404+-------------------+--------------------------------------------+
1405| ``KEY_FIND``      | Find                                       |
1406+-------------------+--------------------------------------------+
1407| ``KEY_HELP``      | Help                                       |
1408+-------------------+--------------------------------------------+
1409| ``KEY_MARK``      | Mark                                       |
1410+-------------------+--------------------------------------------+
1411| ``KEY_MESSAGE``   | Message                                    |
1412+-------------------+--------------------------------------------+
1413| ``KEY_MOVE``      | Move                                       |
1414+-------------------+--------------------------------------------+
1415| ``KEY_NEXT``      | Next                                       |
1416+-------------------+--------------------------------------------+
1417| ``KEY_OPEN``      | Open                                       |
1418+-------------------+--------------------------------------------+
1419| ``KEY_OPTIONS``   | Options                                    |
1420+-------------------+--------------------------------------------+
1421| ``KEY_PREVIOUS``  | Prev (previous)                            |
1422+-------------------+--------------------------------------------+
1423| ``KEY_REDO``      | Redo                                       |
1424+-------------------+--------------------------------------------+
1425| ``KEY_REFERENCE`` | Ref (reference)                            |
1426+-------------------+--------------------------------------------+
1427| ``KEY_REFRESH``   | Refresh                                    |
1428+-------------------+--------------------------------------------+
1429| ``KEY_REPLACE``   | Replace                                    |
1430+-------------------+--------------------------------------------+
1431| ``KEY_RESTART``   | Restart                                    |
1432+-------------------+--------------------------------------------+
1433| ``KEY_RESUME``    | Resume                                     |
1434+-------------------+--------------------------------------------+
1435| ``KEY_SAVE``      | Save                                       |
1436+-------------------+--------------------------------------------+
1437| ``KEY_SBEG``      | Shifted Beg (beginning)                    |
1438+-------------------+--------------------------------------------+
1439| ``KEY_SCANCEL``   | Shifted Cancel                             |
1440+-------------------+--------------------------------------------+
1441| ``KEY_SCOMMAND``  | Shifted Command                            |
1442+-------------------+--------------------------------------------+
1443| ``KEY_SCOPY``     | Shifted Copy                               |
1444+-------------------+--------------------------------------------+
1445| ``KEY_SCREATE``   | Shifted Create                             |
1446+-------------------+--------------------------------------------+
1447| ``KEY_SDC``       | Shifted Delete char                        |
1448+-------------------+--------------------------------------------+
1449| ``KEY_SDL``       | Shifted Delete line                        |
1450+-------------------+--------------------------------------------+
1451| ``KEY_SELECT``    | Select                                     |
1452+-------------------+--------------------------------------------+
1453| ``KEY_SEND``      | Shifted End                                |
1454+-------------------+--------------------------------------------+
1455| ``KEY_SEOL``      | Shifted Clear line                         |
1456+-------------------+--------------------------------------------+
1457| ``KEY_SEXIT``     | Shifted Exit                               |
1458+-------------------+--------------------------------------------+
1459| ``KEY_SFIND``     | Shifted Find                               |
1460+-------------------+--------------------------------------------+
1461| ``KEY_SHELP``     | Shifted Help                               |
1462+-------------------+--------------------------------------------+
1463| ``KEY_SHOME``     | Shifted Home                               |
1464+-------------------+--------------------------------------------+
1465| ``KEY_SIC``       | Shifted Input                              |
1466+-------------------+--------------------------------------------+
1467| ``KEY_SLEFT``     | Shifted Left arrow                         |
1468+-------------------+--------------------------------------------+
1469| ``KEY_SMESSAGE``  | Shifted Message                            |
1470+-------------------+--------------------------------------------+
1471| ``KEY_SMOVE``     | Shifted Move                               |
1472+-------------------+--------------------------------------------+
1473| ``KEY_SNEXT``     | Shifted Next                               |
1474+-------------------+--------------------------------------------+
1475| ``KEY_SOPTIONS``  | Shifted Options                            |
1476+-------------------+--------------------------------------------+
1477| ``KEY_SPREVIOUS`` | Shifted Prev                               |
1478+-------------------+--------------------------------------------+
1479| ``KEY_SPRINT``    | Shifted Print                              |
1480+-------------------+--------------------------------------------+
1481| ``KEY_SREDO``     | Shifted Redo                               |
1482+-------------------+--------------------------------------------+
1483| ``KEY_SREPLACE``  | Shifted Replace                            |
1484+-------------------+--------------------------------------------+
1485| ``KEY_SRIGHT``    | Shifted Right arrow                        |
1486+-------------------+--------------------------------------------+
1487| ``KEY_SRSUME``    | Shifted Resume                             |
1488+-------------------+--------------------------------------------+
1489| ``KEY_SSAVE``     | Shifted Save                               |
1490+-------------------+--------------------------------------------+
1491| ``KEY_SSUSPEND``  | Shifted Suspend                            |
1492+-------------------+--------------------------------------------+
1493| ``KEY_SUNDO``     | Shifted Undo                               |
1494+-------------------+--------------------------------------------+
1495| ``KEY_SUSPEND``   | Suspend                                    |
1496+-------------------+--------------------------------------------+
1497| ``KEY_UNDO``      | Undo                                       |
1498+-------------------+--------------------------------------------+
1499| ``KEY_MOUSE``     | Mouse event has occurred                   |
1500+-------------------+--------------------------------------------+
1501| ``KEY_RESIZE``    | Terminal resize event                      |
1502+-------------------+--------------------------------------------+
1503| ``KEY_MAX``       | Maximum key value                          |
1504+-------------------+--------------------------------------------+
1505
1506On VT100s and their software emulations, such as X terminal emulators, there are
1507normally at least four function keys (:const:`KEY_F1`, :const:`KEY_F2`,
1508:const:`KEY_F3`, :const:`KEY_F4`) available, and the arrow keys mapped to
1509:const:`KEY_UP`, :const:`KEY_DOWN`, :const:`KEY_LEFT` and :const:`KEY_RIGHT` in
1510the obvious way.  If your machine has a PC keyboard, it is safe to expect arrow
1511keys and twelve function keys (older PC keyboards may have only ten function
1512keys); also, the following keypad mappings are standard:
1513
1514+------------------+-----------+
1515| Keycap           | Constant  |
1516+==================+===========+
1517| :kbd:`Insert`    | KEY_IC    |
1518+------------------+-----------+
1519| :kbd:`Delete`    | KEY_DC    |
1520+------------------+-----------+
1521| :kbd:`Home`      | KEY_HOME  |
1522+------------------+-----------+
1523| :kbd:`End`       | KEY_END   |
1524+------------------+-----------+
1525| :kbd:`Page Up`   | KEY_PPAGE |
1526+------------------+-----------+
1527| :kbd:`Page Down` | KEY_NPAGE |
1528+------------------+-----------+
1529
1530The following table lists characters from the alternate character set. These are
1531inherited from the VT100 terminal, and will generally be  available on software
1532emulations such as X terminals.  When there is no graphic available, curses
1533falls back on a crude printable ASCII approximation.
1534
1535.. note::
1536
1537   These are available only after :func:`initscr` has  been called.
1538
1539+------------------+------------------------------------------+
1540| ACS code         | Meaning                                  |
1541+==================+==========================================+
1542| ``ACS_BBSS``     | alternate name for upper right corner    |
1543+------------------+------------------------------------------+
1544| ``ACS_BLOCK``    | solid square block                       |
1545+------------------+------------------------------------------+
1546| ``ACS_BOARD``    | board of squares                         |
1547+------------------+------------------------------------------+
1548| ``ACS_BSBS``     | alternate name for horizontal line       |
1549+------------------+------------------------------------------+
1550| ``ACS_BSSB``     | alternate name for upper left corner     |
1551+------------------+------------------------------------------+
1552| ``ACS_BSSS``     | alternate name for top tee               |
1553+------------------+------------------------------------------+
1554| ``ACS_BTEE``     | bottom tee                               |
1555+------------------+------------------------------------------+
1556| ``ACS_BULLET``   | bullet                                   |
1557+------------------+------------------------------------------+
1558| ``ACS_CKBOARD``  | checker board (stipple)                  |
1559+------------------+------------------------------------------+
1560| ``ACS_DARROW``   | arrow pointing down                      |
1561+------------------+------------------------------------------+
1562| ``ACS_DEGREE``   | degree symbol                            |
1563+------------------+------------------------------------------+
1564| ``ACS_DIAMOND``  | diamond                                  |
1565+------------------+------------------------------------------+
1566| ``ACS_GEQUAL``   | greater-than-or-equal-to                 |
1567+------------------+------------------------------------------+
1568| ``ACS_HLINE``    | horizontal line                          |
1569+------------------+------------------------------------------+
1570| ``ACS_LANTERN``  | lantern symbol                           |
1571+------------------+------------------------------------------+
1572| ``ACS_LARROW``   | left arrow                               |
1573+------------------+------------------------------------------+
1574| ``ACS_LEQUAL``   | less-than-or-equal-to                    |
1575+------------------+------------------------------------------+
1576| ``ACS_LLCORNER`` | lower left-hand corner                   |
1577+------------------+------------------------------------------+
1578| ``ACS_LRCORNER`` | lower right-hand corner                  |
1579+------------------+------------------------------------------+
1580| ``ACS_LTEE``     | left tee                                 |
1581+------------------+------------------------------------------+
1582| ``ACS_NEQUAL``   | not-equal sign                           |
1583+------------------+------------------------------------------+
1584| ``ACS_PI``       | letter pi                                |
1585+------------------+------------------------------------------+
1586| ``ACS_PLMINUS``  | plus-or-minus sign                       |
1587+------------------+------------------------------------------+
1588| ``ACS_PLUS``     | big plus sign                            |
1589+------------------+------------------------------------------+
1590| ``ACS_RARROW``   | right arrow                              |
1591+------------------+------------------------------------------+
1592| ``ACS_RTEE``     | right tee                                |
1593+------------------+------------------------------------------+
1594| ``ACS_S1``       | scan line 1                              |
1595+------------------+------------------------------------------+
1596| ``ACS_S3``       | scan line 3                              |
1597+------------------+------------------------------------------+
1598| ``ACS_S7``       | scan line 7                              |
1599+------------------+------------------------------------------+
1600| ``ACS_S9``       | scan line 9                              |
1601+------------------+------------------------------------------+
1602| ``ACS_SBBS``     | alternate name for lower right corner    |
1603+------------------+------------------------------------------+
1604| ``ACS_SBSB``     | alternate name for vertical line         |
1605+------------------+------------------------------------------+
1606| ``ACS_SBSS``     | alternate name for right tee             |
1607+------------------+------------------------------------------+
1608| ``ACS_SSBB``     | alternate name for lower left corner     |
1609+------------------+------------------------------------------+
1610| ``ACS_SSBS``     | alternate name for bottom tee            |
1611+------------------+------------------------------------------+
1612| ``ACS_SSSB``     | alternate name for left tee              |
1613+------------------+------------------------------------------+
1614| ``ACS_SSSS``     | alternate name for crossover or big plus |
1615+------------------+------------------------------------------+
1616| ``ACS_STERLING`` | pound sterling                           |
1617+------------------+------------------------------------------+
1618| ``ACS_TTEE``     | top tee                                  |
1619+------------------+------------------------------------------+
1620| ``ACS_UARROW``   | up arrow                                 |
1621+------------------+------------------------------------------+
1622| ``ACS_ULCORNER`` | upper left corner                        |
1623+------------------+------------------------------------------+
1624| ``ACS_URCORNER`` | upper right corner                       |
1625+------------------+------------------------------------------+
1626| ``ACS_VLINE``    | vertical line                            |
1627+------------------+------------------------------------------+
1628
1629The following table lists the predefined colors:
1630
1631+-------------------+----------------------------+
1632| Constant          | Color                      |
1633+===================+============================+
1634| ``COLOR_BLACK``   | Black                      |
1635+-------------------+----------------------------+
1636| ``COLOR_BLUE``    | Blue                       |
1637+-------------------+----------------------------+
1638| ``COLOR_CYAN``    | Cyan (light greenish blue) |
1639+-------------------+----------------------------+
1640| ``COLOR_GREEN``   | Green                      |
1641+-------------------+----------------------------+
1642| ``COLOR_MAGENTA`` | Magenta (purplish red)     |
1643+-------------------+----------------------------+
1644| ``COLOR_RED``     | Red                        |
1645+-------------------+----------------------------+
1646| ``COLOR_WHITE``   | White                      |
1647+-------------------+----------------------------+
1648| ``COLOR_YELLOW``  | Yellow                     |
1649+-------------------+----------------------------+
1650
1651
1652:mod:`curses.textpad` --- Text input widget for curses programs
1653===============================================================
1654
1655.. module:: curses.textpad
1656   :synopsis: Emacs-like input editing in a curses window.
1657.. moduleauthor:: Eric Raymond <[email protected]>
1658.. sectionauthor:: Eric Raymond <[email protected]>
1659
1660
1661.. versionadded:: 1.6
1662
1663The :mod:`curses.textpad` module provides a :class:`Textbox` class that handles
1664elementary text editing in a curses window, supporting a set of keybindings
1665resembling those of Emacs (thus, also of Netscape Navigator, BBedit 6.x,
1666FrameMaker, and many other programs).  The module also provides a
1667rectangle-drawing function useful for framing text boxes or for other purposes.
1668
1669The module :mod:`curses.textpad` defines the following function:
1670
1671
1672.. function:: rectangle(win, uly, ulx, lry, lrx)
1673
1674   Draw a rectangle.  The first argument must be a window object; the remaining
1675   arguments are coordinates relative to that window.  The second and third
1676   arguments are the y and x coordinates of the upper left hand corner of the
1677   rectangle to be drawn; the fourth and fifth arguments are the y and x
1678   coordinates of the lower right hand corner. The rectangle will be drawn using
1679   VT100/IBM PC forms characters on terminals that make this possible (including
1680   xterm and most other software terminal emulators).  Otherwise it will be drawn
1681   with ASCII  dashes, vertical bars, and plus signs.
1682
1683
1684.. _curses-textpad-objects:
1685
1686Textbox objects
1687---------------
1688
1689You can instantiate a :class:`Textbox` object as follows:
1690
1691
1692.. class:: Textbox(win)
1693
1694   Return a textbox widget object.  The *win* argument should be a curses
1695   :class:`WindowObject` in which the textbox is to be contained. The edit cursor
1696   of the textbox is initially located at the upper left hand corner of the
1697   containing window, with coordinates ``(0, 0)``. The instance's
1698   :attr:`stripspaces` flag is initially on.
1699
1700   :class:`Textbox` objects have the following methods:
1701
1702
1703   .. method:: edit([validator])
1704
1705      This is the entry point you will normally use.  It accepts editing
1706      keystrokes until one of the termination keystrokes is entered.  If
1707      *validator* is supplied, it must be a function.  It will be called for
1708      each keystroke entered with the keystroke as a parameter; command dispatch
1709      is done on the result. This method returns the window contents as a
1710      string; whether blanks in the window are included is affected by the
1711      :attr:`stripspaces` attribute.
1712
1713
1714   .. method:: do_command(ch)
1715
1716      Process a single command keystroke.  Here are the supported special
1717      keystrokes:
1718
1719      +------------------+-------------------------------------------+
1720      | Keystroke        | Action                                    |
1721      +==================+===========================================+
1722      | :kbd:`Control-A` | Go to left edge of window.                |
1723      +------------------+-------------------------------------------+
1724      | :kbd:`Control-B` | Cursor left, wrapping to previous line if |
1725      |                  | appropriate.                              |
1726      +------------------+-------------------------------------------+
1727      | :kbd:`Control-D` | Delete character under cursor.            |
1728      +------------------+-------------------------------------------+
1729      | :kbd:`Control-E` | Go to right edge (stripspaces off) or end |
1730      |                  | of line (stripspaces on).                 |
1731      +------------------+-------------------------------------------+
1732      | :kbd:`Control-F` | Cursor right, wrapping to next line when  |
1733      |                  | appropriate.                              |
1734      +------------------+-------------------------------------------+
1735      | :kbd:`Control-G` | Terminate, returning the window contents. |
1736      +------------------+-------------------------------------------+
1737      | :kbd:`Control-H` | Delete character backward.                |
1738      +------------------+-------------------------------------------+
1739      | :kbd:`Control-J` | Terminate if the window is 1 line,        |
1740      |                  | otherwise insert newline.                 |
1741      +------------------+-------------------------------------------+
1742      | :kbd:`Control-K` | If line is blank, delete it, otherwise    |
1743      |                  | clear to end of line.                     |
1744      +------------------+-------------------------------------------+
1745      | :kbd:`Control-L` | Refresh screen.                           |
1746      +------------------+-------------------------------------------+
1747      | :kbd:`Control-N` | Cursor down; move down one line.          |
1748      +------------------+-------------------------------------------+
1749      | :kbd:`Control-O` | Insert a blank line at cursor location.   |
1750      +------------------+-------------------------------------------+
1751      | :kbd:`Control-P` | Cursor up; move up one line.              |
1752      +------------------+-------------------------------------------+
1753
1754      Move operations do nothing if the cursor is at an edge where the movement
1755      is not possible.  The following synonyms are supported where possible:
1756
1757      +------------------------+------------------+
1758      | Constant               | Keystroke        |
1759      +========================+==================+
1760      | :const:`KEY_LEFT`      | :kbd:`Control-B` |
1761      +------------------------+------------------+
1762      | :const:`KEY_RIGHT`     | :kbd:`Control-F` |
1763      +------------------------+------------------+
1764      | :const:`KEY_UP`        | :kbd:`Control-P` |
1765      +------------------------+------------------+
1766      | :const:`KEY_DOWN`      | :kbd:`Control-N` |
1767      +------------------------+------------------+
1768      | :const:`KEY_BACKSPACE` | :kbd:`Control-h` |
1769      +------------------------+------------------+
1770
1771      All other keystrokes are treated as a command to insert the given
1772      character and move right (with line wrapping).
1773
1774
1775   .. method:: gather()
1776
1777      Return the window contents as a string; whether blanks in the
1778      window are included is affected by the :attr:`stripspaces` member.
1779
1780
1781   .. attribute:: stripspaces
1782
1783      This attribute is a flag which controls the interpretation of blanks in
1784      the window.  When it is on, trailing blanks on each line are ignored; any
1785      cursor motion that would land the cursor on a trailing blank goes to the
1786      end of that line instead, and trailing blanks are stripped when the window
1787      contents are gathered.
1788
1789