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