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