blob: 1201e8972de63c8c76f00279ad20fa87f9cdc29c [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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04008
Georg Brandl116aa622007-08-15 14:28:22 +00009.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
10.. sectionauthor:: Eric Raymond <esr@thyrsus.com>
11
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040012--------------
13
Georg Brandl116aa622007-08-15 14:28:22 +000014The :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
Andrew Kuchling98f2bbf2014-03-01 07:53:28 -050018for Windows, DOS, and possibly other systems as well. This extension module is
Georg Brandl116aa622007-08-15 14:28:22 +000019designed to match the API of ncurses, an open-source curses library hosted on
20Linux and the BSD variants of Unix.
21
Christian Heimes587c2bf2008-01-19 16:21:02 +000022.. note::
23
Serhiy Storchaka300dd552017-10-04 22:44:13 +030024 Whenever the documentation mentions a *character* it can be specified
25 as an integer, a one-character Unicode string or a one-byte byte string.
26
27 Whenever the documentation mentions a *character string* it can be specified
28 as a Unicode string or a byte string.
29
30.. note::
31
Christian Heimes587c2bf2008-01-19 16:21:02 +000032 Since version 5.4, the ncurses library decides how to interpret non-ASCII data
33 using the ``nl_langinfo`` function. That means that you have to call
34 :func:`locale.setlocale` in the application and encode Unicode strings
35 using one of the system's available encodings. This example uses the
36 system's default encoding::
37
38 import locale
39 locale.setlocale(locale.LC_ALL, '')
40 code = locale.getpreferredencoding()
41
42 Then use *code* as the encoding for :meth:`str.encode` calls.
Georg Brandl116aa622007-08-15 14:28:22 +000043
44.. seealso::
45
46 Module :mod:`curses.ascii`
47 Utilities for working with ASCII characters, regardless of your locale settings.
48
49 Module :mod:`curses.panel`
50 A panel stack extension that adds depth to curses windows.
51
52 Module :mod:`curses.textpad`
53 Editable text widget for curses supporting :program:`Emacs`\ -like bindings.
54
Christian Heimes2202f872008-02-06 14:31:34 +000055 :ref:`curses-howto`
Georg Brandl116aa622007-08-15 14:28:22 +000056 Tutorial material on using curses with Python, by Andrew Kuchling and Eric
Christian Heimes2202f872008-02-06 14:31:34 +000057 Raymond.
Georg Brandl116aa622007-08-15 14:28:22 +000058
Ezio Melottic6641db2013-02-28 18:02:28 +020059 The :source:`Tools/demo/` directory in the Python source distribution contains
Georg Brandl116aa622007-08-15 14:28:22 +000060 some example programs using the curses bindings provided by this module.
61
62
63.. _curses-functions:
64
65Functions
66---------
67
68The module :mod:`curses` defines the following exception:
69
70
71.. exception:: error
72
73 Exception raised when a curses library function returns an error.
74
75.. note::
76
77 Whenever *x* or *y* arguments to a function or a method are optional, they
78 default to the current cursor location. Whenever *attr* is optional, it defaults
79 to :const:`A_NORMAL`.
80
81The module :mod:`curses` defines the following functions:
82
83
84.. function:: baudrate()
85
Ezio Melottib6b73712011-06-26 13:38:11 +030086 Return the output speed of the terminal in bits per second. On software
Georg Brandl116aa622007-08-15 14:28:22 +000087 terminal emulators it will have a fixed high value. Included for historical
88 reasons; in former times, it was used to write output loops for time delays and
89 occasionally to change interfaces depending on the line speed.
90
91
92.. function:: beep()
93
94 Emit a short attention sound.
95
96
97.. function:: can_change_color()
98
Ezio Melottib6b73712011-06-26 13:38:11 +030099 Return ``True`` or ``False``, depending on whether the programmer can change the colors
Georg Brandl116aa622007-08-15 14:28:22 +0000100 displayed by the terminal.
101
102
103.. function:: cbreak()
104
105 Enter cbreak mode. In cbreak mode (sometimes called "rare" mode) normal tty
106 line buffering is turned off and characters are available to be read one by one.
107 However, unlike raw mode, special characters (interrupt, quit, suspend, and flow
108 control) retain their effects on the tty driver and calling program. Calling
109 first :func:`raw` then :func:`cbreak` leaves the terminal in cbreak mode.
110
111
112.. function:: color_content(color_number)
113
Ezio Melottib6b73712011-06-26 13:38:11 +0300114 Return the intensity of the red, green, and blue (RGB) components in the color
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300115 *color_number*, which must be between ``0`` and :const:`COLORS`. Return a 3-tuple,
116 containing the R,G,B values for the given color, which will be between
Georg Brandl116aa622007-08-15 14:28:22 +0000117 ``0`` (no component) and ``1000`` (maximum amount of component).
118
119
120.. function:: color_pair(color_number)
121
Ezio Melottib6b73712011-06-26 13:38:11 +0300122 Return the attribute value for displaying text in the specified color. This
Georg Brandl116aa622007-08-15 14:28:22 +0000123 attribute value can be combined with :const:`A_STANDOUT`, :const:`A_REVERSE`,
124 and the other :const:`A_\*` attributes. :func:`pair_number` is the counterpart
125 to this function.
126
127
128.. function:: curs_set(visibility)
129
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300130 Set the cursor state. *visibility* can be set to ``0``, ``1``, or ``2``, for invisible,
131 normal, or very visible. If the terminal supports the visibility requested, return the
132 previous cursor state; otherwise raise an exception. On many
Georg Brandl116aa622007-08-15 14:28:22 +0000133 terminals, the "visible" mode is an underline cursor and the "very visible" mode
134 is a block cursor.
135
136
137.. function:: def_prog_mode()
138
Ezio Melottib6b73712011-06-26 13:38:11 +0300139 Save the current terminal mode as the "program" mode, the mode when the running
Georg Brandl116aa622007-08-15 14:28:22 +0000140 program is using curses. (Its counterpart is the "shell" mode, for when the
141 program is not in curses.) Subsequent calls to :func:`reset_prog_mode` will
142 restore this mode.
143
144
145.. function:: def_shell_mode()
146
Ezio Melottib6b73712011-06-26 13:38:11 +0300147 Save the current terminal mode as the "shell" mode, the mode when the running
Georg Brandl116aa622007-08-15 14:28:22 +0000148 program is not using curses. (Its counterpart is the "program" mode, when the
149 program is using curses capabilities.) Subsequent calls to
150 :func:`reset_shell_mode` will restore this mode.
151
152
153.. function:: delay_output(ms)
154
Ezio Melottib6b73712011-06-26 13:38:11 +0300155 Insert an *ms* millisecond pause in output.
Georg Brandl116aa622007-08-15 14:28:22 +0000156
157
158.. function:: doupdate()
159
160 Update the physical screen. The curses library keeps two data structures, one
161 representing the current physical screen contents and a virtual screen
162 representing the desired next state. The :func:`doupdate` ground updates the
163 physical screen to match the virtual screen.
164
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300165 The virtual screen may be updated by a :meth:`~window.noutrefresh` call after write
166 operations such as :meth:`~window.addstr` have been performed on a window. The normal
167 :meth:`~window.refresh` call is simply :meth:`!noutrefresh` followed by :func:`!doupdate`;
Georg Brandl116aa622007-08-15 14:28:22 +0000168 if you have to update multiple windows, you can speed performance and perhaps
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300169 reduce screen flicker by issuing :meth:`!noutrefresh` calls on all windows,
170 followed by a single :func:`!doupdate`.
Georg Brandl116aa622007-08-15 14:28:22 +0000171
172
173.. function:: echo()
174
175 Enter echo mode. In echo mode, each character input is echoed to the screen as
176 it is entered.
177
178
179.. function:: endwin()
180
181 De-initialize the library, and return terminal to normal status.
182
183
184.. function:: erasechar()
185
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300186 Return the user's current erase character as a one-byte bytes object. Under Unix operating systems this
Georg Brandl116aa622007-08-15 14:28:22 +0000187 is a property of the controlling tty of the curses program, and is not set by
188 the curses library itself.
189
190
191.. function:: filter()
192
Georg Brandl502d9a52009-07-26 15:02:41 +0000193 The :func:`.filter` routine, if used, must be called before :func:`initscr` is
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300194 called. The effect is that, during those calls, :envvar:`LINES` is set to ``1``; the
195 capabilities ``clear``, ``cup``, ``cud``, ``cud1``, ``cuu1``, ``cuu``, ``vpa`` are disabled; and the ``home``
196 string is set to the value of ``cr``. The effect is that the cursor is confined to
Georg Brandl116aa622007-08-15 14:28:22 +0000197 the current line, and so are screen updates. This may be used for enabling
198 character-at-a-time line editing without touching the rest of the screen.
199
200
201.. function:: flash()
202
203 Flash the screen. That is, change it to reverse-video and then change it back
204 in a short interval. Some people prefer such as 'visible bell' to the audible
205 attention signal produced by :func:`beep`.
206
207
208.. function:: flushinp()
209
210 Flush all input buffers. This throws away any typeahead that has been typed
211 by the user and has not yet been processed by the program.
212
213
214.. function:: getmouse()
215
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300216 After :meth:`~window.getch` returns :const:`KEY_MOUSE` to signal a mouse event, this
Georg Brandl116aa622007-08-15 14:28:22 +0000217 method should be call to retrieve the queued mouse event, represented as a
218 5-tuple ``(id, x, y, z, bstate)``. *id* is an ID value used to distinguish
219 multiple devices, and *x*, *y*, *z* are the event's coordinates. (*z* is
Ezio Melottib6b73712011-06-26 13:38:11 +0300220 currently unused.) *bstate* is an integer value whose bits will be set to
Georg Brandl116aa622007-08-15 14:28:22 +0000221 indicate the type of event, and will be the bitwise OR of one or more of the
222 following constants, where *n* is the button number from 1 to 4:
223 :const:`BUTTONn_PRESSED`, :const:`BUTTONn_RELEASED`, :const:`BUTTONn_CLICKED`,
224 :const:`BUTTONn_DOUBLE_CLICKED`, :const:`BUTTONn_TRIPLE_CLICKED`,
225 :const:`BUTTON_SHIFT`, :const:`BUTTON_CTRL`, :const:`BUTTON_ALT`.
226
227
228.. function:: getsyx()
229
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300230 Return the current coordinates of the virtual screen cursor as a tuple
231 ``(y, x)``. If :meth:`leaveok <window.leaveok>` is currently ``True``, then return ``(-1, -1)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000232
233
234.. function:: getwin(file)
235
Ezio Melottib6b73712011-06-26 13:38:11 +0300236 Read window related data stored in the file by an earlier :func:`putwin` call.
Georg Brandl116aa622007-08-15 14:28:22 +0000237 The routine then creates and initializes a new window using that data, returning
238 the new window object.
239
240
241.. function:: has_colors()
242
Ezio Melottib6b73712011-06-26 13:38:11 +0300243 Return ``True`` if the terminal can display colors; otherwise, return ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +0000244
245
246.. function:: has_ic()
247
Ezio Melottib6b73712011-06-26 13:38:11 +0300248 Return ``True`` if the terminal has insert- and delete-character capabilities.
Georg Brandl116aa622007-08-15 14:28:22 +0000249 This function is included for historical reasons only, as all modern software
250 terminal emulators have such capabilities.
251
252
253.. function:: has_il()
254
Ezio Melottib6b73712011-06-26 13:38:11 +0300255 Return ``True`` if the terminal has insert- and delete-line capabilities, or can
Georg Brandl116aa622007-08-15 14:28:22 +0000256 simulate them using scrolling regions. This function is included for
257 historical reasons only, as all modern software terminal emulators have such
258 capabilities.
259
260
261.. function:: has_key(ch)
262
Ezio Melottib6b73712011-06-26 13:38:11 +0300263 Take a key value *ch*, and return ``True`` if the current terminal type recognizes
Georg Brandl116aa622007-08-15 14:28:22 +0000264 a key with that value.
265
266
267.. function:: halfdelay(tenths)
268
269 Used for half-delay mode, which is similar to cbreak mode in that characters
270 typed by the user are immediately available to the program. However, after
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300271 blocking for *tenths* tenths of seconds, raise an exception if nothing has
Ezio Melottib6b73712011-06-26 13:38:11 +0300272 been typed. The value of *tenths* must be a number between ``1`` and ``255``. Use
Georg Brandl116aa622007-08-15 14:28:22 +0000273 :func:`nocbreak` to leave half-delay mode.
274
275
276.. function:: init_color(color_number, r, g, b)
277
Ezio Melottib6b73712011-06-26 13:38:11 +0300278 Change the definition of a color, taking the number of the color to be changed
Georg Brandl116aa622007-08-15 14:28:22 +0000279 followed by three RGB values (for the amounts of red, green, and blue
280 components). The value of *color_number* must be between ``0`` and
281 :const:`COLORS`. Each of *r*, *g*, *b*, must be a value between ``0`` and
282 ``1000``. When :func:`init_color` is used, all occurrences of that color on the
283 screen immediately change to the new definition. This function is a no-op on
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300284 most terminals; it is active only if :func:`can_change_color` returns ``True``.
Georg Brandl116aa622007-08-15 14:28:22 +0000285
286
287.. function:: init_pair(pair_number, fg, bg)
288
Ezio Melottib6b73712011-06-26 13:38:11 +0300289 Change the definition of a color-pair. It takes three arguments: the number of
Georg Brandl116aa622007-08-15 14:28:22 +0000290 the color-pair to be changed, the foreground color number, and the background
291 color number. The value of *pair_number* must be between ``1`` and
292 ``COLOR_PAIRS - 1`` (the ``0`` color pair is wired to white on black and cannot
293 be changed). The value of *fg* and *bg* arguments must be between ``0`` and
294 :const:`COLORS`. If the color-pair was previously initialized, the screen is
295 refreshed and all occurrences of that color-pair are changed to the new
296 definition.
297
298
299.. function:: initscr()
300
Berker Peksag93fc20b2017-05-23 03:16:07 +0300301 Initialize the library. Return a :ref:`window <curses-window-objects>` object
302 which represents the whole screen.
Georg Brandl116aa622007-08-15 14:28:22 +0000303
304 .. note::
305
306 If there is an error opening the terminal, the underlying curses library may
307 cause the interpreter to exit.
308
309
Ezio Melotti4850d522011-06-26 13:34:56 +0300310.. function:: is_term_resized(nlines, ncols)
311
312 Return ``True`` if :func:`resize_term` would modify the window structure,
313 ``False`` otherwise.
314
315
Georg Brandl116aa622007-08-15 14:28:22 +0000316.. function:: isendwin()
317
Ezio Melottib6b73712011-06-26 13:38:11 +0300318 Return ``True`` if :func:`endwin` has been called (that is, the curses library has
Georg Brandl116aa622007-08-15 14:28:22 +0000319 been deinitialized).
320
321
322.. function:: keyname(k)
323
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300324 Return the name of the key numbered *k* as a bytes object. The name of a key generating printable
Georg Brandl116aa622007-08-15 14:28:22 +0000325 ASCII character is the key's character. The name of a control-key combination
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300326 is a two-byte bytes object consisting of a caret (``b'^'``) followed by the corresponding
Serhiy Storchakac7b1a0b2016-11-26 13:43:28 +0200327 printable ASCII character. The name of an alt-key combination (128--255) is a
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300328 bytes object consisting of the prefix ``b'M-'`` followed by the name of the corresponding
Georg Brandl116aa622007-08-15 14:28:22 +0000329 ASCII character.
330
331
332.. function:: killchar()
333
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300334 Return the user's current line kill character as a one-byte bytes object. Under Unix operating systems
Georg Brandl116aa622007-08-15 14:28:22 +0000335 this is a property of the controlling tty of the curses program, and is not set
336 by the curses library itself.
337
338
339.. function:: longname()
340
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300341 Return a bytes object containing the terminfo long name field describing the current
Georg Brandl116aa622007-08-15 14:28:22 +0000342 terminal. The maximum length of a verbose description is 128 characters. It is
343 defined only after the call to :func:`initscr`.
344
345
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300346.. function:: meta(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000347
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300348 If *flag* is ``True``, allow 8-bit characters to be input. If
349 *flag* is ``False``, allow only 7-bit chars.
Georg Brandl116aa622007-08-15 14:28:22 +0000350
351
352.. function:: mouseinterval(interval)
353
Ezio Melottib6b73712011-06-26 13:38:11 +0300354 Set the maximum time in milliseconds that can elapse between press and release
355 events in order for them to be recognized as a click, and return the previous
Georg Brandl116aa622007-08-15 14:28:22 +0000356 interval value. The default value is 200 msec, or one fifth of a second.
357
358
359.. function:: mousemask(mousemask)
360
Ezio Melottib6b73712011-06-26 13:38:11 +0300361 Set the mouse events to be reported, and return a tuple ``(availmask,
Georg Brandl116aa622007-08-15 14:28:22 +0000362 oldmask)``. *availmask* indicates which of the specified mouse events can be
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300363 reported; on complete failure it returns ``0``. *oldmask* is the previous value of
Georg Brandl116aa622007-08-15 14:28:22 +0000364 the given window's mouse event mask. If this function is never called, no mouse
365 events are ever reported.
366
367
368.. function:: napms(ms)
369
370 Sleep for *ms* milliseconds.
371
372
373.. function:: newpad(nlines, ncols)
374
Ezio Melottib6b73712011-06-26 13:38:11 +0300375 Create and return a pointer to a new pad data structure with the given number
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300376 of lines and columns. Return a pad as a window object.
Georg Brandl116aa622007-08-15 14:28:22 +0000377
378 A pad is like a window, except that it is not restricted by the screen size, and
379 is not necessarily associated with a particular part of the screen. Pads can be
380 used when a large window is needed, and only a part of the window will be on the
381 screen at one time. Automatic refreshes of pads (such as from scrolling or
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300382 echoing of input) do not occur. The :meth:`~window.refresh` and :meth:`~window.noutrefresh`
Georg Brandl116aa622007-08-15 14:28:22 +0000383 methods of a pad require 6 arguments to specify the part of the pad to be
384 displayed and the location on the screen to be used for the display. The
Ezio Melottib6b73712011-06-26 13:38:11 +0300385 arguments are *pminrow*, *pmincol*, *sminrow*, *smincol*, *smaxrow*, *smaxcol*; the *p*
Georg Brandl116aa622007-08-15 14:28:22 +0000386 arguments refer to the upper left corner of the pad region to be displayed and
Ezio Melottib6b73712011-06-26 13:38:11 +0300387 the *s* arguments define a clipping box on the screen within which the pad region
Georg Brandl116aa622007-08-15 14:28:22 +0000388 is to be displayed.
389
390
Georg Brandl1ed80b02013-10-29 08:10:36 +0100391.. function:: newwin(nlines, ncols)
Ezio Melottie0add762012-09-14 06:32:35 +0300392 newwin(nlines, ncols, begin_y, begin_x)
Georg Brandl116aa622007-08-15 14:28:22 +0000393
Berker Peksag93fc20b2017-05-23 03:16:07 +0300394 Return a new :ref:`window <curses-window-objects>`, whose left-upper corner
395 is at ``(begin_y, begin_x)``, and whose height/width is *nlines*/*ncols*.
Georg Brandl116aa622007-08-15 14:28:22 +0000396
397 By default, the window will extend from the specified position to the lower
398 right corner of the screen.
399
400
401.. function:: nl()
402
403 Enter newline mode. This mode translates the return key into newline on input,
404 and translates newline into return and line-feed on output. Newline mode is
405 initially on.
406
407
408.. function:: nocbreak()
409
410 Leave cbreak mode. Return to normal "cooked" mode with line buffering.
411
412
413.. function:: noecho()
414
415 Leave echo mode. Echoing of input characters is turned off.
416
417
418.. function:: nonl()
419
420 Leave newline mode. Disable translation of return into newline on input, and
421 disable low-level translation of newline into newline/return on output (but this
422 does not change the behavior of ``addch('\n')``, which always does the
423 equivalent of return and line feed on the virtual screen). With translation
424 off, curses can sometimes speed up vertical motion a little; also, it will be
425 able to detect the return key on input.
426
427
428.. function:: noqiflush()
429
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300430 When the :func:`!noqiflush` routine is used, normal flush of input and output queues
431 associated with the ``INTR``, ``QUIT`` and ``SUSP`` characters will not be done. You may
432 want to call :func:`!noqiflush` in a signal handler if you want output to
Georg Brandl116aa622007-08-15 14:28:22 +0000433 continue as though the interrupt had not occurred, after the handler exits.
434
435
436.. function:: noraw()
437
438 Leave raw mode. Return to normal "cooked" mode with line buffering.
439
440
441.. function:: pair_content(pair_number)
442
Ezio Melottib6b73712011-06-26 13:38:11 +0300443 Return a tuple ``(fg, bg)`` containing the colors for the requested color pair.
Georg Brandl116aa622007-08-15 14:28:22 +0000444 The value of *pair_number* must be between ``1`` and ``COLOR_PAIRS - 1``.
445
446
447.. function:: pair_number(attr)
448
Ezio Melottib6b73712011-06-26 13:38:11 +0300449 Return the number of the color-pair set by the attribute value *attr*.
Georg Brandl116aa622007-08-15 14:28:22 +0000450 :func:`color_pair` is the counterpart to this function.
451
452
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300453.. function:: putp(str)
Georg Brandl116aa622007-08-15 14:28:22 +0000454
Ezio Melottib6b73712011-06-26 13:38:11 +0300455 Equivalent to ``tputs(str, 1, putchar)``; emit the value of a specified
456 terminfo capability for the current terminal. Note that the output of :func:`putp`
Georg Brandl116aa622007-08-15 14:28:22 +0000457 always goes to standard output.
458
459
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300460.. function:: qiflush([flag])
Georg Brandl116aa622007-08-15 14:28:22 +0000461
Ezio Melottib6b73712011-06-26 13:38:11 +0300462 If *flag* is ``False``, the effect is the same as calling :func:`noqiflush`. If
463 *flag* is ``True``, or no argument is provided, the queues will be flushed when
Georg Brandl116aa622007-08-15 14:28:22 +0000464 these control characters are read.
465
466
467.. function:: raw()
468
469 Enter raw mode. In raw mode, normal line buffering and processing of
470 interrupt, quit, suspend, and flow control keys are turned off; characters are
471 presented to curses input functions one by one.
472
473
474.. function:: reset_prog_mode()
475
Ezio Melottib6b73712011-06-26 13:38:11 +0300476 Restore the terminal to "program" mode, as previously saved by
Georg Brandl116aa622007-08-15 14:28:22 +0000477 :func:`def_prog_mode`.
478
479
480.. function:: reset_shell_mode()
481
Ezio Melottib6b73712011-06-26 13:38:11 +0300482 Restore the terminal to "shell" mode, as previously saved by
Georg Brandl116aa622007-08-15 14:28:22 +0000483 :func:`def_shell_mode`.
484
485
Ezio Melotti4850d522011-06-26 13:34:56 +0300486.. function:: resetty()
487
488 Restore the state of the terminal modes to what it was at the last call to
489 :func:`savetty`.
490
491
492.. function:: resize_term(nlines, ncols)
493
494 Backend function used by :func:`resizeterm`, performing most of the work;
495 when resizing the windows, :func:`resize_term` blank-fills the areas that are
496 extended. The calling application should fill in these areas with
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300497 appropriate data. The :func:`!resize_term` function attempts to resize all
Ezio Melotti4850d522011-06-26 13:34:56 +0300498 windows. However, due to the calling convention of pads, it is not possible
499 to resize these without additional interaction with the application.
500
501
502.. function:: resizeterm(nlines, ncols)
503
504 Resize the standard and current windows to the specified dimensions, and
505 adjusts other bookkeeping data used by the curses library that record the
506 window dimensions (in particular the SIGWINCH handler).
507
508
509.. function:: savetty()
510
511 Save the current state of the terminal modes in a buffer, usable by
512 :func:`resetty`.
513
Anthony Sottileb32cb972019-10-31 02:13:48 -0700514.. function:: get_escdelay()
515
516 Retrieves the value set by :func:`set_escdelay`.
517
518 .. versionadded:: 3.9
519
520.. function:: set_escdelay(ms)
521
522 Sets the number of milliseconds to wait after reading an escape character,
523 to distinguish between an individual escape character entered on the
524 keyboard from escape sequences sent by cursor and function keys.
525
526 .. versionadded:: 3.9
527
528.. function:: get_tabsize()
529
530 Retrieves the value set by :func:`set_tabsize`.
531
532 .. versionadded:: 3.9
533
534.. function:: set_tabsize(size)
535
536 Sets the number of columns used by the curses library when converting a tab
537 character to spaces as it adds the tab to a window.
538
539 .. versionadded:: 3.9
Ezio Melotti4850d522011-06-26 13:34:56 +0300540
Georg Brandl116aa622007-08-15 14:28:22 +0000541.. function:: setsyx(y, x)
542
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300543 Set the virtual screen cursor to *y*, *x*. If *y* and *x* are both ``-1``, then
544 :meth:`leaveok <window.leaveok>` is set ``True``.
Georg Brandl116aa622007-08-15 14:28:22 +0000545
546
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300547.. function:: setupterm(term=None, fd=-1)
Georg Brandl116aa622007-08-15 14:28:22 +0000548
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300549 Initialize the terminal. *term* is a string giving
550 the terminal name, or ``None``; if omitted or ``None``, the value of the
551 :envvar:`TERM` environment variable will be used. *fd* is the
Georg Brandl116aa622007-08-15 14:28:22 +0000552 file descriptor to which any initialization sequences will be sent; if not
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300553 supplied or ``-1``, the file descriptor for ``sys.stdout`` will be used.
Georg Brandl116aa622007-08-15 14:28:22 +0000554
555
556.. function:: start_color()
557
558 Must be called if the programmer wants to use colors, and before any other color
559 manipulation routine is called. It is good practice to call this routine right
560 after :func:`initscr`.
561
562 :func:`start_color` initializes eight basic colors (black, red, green, yellow,
563 blue, magenta, cyan, and white), and two global variables in the :mod:`curses`
564 module, :const:`COLORS` and :const:`COLOR_PAIRS`, containing the maximum number
565 of colors and color-pairs the terminal can support. It also restores the colors
566 on the terminal to the values they had when the terminal was just turned on.
567
568
569.. function:: termattrs()
570
Ezio Melottib6b73712011-06-26 13:38:11 +0300571 Return a logical OR of all video attributes supported by the terminal. This
Georg Brandl116aa622007-08-15 14:28:22 +0000572 information is useful when a curses program needs complete control over the
573 appearance of the screen.
574
575
576.. function:: termname()
577
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300578 Return the value of the environment variable :envvar:`TERM`, as a bytes object,
579 truncated to 14 characters.
Georg Brandl116aa622007-08-15 14:28:22 +0000580
581
582.. function:: tigetflag(capname)
583
Ezio Melottib6b73712011-06-26 13:38:11 +0300584 Return the value of the Boolean capability corresponding to the terminfo
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300585 capability name *capname* as an integer. Return the value ``-1`` if *capname* is not a
Georg Brandl116aa622007-08-15 14:28:22 +0000586 Boolean capability, or ``0`` if it is canceled or absent from the terminal
587 description.
588
589
590.. function:: tigetnum(capname)
591
Ezio Melottib6b73712011-06-26 13:38:11 +0300592 Return the value of the numeric capability corresponding to the terminfo
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300593 capability name *capname* as an integer. Return the value ``-2`` if *capname* is not a
Georg Brandl116aa622007-08-15 14:28:22 +0000594 numeric capability, or ``-1`` if it is canceled or absent from the terminal
595 description.
596
597
598.. function:: tigetstr(capname)
599
Ezio Melottib6b73712011-06-26 13:38:11 +0300600 Return the value of the string capability corresponding to the terminfo
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300601 capability name *capname* as a bytes object. Return ``None`` if *capname*
602 is not a terminfo "string capability", or is canceled or absent from the
603 terminal description.
Georg Brandl116aa622007-08-15 14:28:22 +0000604
605
Georg Brandlc2a4f4f2009-04-10 09:03:43 +0000606.. function:: tparm(str[, ...])
Georg Brandl116aa622007-08-15 14:28:22 +0000607
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300608 Instantiate the bytes object *str* with the supplied parameters, where *str* should
Ezio Melottib6b73712011-06-26 13:38:11 +0300609 be a parameterized string obtained from the terminfo database. E.g.
Victor Stinner26621332011-11-02 23:45:29 +0100610 ``tparm(tigetstr("cup"), 5, 3)`` could result in ``b'\033[6;4H'``, the exact
Georg Brandl116aa622007-08-15 14:28:22 +0000611 result depending on terminal type.
612
613
614.. function:: typeahead(fd)
615
Ezio Melottib6b73712011-06-26 13:38:11 +0300616 Specify that the file descriptor *fd* be used for typeahead checking. If *fd*
Georg Brandl116aa622007-08-15 14:28:22 +0000617 is ``-1``, then no typeahead checking is done.
618
619 The curses library does "line-breakout optimization" by looking for typeahead
620 periodically while updating the screen. If input is found, and it is coming
621 from a tty, the current update is postponed until refresh or doupdate is called
622 again, allowing faster response to commands typed in advance. This function
623 allows specifying a different file descriptor for typeahead checking.
624
625
626.. function:: unctrl(ch)
627
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300628 Return a bytes object which is a printable representation of the character *ch*.
629 Control characters are represented as a caret followed by the character, for
630 example as ``b'^C'``. Printing characters are left as they are.
Georg Brandl116aa622007-08-15 14:28:22 +0000631
632
633.. function:: ungetch(ch)
634
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300635 Push *ch* so the next :meth:`~window.getch` will return it.
Georg Brandl116aa622007-08-15 14:28:22 +0000636
637 .. note::
638
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300639 Only one *ch* can be pushed before :meth:`!getch` is called.
Georg Brandl116aa622007-08-15 14:28:22 +0000640
641
Steve Dowerd2bc3892015-04-15 18:06:05 -0400642.. function:: update_lines_cols()
643
644 Update :envvar:`LINES` and :envvar:`COLS`. Useful for detecting manual screen resize.
645
646 .. versionadded:: 3.5
647
648
Victor Stinner71e44cb2011-09-06 01:53:03 +0200649.. function:: unget_wch(ch)
650
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300651 Push *ch* so the next :meth:`~window.get_wch` will return it.
Victor Stinner71e44cb2011-09-06 01:53:03 +0200652
653 .. note::
654
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300655 Only one *ch* can be pushed before :meth:`!get_wch` is called.
Victor Stinner71e44cb2011-09-06 01:53:03 +0200656
657 .. versionadded:: 3.3
658
659
Georg Brandl116aa622007-08-15 14:28:22 +0000660.. function:: ungetmouse(id, x, y, z, bstate)
661
662 Push a :const:`KEY_MOUSE` event onto the input queue, associating the given
663 state data with it.
664
665
666.. function:: use_env(flag)
667
668 If used, this function should be called before :func:`initscr` or newterm are
Ezio Melottib6b73712011-06-26 13:38:11 +0300669 called. When *flag* is ``False``, the values of lines and columns specified in the
Georg Brandl116aa622007-08-15 14:28:22 +0000670 terminfo database will be used, even if environment variables :envvar:`LINES`
671 and :envvar:`COLUMNS` (used by default) are set, or if curses is running in a
672 window (in which case default behavior would be to use the window size if
673 :envvar:`LINES` and :envvar:`COLUMNS` are not set).
674
675
676.. function:: use_default_colors()
677
678 Allow use of default values for colors on terminals supporting this feature. Use
679 this to support transparency in your application. The default color is assigned
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300680 to the color number ``-1``. After calling this function, ``init_pair(x,
Georg Brandl116aa622007-08-15 14:28:22 +0000681 curses.COLOR_RED, -1)`` initializes, for instance, color pair *x* to a red
682 foreground color on the default background.
683
684
Serhiy Storchaka142566c2019-06-05 18:22:31 +0300685.. function:: wrapper(func, /, *args, **kwargs)
R David Murray409c32f2011-06-18 19:34:12 -0400686
687 Initialize curses and call another callable object, *func*, which should be the
688 rest of your curses-using application. If the application raises an exception,
689 this function will restore the terminal to a sane state before re-raising the
690 exception and generating a traceback. The callable object *func* is then passed
691 the main window 'stdscr' as its first argument, followed by any other arguments
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300692 passed to :func:`!wrapper`. Before calling *func*, :func:`!wrapper` turns on
R David Murray409c32f2011-06-18 19:34:12 -0400693 cbreak mode, turns off echo, enables the terminal keypad, and initializes colors
694 if the terminal has color support. On exit (whether normally or by exception)
695 it restores cooked mode, turns on echo, and disables the terminal keypad.
696
697
Georg Brandl116aa622007-08-15 14:28:22 +0000698.. _curses-window-objects:
699
700Window Objects
701--------------
702
703Window objects, as returned by :func:`initscr` and :func:`newwin` above, have
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100704the following methods and attributes:
Georg Brandl116aa622007-08-15 14:28:22 +0000705
706
Ezio Melottie0add762012-09-14 06:32:35 +0300707.. method:: window.addch(ch[, attr])
708 window.addch(y, x, ch[, attr])
Georg Brandl116aa622007-08-15 14:28:22 +0000709
Georg Brandl116aa622007-08-15 14:28:22 +0000710 Paint character *ch* at ``(y, x)`` with attributes *attr*, overwriting any
711 character previously painter at that location. By default, the character
712 position and attributes are the current settings for the window object.
713
Jay Crottsef5ce882018-04-06 20:27:07 -0500714 .. note::
715
716 Writing outside the window, subwindow, or pad raises a :exc:`curses.error`.
717 Attempting to write to the lower right corner of a window, subwindow,
718 or pad will cause an exception to be raised after the character is printed.
719
Georg Brandl116aa622007-08-15 14:28:22 +0000720
Ezio Melottie0add762012-09-14 06:32:35 +0300721.. method:: window.addnstr(str, n[, attr])
722 window.addnstr(y, x, str, n[, attr])
Georg Brandl116aa622007-08-15 14:28:22 +0000723
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300724 Paint at most *n* characters of the character string *str* at
725 ``(y, x)`` with attributes
Georg Brandl116aa622007-08-15 14:28:22 +0000726 *attr*, overwriting anything previously on the display.
727
728
Ezio Melottie0add762012-09-14 06:32:35 +0300729.. method:: window.addstr(str[, attr])
730 window.addstr(y, x, str[, attr])
Georg Brandl116aa622007-08-15 14:28:22 +0000731
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300732 Paint the character string *str* at ``(y, x)`` with attributes
733 *attr*, overwriting anything previously on the display.
Georg Brandl116aa622007-08-15 14:28:22 +0000734
Jay Crottsef5ce882018-04-06 20:27:07 -0500735 .. note::
736
Toshio Kuratomie7b11362019-05-17 18:54:02 -0400737 * Writing outside the window, subwindow, or pad raises :exc:`curses.error`.
738 Attempting to write to the lower right corner of a window, subwindow,
739 or pad will cause an exception to be raised after the string is printed.
740
741 * A `bug in ncurses <https://bugs.python.org/issue35924>`_, the backend
742 for this Python module, can cause SegFaults when resizing windows. This
743 is fixed in ncurses-6.1-20190511. If you are stuck with an earlier
744 ncurses, you can avoid triggering this if you do not call :func:`addstr`
745 with a *str* that has embedded newlines. Instead, call :func:`addstr`
746 separately for each line.
Jay Crottsef5ce882018-04-06 20:27:07 -0500747
Georg Brandl116aa622007-08-15 14:28:22 +0000748
749.. method:: window.attroff(attr)
750
751 Remove attribute *attr* from the "background" set applied to all writes to the
752 current window.
753
754
755.. method:: window.attron(attr)
756
757 Add attribute *attr* from the "background" set applied to all writes to the
758 current window.
759
760
761.. method:: window.attrset(attr)
762
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300763 Set the "background" set of attributes to *attr*. This set is initially
764 ``0`` (no attributes).
Georg Brandl116aa622007-08-15 14:28:22 +0000765
766
767.. method:: window.bkgd(ch[, attr])
768
Ezio Melottib6b73712011-06-26 13:38:11 +0300769 Set the background property of the window to the character *ch*, with
Georg Brandl116aa622007-08-15 14:28:22 +0000770 attributes *attr*. The change is then applied to every character position in
771 that window:
772
773 * The attribute of every character in the window is changed to the new
774 background attribute.
775
776 * Wherever the former background character appears, it is changed to the new
777 background character.
778
779
780.. method:: window.bkgdset(ch[, attr])
781
Ezio Melottib6b73712011-06-26 13:38:11 +0300782 Set the window's background. A window's background consists of a character and
Georg Brandl116aa622007-08-15 14:28:22 +0000783 any combination of attributes. The attribute part of the background is combined
784 (OR'ed) with all non-blank characters that are written into the window. Both
785 the character and attribute parts of the background are combined with the blank
786 characters. The background becomes a property of the character and moves with
787 the character through any scrolling and insert/delete line/character operations.
788
789
790.. method:: window.border([ls[, rs[, ts[, bs[, tl[, tr[, bl[, br]]]]]]]])
791
792 Draw a border around the edges of the window. Each parameter specifies the
793 character to use for a specific part of the border; see the table below for more
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300794 details.
Georg Brandl116aa622007-08-15 14:28:22 +0000795
796 .. note::
797
798 A ``0`` value for any parameter will cause the default character to be used for
799 that parameter. Keyword parameters can *not* be used. The defaults are listed
800 in this table:
801
802 +-----------+---------------------+-----------------------+
803 | Parameter | Description | Default value |
804 +===========+=====================+=======================+
805 | *ls* | Left side | :const:`ACS_VLINE` |
806 +-----------+---------------------+-----------------------+
807 | *rs* | Right side | :const:`ACS_VLINE` |
808 +-----------+---------------------+-----------------------+
809 | *ts* | Top | :const:`ACS_HLINE` |
810 +-----------+---------------------+-----------------------+
811 | *bs* | Bottom | :const:`ACS_HLINE` |
812 +-----------+---------------------+-----------------------+
813 | *tl* | Upper-left corner | :const:`ACS_ULCORNER` |
814 +-----------+---------------------+-----------------------+
815 | *tr* | Upper-right corner | :const:`ACS_URCORNER` |
816 +-----------+---------------------+-----------------------+
817 | *bl* | Bottom-left corner | :const:`ACS_LLCORNER` |
818 +-----------+---------------------+-----------------------+
819 | *br* | Bottom-right corner | :const:`ACS_LRCORNER` |
820 +-----------+---------------------+-----------------------+
821
822
823.. method:: window.box([vertch, horch])
824
825 Similar to :meth:`border`, but both *ls* and *rs* are *vertch* and both *ts* and
Ezio Melottib6b73712011-06-26 13:38:11 +0300826 *bs* are *horch*. The default corner characters are always used by this function.
Georg Brandl116aa622007-08-15 14:28:22 +0000827
828
Ezio Melottie0add762012-09-14 06:32:35 +0300829.. method:: window.chgat(attr)
830 window.chgat(num, attr)
831 window.chgat(y, x, attr)
832 window.chgat(y, x, num, attr)
Georg Brandl116aa622007-08-15 14:28:22 +0000833
Ezio Melottib6b73712011-06-26 13:38:11 +0300834 Set the attributes of *num* characters at the current cursor position, or at
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300835 position ``(y, x)`` if supplied. If *num* is not given or is ``-1``,
Chillar Anandb838cc32017-11-04 13:43:16 +0530836 the attribute will be set on all the characters to the end of the line. This
837 function moves cursor to position ``(y, x)`` if supplied. The changed line
838 will be touched using the :meth:`touchline` method so that the contents will
839 be redisplayed by the next window refresh.
Georg Brandl116aa622007-08-15 14:28:22 +0000840
841
842.. method:: window.clear()
843
Ezio Melottib6b73712011-06-26 13:38:11 +0300844 Like :meth:`erase`, but also cause the whole window to be repainted upon next
Georg Brandl116aa622007-08-15 14:28:22 +0000845 call to :meth:`refresh`.
846
847
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300848.. method:: window.clearok(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000849
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300850 If *flag* is ``True``, the next call to :meth:`refresh` will clear the window
Georg Brandl116aa622007-08-15 14:28:22 +0000851 completely.
852
853
854.. method:: window.clrtobot()
855
856 Erase from cursor to the end of the window: all lines below the cursor are
857 deleted, and then the equivalent of :meth:`clrtoeol` is performed.
858
859
860.. method:: window.clrtoeol()
861
862 Erase from cursor to the end of the line.
863
864
865.. method:: window.cursyncup()
866
Ezio Melottib6b73712011-06-26 13:38:11 +0300867 Update the current cursor position of all the ancestors of the window to
Georg Brandl116aa622007-08-15 14:28:22 +0000868 reflect the current cursor position of the window.
869
870
871.. method:: window.delch([y, x])
872
873 Delete any character at ``(y, x)``.
874
875
876.. method:: window.deleteln()
877
Ezio Melottib6b73712011-06-26 13:38:11 +0300878 Delete the line under the cursor. All following lines are moved up by one line.
Georg Brandl116aa622007-08-15 14:28:22 +0000879
880
Ezio Melottie0add762012-09-14 06:32:35 +0300881.. method:: window.derwin(begin_y, begin_x)
882 window.derwin(nlines, ncols, begin_y, begin_x)
Georg Brandl116aa622007-08-15 14:28:22 +0000883
884 An abbreviation for "derive window", :meth:`derwin` is the same as calling
885 :meth:`subwin`, except that *begin_y* and *begin_x* are relative to the origin
Ezio Melottib6b73712011-06-26 13:38:11 +0300886 of the window, rather than relative to the entire screen. Return a window
Georg Brandl116aa622007-08-15 14:28:22 +0000887 object for the derived window.
888
889
890.. method:: window.echochar(ch[, attr])
891
892 Add character *ch* with attribute *attr*, and immediately call :meth:`refresh`
893 on the window.
894
895
896.. method:: window.enclose(y, x)
897
Ezio Melottib6b73712011-06-26 13:38:11 +0300898 Test whether the given pair of screen-relative character-cell coordinates are
899 enclosed by the given window, returning ``True`` or ``False``. It is useful for
Georg Brandl116aa622007-08-15 14:28:22 +0000900 determining what subset of the screen windows enclose the location of a mouse
901 event.
902
903
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100904.. attribute:: window.encoding
905
906 Encoding used to encode method arguments (Unicode strings and characters).
Ross Lagerwallce66a3e2012-09-06 18:58:43 +0200907 The encoding attribute is inherited from the parent window when a subwindow
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100908 is created, for example with :meth:`window.subwin`. By default, the locale
909 encoding is used (see :func:`locale.getpreferredencoding`).
910
911 .. versionadded:: 3.3
912
913
Georg Brandl116aa622007-08-15 14:28:22 +0000914.. method:: window.erase()
915
916 Clear the window.
917
918
919.. method:: window.getbegyx()
920
921 Return a tuple ``(y, x)`` of co-ordinates of upper-left corner.
922
923
Ezio Melotti4850d522011-06-26 13:34:56 +0300924.. method:: window.getbkgd()
925
926 Return the given window's current background character/attribute pair.
927
928
Georg Brandl116aa622007-08-15 14:28:22 +0000929.. method:: window.getch([y, x])
930
931 Get a character. Note that the integer returned does *not* have to be in ASCII
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300932 range: function keys, keypad keys and so on are represented by numbers higher
933 than 255. In no-delay mode, return ``-1`` if there is no input, otherwise
934 wait until a key is pressed.
Georg Brandl116aa622007-08-15 14:28:22 +0000935
936
Victor Stinnera7878b72011-07-14 23:07:44 +0200937.. method:: window.get_wch([y, x])
938
Victor Stinner1d39cde2012-08-29 01:40:57 +0200939 Get a wide character. Return a character for most keys, or an integer for
940 function keys, keypad keys, and other special keys.
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300941 In no-delay mode, raise an exception if there is no input.
Victor Stinnera7878b72011-07-14 23:07:44 +0200942
943 .. versionadded:: 3.3
944
945
Georg Brandl116aa622007-08-15 14:28:22 +0000946.. method:: window.getkey([y, x])
947
948 Get a character, returning a string instead of an integer, as :meth:`getch`
Victor Stinner1d39cde2012-08-29 01:40:57 +0200949 does. Function keys, keypad keys and other special keys return a multibyte
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300950 string containing the key name. In no-delay mode, raise an exception if
Victor Stinner1d39cde2012-08-29 01:40:57 +0200951 there is no input.
Georg Brandl116aa622007-08-15 14:28:22 +0000952
953
954.. method:: window.getmaxyx()
955
956 Return a tuple ``(y, x)`` of the height and width of the window.
957
958
959.. method:: window.getparyx()
960
Ezio Melottib6b73712011-06-26 13:38:11 +0300961 Return the beginning coordinates of this window relative to its parent window
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300962 as a tuple ``(y, x)``. Return ``(-1, -1)`` if this window has no
Georg Brandl116aa622007-08-15 14:28:22 +0000963 parent.
964
965
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300966.. method:: window.getstr()
967 window.getstr(n)
968 window.getstr(y, x)
969 window.getstr(y, x, n)
Georg Brandl116aa622007-08-15 14:28:22 +0000970
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300971 Read a bytes object from the user, with primitive line editing capacity.
Georg Brandl116aa622007-08-15 14:28:22 +0000972
973
974.. method:: window.getyx()
975
976 Return a tuple ``(y, x)`` of current cursor position relative to the window's
977 upper-left corner.
978
979
Ezio Melottie0add762012-09-14 06:32:35 +0300980.. method:: window.hline(ch, n)
981 window.hline(y, x, ch, n)
Georg Brandl116aa622007-08-15 14:28:22 +0000982
983 Display a horizontal line starting at ``(y, x)`` with length *n* consisting of
984 the character *ch*.
985
986
987.. method:: window.idcok(flag)
988
Ezio Melottib6b73712011-06-26 13:38:11 +0300989 If *flag* is ``False``, curses no longer considers using the hardware insert/delete
990 character feature of the terminal; if *flag* is ``True``, use of character insertion
Georg Brandl116aa622007-08-15 14:28:22 +0000991 and deletion is enabled. When curses is first initialized, use of character
992 insert/delete is enabled by default.
993
994
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300995.. method:: window.idlok(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000996
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300997 If *flag* is ``True``, :mod:`curses` will try and use hardware line
Georg Brandl116aa622007-08-15 14:28:22 +0000998 editing facilities. Otherwise, line insertion/deletion are disabled.
999
1000
1001.. method:: window.immedok(flag)
1002
Ezio Melottib6b73712011-06-26 13:38:11 +03001003 If *flag* is ``True``, any change in the window image automatically causes the
Georg Brandl116aa622007-08-15 14:28:22 +00001004 window to be refreshed; you no longer have to call :meth:`refresh` yourself.
1005 However, it may degrade performance considerably, due to repeated calls to
1006 wrefresh. This option is disabled by default.
1007
1008
1009.. method:: window.inch([y, x])
1010
1011 Return the character at the given position in the window. The bottom 8 bits are
1012 the character proper, and upper bits are the attributes.
1013
1014
Ezio Melottie0add762012-09-14 06:32:35 +03001015.. method:: window.insch(ch[, attr])
1016 window.insch(y, x, ch[, attr])
Georg Brandl116aa622007-08-15 14:28:22 +00001017
1018 Paint character *ch* at ``(y, x)`` with attributes *attr*, moving the line from
1019 position *x* right by one character.
1020
1021
1022.. method:: window.insdelln(nlines)
1023
Ezio Melottib6b73712011-06-26 13:38:11 +03001024 Insert *nlines* lines into the specified window above the current line. The
Georg Brandl116aa622007-08-15 14:28:22 +00001025 *nlines* bottom lines are lost. For negative *nlines*, delete *nlines* lines
1026 starting with the one under the cursor, and move the remaining lines up. The
1027 bottom *nlines* lines are cleared. The current cursor position remains the
1028 same.
1029
1030
1031.. method:: window.insertln()
1032
Ezio Melottib6b73712011-06-26 13:38:11 +03001033 Insert a blank line under the cursor. All following lines are moved down by one
Georg Brandl116aa622007-08-15 14:28:22 +00001034 line.
1035
1036
Ezio Melottie0add762012-09-14 06:32:35 +03001037.. method:: window.insnstr(str, n[, attr])
1038 window.insnstr(y, x, str, n[, attr])
Georg Brandl116aa622007-08-15 14:28:22 +00001039
1040 Insert a character string (as many characters as will fit on the line) before
1041 the character under the cursor, up to *n* characters. If *n* is zero or
1042 negative, the entire string is inserted. All characters to the right of the
1043 cursor are shifted right, with the rightmost characters on the line being lost.
1044 The cursor position does not change (after moving to *y*, *x*, if specified).
1045
1046
Ezio Melottie0add762012-09-14 06:32:35 +03001047.. method:: window.insstr(str[, attr])
1048 window.insstr(y, x, str[, attr])
Georg Brandl116aa622007-08-15 14:28:22 +00001049
1050 Insert a character string (as many characters as will fit on the line) before
1051 the character under the cursor. All characters to the right of the cursor are
1052 shifted right, with the rightmost characters on the line being lost. The cursor
1053 position does not change (after moving to *y*, *x*, if specified).
1054
1055
Ezio Melottie0add762012-09-14 06:32:35 +03001056.. method:: window.instr([n])
1057 window.instr(y, x[, n])
Georg Brandl116aa622007-08-15 14:28:22 +00001058
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001059 Return a bytes object of characters, extracted from the window starting at the
Georg Brandl116aa622007-08-15 14:28:22 +00001060 current cursor position, or at *y*, *x* if specified. Attributes are stripped
Ezio Melottib6b73712011-06-26 13:38:11 +03001061 from the characters. If *n* is specified, :meth:`instr` returns a string
Georg Brandl116aa622007-08-15 14:28:22 +00001062 at most *n* characters long (exclusive of the trailing NUL).
1063
1064
1065.. method:: window.is_linetouched(line)
1066
Ezio Melottib6b73712011-06-26 13:38:11 +03001067 Return ``True`` if the specified line was modified since the last call to
1068 :meth:`refresh`; otherwise return ``False``. Raise a :exc:`curses.error`
Georg Brandl116aa622007-08-15 14:28:22 +00001069 exception if *line* is not valid for the given window.
1070
1071
1072.. method:: window.is_wintouched()
1073
Ezio Melottib6b73712011-06-26 13:38:11 +03001074 Return ``True`` if the specified window was modified since the last call to
1075 :meth:`refresh`; otherwise return ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +00001076
1077
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001078.. method:: window.keypad(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001079
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001080 If *flag* is ``True``, escape sequences generated by some keys (keypad, function keys)
1081 will be interpreted by :mod:`curses`. If *flag* is ``False``, escape sequences will be
Georg Brandl116aa622007-08-15 14:28:22 +00001082 left as is in the input stream.
1083
1084
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001085.. method:: window.leaveok(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001086
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001087 If *flag* is ``True``, cursor is left where it is on update, instead of being at "cursor
Georg Brandl116aa622007-08-15 14:28:22 +00001088 position." This reduces cursor movement where possible. If possible the cursor
1089 will be made invisible.
1090
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001091 If *flag* is ``False``, cursor will always be at "cursor position" after an update.
Georg Brandl116aa622007-08-15 14:28:22 +00001092
1093
1094.. method:: window.move(new_y, new_x)
1095
1096 Move cursor to ``(new_y, new_x)``.
1097
1098
1099.. method:: window.mvderwin(y, x)
1100
Ezio Melottib6b73712011-06-26 13:38:11 +03001101 Move the window inside its parent window. The screen-relative parameters of
Georg Brandl116aa622007-08-15 14:28:22 +00001102 the window are not changed. This routine is used to display different parts of
1103 the parent window at the same physical position on the screen.
1104
1105
1106.. method:: window.mvwin(new_y, new_x)
1107
1108 Move the window so its upper-left corner is at ``(new_y, new_x)``.
1109
1110
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001111.. method:: window.nodelay(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001112
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001113 If *flag* is ``True``, :meth:`getch` will be non-blocking.
Georg Brandl116aa622007-08-15 14:28:22 +00001114
1115
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001116.. method:: window.notimeout(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001117
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001118 If *flag* is ``True``, escape sequences will not be timed out.
Georg Brandl116aa622007-08-15 14:28:22 +00001119
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001120 If *flag* is ``False``, after a few milliseconds, an escape sequence will not be
Georg Brandl116aa622007-08-15 14:28:22 +00001121 interpreted, and will be left in the input stream as is.
1122
1123
1124.. method:: window.noutrefresh()
1125
1126 Mark for refresh but wait. This function updates the data structure
1127 representing the desired state of the window, but does not force an update of
1128 the physical screen. To accomplish that, call :func:`doupdate`.
1129
1130
1131.. method:: window.overlay(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])
1132
1133 Overlay the window on top of *destwin*. The windows need not be the same size,
1134 only the overlapping region is copied. This copy is non-destructive, which means
1135 that the current background character does not overwrite the old contents of
1136 *destwin*.
1137
1138 To get fine-grained control over the copied region, the second form of
1139 :meth:`overlay` can be used. *sminrow* and *smincol* are the upper-left
1140 coordinates of the source window, and the other variables mark a rectangle in
1141 the destination window.
1142
1143
1144.. method:: window.overwrite(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])
1145
1146 Overwrite the window on top of *destwin*. The windows need not be the same size,
1147 in which case only the overlapping region is copied. This copy is destructive,
1148 which means that the current background character overwrites the old contents of
1149 *destwin*.
1150
1151 To get fine-grained control over the copied region, the second form of
1152 :meth:`overwrite` can be used. *sminrow* and *smincol* are the upper-left
1153 coordinates of the source window, the other variables mark a rectangle in the
1154 destination window.
1155
1156
1157.. method:: window.putwin(file)
1158
Ezio Melottib6b73712011-06-26 13:38:11 +03001159 Write all data associated with the window into the provided file object. This
Georg Brandl116aa622007-08-15 14:28:22 +00001160 information can be later retrieved using the :func:`getwin` function.
1161
1162
1163.. method:: window.redrawln(beg, num)
1164
Ezio Melottib6b73712011-06-26 13:38:11 +03001165 Indicate that the *num* screen lines, starting at line *beg*, are corrupted and
Georg Brandl116aa622007-08-15 14:28:22 +00001166 should be completely redrawn on the next :meth:`refresh` call.
1167
1168
1169.. method:: window.redrawwin()
1170
Ezio Melottib6b73712011-06-26 13:38:11 +03001171 Touch the entire window, causing it to be completely redrawn on the next
Georg Brandl116aa622007-08-15 14:28:22 +00001172 :meth:`refresh` call.
1173
1174
1175.. method:: window.refresh([pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol])
1176
1177 Update the display immediately (sync actual screen with previous
1178 drawing/deleting methods).
1179
1180 The 6 optional arguments can only be specified when the window is a pad created
1181 with :func:`newpad`. The additional parameters are needed to indicate what part
1182 of the pad and screen are involved. *pminrow* and *pmincol* specify the upper
1183 left-hand corner of the rectangle to be displayed in the pad. *sminrow*,
1184 *smincol*, *smaxrow*, and *smaxcol* specify the edges of the rectangle to be
1185 displayed on the screen. The lower right-hand corner of the rectangle to be
1186 displayed in the pad is calculated from the screen coordinates, since the
1187 rectangles must be the same size. Both rectangles must be entirely contained
1188 within their respective structures. Negative values of *pminrow*, *pmincol*,
1189 *sminrow*, or *smincol* are treated as if they were zero.
1190
1191
Ezio Melotti4850d522011-06-26 13:34:56 +03001192.. method:: window.resize(nlines, ncols)
1193
1194 Reallocate storage for a curses window to adjust its dimensions to the
1195 specified values. If either dimension is larger than the current values, the
1196 window's data is filled with blanks that have the current background
1197 rendition (as set by :meth:`bkgdset`) merged into them.
1198
1199
Hynek Schlawack979f37a2012-05-22 16:12:18 +02001200.. method:: window.scroll([lines=1])
Georg Brandl116aa622007-08-15 14:28:22 +00001201
1202 Scroll the screen or scrolling region upward by *lines* lines.
1203
1204
1205.. method:: window.scrollok(flag)
1206
Ezio Melottib6b73712011-06-26 13:38:11 +03001207 Control what happens when the cursor of a window is moved off the edge of the
Georg Brandl116aa622007-08-15 14:28:22 +00001208 window or scrolling region, either as a result of a newline action on the bottom
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001209 line, or typing the last character of the last line. If *flag* is ``False``, the
1210 cursor is left on the bottom line. If *flag* is ``True``, the window is scrolled up
Georg Brandl116aa622007-08-15 14:28:22 +00001211 one line. Note that in order to get the physical scrolling effect on the
1212 terminal, it is also necessary to call :meth:`idlok`.
1213
1214
1215.. method:: window.setscrreg(top, bottom)
1216
1217 Set the scrolling region from line *top* to line *bottom*. All scrolling actions
1218 will take place in this region.
1219
1220
1221.. method:: window.standend()
1222
1223 Turn off the standout attribute. On some terminals this has the side effect of
1224 turning off all attributes.
1225
1226
1227.. method:: window.standout()
1228
1229 Turn on attribute *A_STANDOUT*.
1230
1231
Ezio Melottie0add762012-09-14 06:32:35 +03001232.. method:: window.subpad(begin_y, begin_x)
1233 window.subpad(nlines, ncols, begin_y, begin_x)
Georg Brandl116aa622007-08-15 14:28:22 +00001234
1235 Return a sub-window, whose upper-left corner is at ``(begin_y, begin_x)``, and
1236 whose width/height is *ncols*/*nlines*.
1237
1238
Ezio Melottie0add762012-09-14 06:32:35 +03001239.. method:: window.subwin(begin_y, begin_x)
1240 window.subwin(nlines, ncols, begin_y, begin_x)
Georg Brandl116aa622007-08-15 14:28:22 +00001241
1242 Return a sub-window, whose upper-left corner is at ``(begin_y, begin_x)``, and
1243 whose width/height is *ncols*/*nlines*.
1244
1245 By default, the sub-window will extend from the specified position to the lower
1246 right corner of the window.
1247
1248
1249.. method:: window.syncdown()
1250
Ezio Melottib6b73712011-06-26 13:38:11 +03001251 Touch each location in the window that has been touched in any of its ancestor
Georg Brandl116aa622007-08-15 14:28:22 +00001252 windows. This routine is called by :meth:`refresh`, so it should almost never
1253 be necessary to call it manually.
1254
1255
1256.. method:: window.syncok(flag)
1257
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001258 If *flag* is ``True``, then :meth:`syncup` is called automatically
Georg Brandl116aa622007-08-15 14:28:22 +00001259 whenever there is a change in the window.
1260
1261
1262.. method:: window.syncup()
1263
Ezio Melottib6b73712011-06-26 13:38:11 +03001264 Touch all locations in ancestors of the window that have been changed in the
Georg Brandl116aa622007-08-15 14:28:22 +00001265 window.
1266
1267
1268.. method:: window.timeout(delay)
1269
Ezio Melottib6b73712011-06-26 13:38:11 +03001270 Set blocking or non-blocking read behavior for the window. If *delay* is
Georg Brandl116aa622007-08-15 14:28:22 +00001271 negative, blocking read is used (which will wait indefinitely for input). If
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001272 *delay* is zero, then non-blocking read is used, and :meth:`getch` will
1273 return ``-1`` if no input is waiting. If *delay* is positive, then
1274 :meth:`getch` will block for *delay* milliseconds, and return ``-1`` if there is
Georg Brandl116aa622007-08-15 14:28:22 +00001275 still no input at the end of that time.
1276
1277
1278.. method:: window.touchline(start, count[, changed])
1279
1280 Pretend *count* lines have been changed, starting with line *start*. If
1281 *changed* is supplied, it specifies whether the affected lines are marked as
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001282 having been changed (*changed*\ ``=True``) or unchanged (*changed*\ ``=False``).
Georg Brandl116aa622007-08-15 14:28:22 +00001283
1284
1285.. method:: window.touchwin()
1286
1287 Pretend the whole window has been changed, for purposes of drawing
1288 optimizations.
1289
1290
1291.. method:: window.untouchwin()
1292
Ezio Melottib6b73712011-06-26 13:38:11 +03001293 Mark all lines in the window as unchanged since the last call to
Georg Brandl116aa622007-08-15 14:28:22 +00001294 :meth:`refresh`.
1295
1296
Ezio Melottie0add762012-09-14 06:32:35 +03001297.. method:: window.vline(ch, n)
1298 window.vline(y, x, ch, n)
Georg Brandl116aa622007-08-15 14:28:22 +00001299
1300 Display a vertical line starting at ``(y, x)`` with length *n* consisting of the
1301 character *ch*.
1302
1303
1304Constants
1305---------
1306
1307The :mod:`curses` module defines the following data members:
1308
1309
1310.. data:: ERR
1311
Anthony Sottilea26ace12019-09-13 23:17:09 -07001312 Some curses routines that return an integer, such as :meth:`~window.getch`, return
Georg Brandl116aa622007-08-15 14:28:22 +00001313 :const:`ERR` upon failure.
1314
1315
1316.. data:: OK
1317
1318 Some curses routines that return an integer, such as :func:`napms`, return
1319 :const:`OK` upon success.
1320
1321
1322.. data:: version
1323
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001324 A bytes object representing the current version of the module. Also available as
Georg Brandl116aa622007-08-15 14:28:22 +00001325 :const:`__version__`.
1326
Serhiy Storchakab232df92018-10-30 13:22:42 +02001327
1328.. data:: ncurses_version
1329
1330 A named tuple containing the three components of the ncurses library
1331 version: *major*, *minor*, and *patch*. All values are integers. The
1332 components can also be accessed by name, so ``curses.ncurses_version[0]``
1333 is equivalent to ``curses.ncurses_version.major`` and so on.
1334
1335 Availability: if the ncurses library is used.
1336
1337 .. versionadded:: 3.8
1338
1339
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001340Some constants are available to specify character cell attributes.
1341The exact constants available are system dependent.
Georg Brandl116aa622007-08-15 14:28:22 +00001342
1343+------------------+-------------------------------+
1344| Attribute | Meaning |
1345+==================+===============================+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001346| ``A_ALTCHARSET`` | Alternate character set mode |
Georg Brandl116aa622007-08-15 14:28:22 +00001347+------------------+-------------------------------+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001348| ``A_BLINK`` | Blink mode |
Georg Brandl116aa622007-08-15 14:28:22 +00001349+------------------+-------------------------------+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001350| ``A_BOLD`` | Bold mode |
Georg Brandl116aa622007-08-15 14:28:22 +00001351+------------------+-------------------------------+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001352| ``A_DIM`` | Dim mode |
Eijebongab7886b2017-04-26 17:17:12 +02001353+------------------+-------------------------------+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001354| ``A_INVIS`` | Invisible or blank mode |
Georg Brandl116aa622007-08-15 14:28:22 +00001355+------------------+-------------------------------+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001356| ``A_ITALIC`` | Italic mode |
1357+------------------+-------------------------------+
1358| ``A_NORMAL`` | Normal attribute |
1359+------------------+-------------------------------+
1360| ``A_PROTECT`` | Protected mode |
Georg Brandl116aa622007-08-15 14:28:22 +00001361+------------------+-------------------------------+
Georg Brandl931e5c12011-03-06 11:08:35 +01001362| ``A_REVERSE`` | Reverse background and |
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001363| | foreground colors |
Georg Brandl931e5c12011-03-06 11:08:35 +01001364+------------------+-------------------------------+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001365| ``A_STANDOUT`` | Standout mode |
Georg Brandl116aa622007-08-15 14:28:22 +00001366+------------------+-------------------------------+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001367| ``A_UNDERLINE`` | Underline mode |
1368+------------------+-------------------------------+
1369| ``A_HORIZONTAL`` | Horizontal highlight |
1370+------------------+-------------------------------+
1371| ``A_LEFT`` | Left highlight |
1372+------------------+-------------------------------+
1373| ``A_LOW`` | Low highlight |
1374+------------------+-------------------------------+
1375| ``A_RIGHT`` | Right highlight |
1376+------------------+-------------------------------+
1377| ``A_TOP`` | Top highlight |
1378+------------------+-------------------------------+
1379| ``A_VERTICAL`` | Vertical highlight |
1380+------------------+-------------------------------+
1381| ``A_CHARTEXT`` | Bit-mask to extract a |
1382| | character |
Georg Brandl116aa622007-08-15 14:28:22 +00001383+------------------+-------------------------------+
1384
Eijebongab7886b2017-04-26 17:17:12 +02001385.. versionadded:: 3.7
1386 ``A_ITALIC`` was added.
1387
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001388Several constants are available to extract corresponding attributes returned
1389by some methods.
1390
1391+------------------+-------------------------------+
1392| Bit-mask | Meaning |
1393+==================+===============================+
1394| ``A_ATTRIBUTES`` | Bit-mask to extract |
1395| | attributes |
1396+------------------+-------------------------------+
1397| ``A_CHARTEXT`` | Bit-mask to extract a |
1398| | character |
1399+------------------+-------------------------------+
1400| ``A_COLOR`` | Bit-mask to extract |
1401| | color-pair field information |
1402+------------------+-------------------------------+
1403
Georg Brandl116aa622007-08-15 14:28:22 +00001404Keys are referred to by integer constants with names starting with ``KEY_``.
1405The exact keycaps available are system dependent.
1406
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001407.. XXX this table is far too large! should it be alphabetized?
Georg Brandl116aa622007-08-15 14:28:22 +00001408
1409+-------------------+--------------------------------------------+
1410| Key constant | Key |
1411+===================+============================================+
1412| ``KEY_MIN`` | Minimum key value |
1413+-------------------+--------------------------------------------+
1414| ``KEY_BREAK`` | Break key (unreliable) |
1415+-------------------+--------------------------------------------+
1416| ``KEY_DOWN`` | Down-arrow |
1417+-------------------+--------------------------------------------+
1418| ``KEY_UP`` | Up-arrow |
1419+-------------------+--------------------------------------------+
1420| ``KEY_LEFT`` | Left-arrow |
1421+-------------------+--------------------------------------------+
1422| ``KEY_RIGHT`` | Right-arrow |
1423+-------------------+--------------------------------------------+
1424| ``KEY_HOME`` | Home key (upward+left arrow) |
1425+-------------------+--------------------------------------------+
1426| ``KEY_BACKSPACE`` | Backspace (unreliable) |
1427+-------------------+--------------------------------------------+
1428| ``KEY_F0`` | Function keys. Up to 64 function keys are |
1429| | supported. |
1430+-------------------+--------------------------------------------+
1431| ``KEY_Fn`` | Value of function key *n* |
1432+-------------------+--------------------------------------------+
1433| ``KEY_DL`` | Delete line |
1434+-------------------+--------------------------------------------+
1435| ``KEY_IL`` | Insert line |
1436+-------------------+--------------------------------------------+
1437| ``KEY_DC`` | Delete character |
1438+-------------------+--------------------------------------------+
1439| ``KEY_IC`` | Insert char or enter insert mode |
1440+-------------------+--------------------------------------------+
1441| ``KEY_EIC`` | Exit insert char mode |
1442+-------------------+--------------------------------------------+
1443| ``KEY_CLEAR`` | Clear screen |
1444+-------------------+--------------------------------------------+
1445| ``KEY_EOS`` | Clear to end of screen |
1446+-------------------+--------------------------------------------+
1447| ``KEY_EOL`` | Clear to end of line |
1448+-------------------+--------------------------------------------+
1449| ``KEY_SF`` | Scroll 1 line forward |
1450+-------------------+--------------------------------------------+
1451| ``KEY_SR`` | Scroll 1 line backward (reverse) |
1452+-------------------+--------------------------------------------+
1453| ``KEY_NPAGE`` | Next page |
1454+-------------------+--------------------------------------------+
1455| ``KEY_PPAGE`` | Previous page |
1456+-------------------+--------------------------------------------+
1457| ``KEY_STAB`` | Set tab |
1458+-------------------+--------------------------------------------+
1459| ``KEY_CTAB`` | Clear tab |
1460+-------------------+--------------------------------------------+
1461| ``KEY_CATAB`` | Clear all tabs |
1462+-------------------+--------------------------------------------+
1463| ``KEY_ENTER`` | Enter or send (unreliable) |
1464+-------------------+--------------------------------------------+
1465| ``KEY_SRESET`` | Soft (partial) reset (unreliable) |
1466+-------------------+--------------------------------------------+
1467| ``KEY_RESET`` | Reset or hard reset (unreliable) |
1468+-------------------+--------------------------------------------+
1469| ``KEY_PRINT`` | Print |
1470+-------------------+--------------------------------------------+
1471| ``KEY_LL`` | Home down or bottom (lower left) |
1472+-------------------+--------------------------------------------+
1473| ``KEY_A1`` | Upper left of keypad |
1474+-------------------+--------------------------------------------+
1475| ``KEY_A3`` | Upper right of keypad |
1476+-------------------+--------------------------------------------+
1477| ``KEY_B2`` | Center of keypad |
1478+-------------------+--------------------------------------------+
1479| ``KEY_C1`` | Lower left of keypad |
1480+-------------------+--------------------------------------------+
1481| ``KEY_C3`` | Lower right of keypad |
1482+-------------------+--------------------------------------------+
1483| ``KEY_BTAB`` | Back tab |
1484+-------------------+--------------------------------------------+
1485| ``KEY_BEG`` | Beg (beginning) |
1486+-------------------+--------------------------------------------+
1487| ``KEY_CANCEL`` | Cancel |
1488+-------------------+--------------------------------------------+
1489| ``KEY_CLOSE`` | Close |
1490+-------------------+--------------------------------------------+
1491| ``KEY_COMMAND`` | Cmd (command) |
1492+-------------------+--------------------------------------------+
1493| ``KEY_COPY`` | Copy |
1494+-------------------+--------------------------------------------+
1495| ``KEY_CREATE`` | Create |
1496+-------------------+--------------------------------------------+
1497| ``KEY_END`` | End |
1498+-------------------+--------------------------------------------+
1499| ``KEY_EXIT`` | Exit |
1500+-------------------+--------------------------------------------+
1501| ``KEY_FIND`` | Find |
1502+-------------------+--------------------------------------------+
1503| ``KEY_HELP`` | Help |
1504+-------------------+--------------------------------------------+
1505| ``KEY_MARK`` | Mark |
1506+-------------------+--------------------------------------------+
1507| ``KEY_MESSAGE`` | Message |
1508+-------------------+--------------------------------------------+
1509| ``KEY_MOVE`` | Move |
1510+-------------------+--------------------------------------------+
1511| ``KEY_NEXT`` | Next |
1512+-------------------+--------------------------------------------+
1513| ``KEY_OPEN`` | Open |
1514+-------------------+--------------------------------------------+
1515| ``KEY_OPTIONS`` | Options |
1516+-------------------+--------------------------------------------+
1517| ``KEY_PREVIOUS`` | Prev (previous) |
1518+-------------------+--------------------------------------------+
1519| ``KEY_REDO`` | Redo |
1520+-------------------+--------------------------------------------+
1521| ``KEY_REFERENCE`` | Ref (reference) |
1522+-------------------+--------------------------------------------+
1523| ``KEY_REFRESH`` | Refresh |
1524+-------------------+--------------------------------------------+
1525| ``KEY_REPLACE`` | Replace |
1526+-------------------+--------------------------------------------+
1527| ``KEY_RESTART`` | Restart |
1528+-------------------+--------------------------------------------+
1529| ``KEY_RESUME`` | Resume |
1530+-------------------+--------------------------------------------+
1531| ``KEY_SAVE`` | Save |
1532+-------------------+--------------------------------------------+
1533| ``KEY_SBEG`` | Shifted Beg (beginning) |
1534+-------------------+--------------------------------------------+
1535| ``KEY_SCANCEL`` | Shifted Cancel |
1536+-------------------+--------------------------------------------+
1537| ``KEY_SCOMMAND`` | Shifted Command |
1538+-------------------+--------------------------------------------+
1539| ``KEY_SCOPY`` | Shifted Copy |
1540+-------------------+--------------------------------------------+
1541| ``KEY_SCREATE`` | Shifted Create |
1542+-------------------+--------------------------------------------+
1543| ``KEY_SDC`` | Shifted Delete char |
1544+-------------------+--------------------------------------------+
1545| ``KEY_SDL`` | Shifted Delete line |
1546+-------------------+--------------------------------------------+
1547| ``KEY_SELECT`` | Select |
1548+-------------------+--------------------------------------------+
1549| ``KEY_SEND`` | Shifted End |
1550+-------------------+--------------------------------------------+
1551| ``KEY_SEOL`` | Shifted Clear line |
1552+-------------------+--------------------------------------------+
Mariatta64508782017-03-19 20:48:04 -07001553| ``KEY_SEXIT`` | Shifted Exit |
Georg Brandl116aa622007-08-15 14:28:22 +00001554+-------------------+--------------------------------------------+
1555| ``KEY_SFIND`` | Shifted Find |
1556+-------------------+--------------------------------------------+
1557| ``KEY_SHELP`` | Shifted Help |
1558+-------------------+--------------------------------------------+
1559| ``KEY_SHOME`` | Shifted Home |
1560+-------------------+--------------------------------------------+
1561| ``KEY_SIC`` | Shifted Input |
1562+-------------------+--------------------------------------------+
1563| ``KEY_SLEFT`` | Shifted Left arrow |
1564+-------------------+--------------------------------------------+
1565| ``KEY_SMESSAGE`` | Shifted Message |
1566+-------------------+--------------------------------------------+
1567| ``KEY_SMOVE`` | Shifted Move |
1568+-------------------+--------------------------------------------+
1569| ``KEY_SNEXT`` | Shifted Next |
1570+-------------------+--------------------------------------------+
1571| ``KEY_SOPTIONS`` | Shifted Options |
1572+-------------------+--------------------------------------------+
1573| ``KEY_SPREVIOUS`` | Shifted Prev |
1574+-------------------+--------------------------------------------+
1575| ``KEY_SPRINT`` | Shifted Print |
1576+-------------------+--------------------------------------------+
1577| ``KEY_SREDO`` | Shifted Redo |
1578+-------------------+--------------------------------------------+
1579| ``KEY_SREPLACE`` | Shifted Replace |
1580+-------------------+--------------------------------------------+
1581| ``KEY_SRIGHT`` | Shifted Right arrow |
1582+-------------------+--------------------------------------------+
1583| ``KEY_SRSUME`` | Shifted Resume |
1584+-------------------+--------------------------------------------+
1585| ``KEY_SSAVE`` | Shifted Save |
1586+-------------------+--------------------------------------------+
1587| ``KEY_SSUSPEND`` | Shifted Suspend |
1588+-------------------+--------------------------------------------+
1589| ``KEY_SUNDO`` | Shifted Undo |
1590+-------------------+--------------------------------------------+
1591| ``KEY_SUSPEND`` | Suspend |
1592+-------------------+--------------------------------------------+
1593| ``KEY_UNDO`` | Undo |
1594+-------------------+--------------------------------------------+
1595| ``KEY_MOUSE`` | Mouse event has occurred |
1596+-------------------+--------------------------------------------+
1597| ``KEY_RESIZE`` | Terminal resize event |
1598+-------------------+--------------------------------------------+
1599| ``KEY_MAX`` | Maximum key value |
1600+-------------------+--------------------------------------------+
1601
1602On VT100s and their software emulations, such as X terminal emulators, there are
1603normally at least four function keys (:const:`KEY_F1`, :const:`KEY_F2`,
1604:const:`KEY_F3`, :const:`KEY_F4`) available, and the arrow keys mapped to
1605:const:`KEY_UP`, :const:`KEY_DOWN`, :const:`KEY_LEFT` and :const:`KEY_RIGHT` in
1606the obvious way. If your machine has a PC keyboard, it is safe to expect arrow
1607keys and twelve function keys (older PC keyboards may have only ten function
1608keys); also, the following keypad mappings are standard:
1609
1610+------------------+-----------+
1611| Keycap | Constant |
1612+==================+===========+
1613| :kbd:`Insert` | KEY_IC |
1614+------------------+-----------+
1615| :kbd:`Delete` | KEY_DC |
1616+------------------+-----------+
1617| :kbd:`Home` | KEY_HOME |
1618+------------------+-----------+
1619| :kbd:`End` | KEY_END |
1620+------------------+-----------+
Berker Peksagaf836392016-04-02 04:48:27 +03001621| :kbd:`Page Up` | KEY_PPAGE |
Georg Brandl116aa622007-08-15 14:28:22 +00001622+------------------+-----------+
Berker Peksagaf836392016-04-02 04:48:27 +03001623| :kbd:`Page Down` | KEY_NPAGE |
Georg Brandl116aa622007-08-15 14:28:22 +00001624+------------------+-----------+
1625
1626The following table lists characters from the alternate character set. These are
1627inherited from the VT100 terminal, and will generally be available on software
1628emulations such as X terminals. When there is no graphic available, curses
1629falls back on a crude printable ASCII approximation.
1630
1631.. note::
1632
1633 These are available only after :func:`initscr` has been called.
1634
1635+------------------+------------------------------------------+
1636| ACS code | Meaning |
1637+==================+==========================================+
1638| ``ACS_BBSS`` | alternate name for upper right corner |
1639+------------------+------------------------------------------+
1640| ``ACS_BLOCK`` | solid square block |
1641+------------------+------------------------------------------+
1642| ``ACS_BOARD`` | board of squares |
1643+------------------+------------------------------------------+
1644| ``ACS_BSBS`` | alternate name for horizontal line |
1645+------------------+------------------------------------------+
1646| ``ACS_BSSB`` | alternate name for upper left corner |
1647+------------------+------------------------------------------+
1648| ``ACS_BSSS`` | alternate name for top tee |
1649+------------------+------------------------------------------+
1650| ``ACS_BTEE`` | bottom tee |
1651+------------------+------------------------------------------+
1652| ``ACS_BULLET`` | bullet |
1653+------------------+------------------------------------------+
1654| ``ACS_CKBOARD`` | checker board (stipple) |
1655+------------------+------------------------------------------+
1656| ``ACS_DARROW`` | arrow pointing down |
1657+------------------+------------------------------------------+
1658| ``ACS_DEGREE`` | degree symbol |
1659+------------------+------------------------------------------+
1660| ``ACS_DIAMOND`` | diamond |
1661+------------------+------------------------------------------+
1662| ``ACS_GEQUAL`` | greater-than-or-equal-to |
1663+------------------+------------------------------------------+
1664| ``ACS_HLINE`` | horizontal line |
1665+------------------+------------------------------------------+
1666| ``ACS_LANTERN`` | lantern symbol |
1667+------------------+------------------------------------------+
1668| ``ACS_LARROW`` | left arrow |
1669+------------------+------------------------------------------+
1670| ``ACS_LEQUAL`` | less-than-or-equal-to |
1671+------------------+------------------------------------------+
1672| ``ACS_LLCORNER`` | lower left-hand corner |
1673+------------------+------------------------------------------+
1674| ``ACS_LRCORNER`` | lower right-hand corner |
1675+------------------+------------------------------------------+
1676| ``ACS_LTEE`` | left tee |
1677+------------------+------------------------------------------+
1678| ``ACS_NEQUAL`` | not-equal sign |
1679+------------------+------------------------------------------+
1680| ``ACS_PI`` | letter pi |
1681+------------------+------------------------------------------+
1682| ``ACS_PLMINUS`` | plus-or-minus sign |
1683+------------------+------------------------------------------+
1684| ``ACS_PLUS`` | big plus sign |
1685+------------------+------------------------------------------+
1686| ``ACS_RARROW`` | right arrow |
1687+------------------+------------------------------------------+
1688| ``ACS_RTEE`` | right tee |
1689+------------------+------------------------------------------+
1690| ``ACS_S1`` | scan line 1 |
1691+------------------+------------------------------------------+
1692| ``ACS_S3`` | scan line 3 |
1693+------------------+------------------------------------------+
1694| ``ACS_S7`` | scan line 7 |
1695+------------------+------------------------------------------+
1696| ``ACS_S9`` | scan line 9 |
1697+------------------+------------------------------------------+
1698| ``ACS_SBBS`` | alternate name for lower right corner |
1699+------------------+------------------------------------------+
1700| ``ACS_SBSB`` | alternate name for vertical line |
1701+------------------+------------------------------------------+
1702| ``ACS_SBSS`` | alternate name for right tee |
1703+------------------+------------------------------------------+
1704| ``ACS_SSBB`` | alternate name for lower left corner |
1705+------------------+------------------------------------------+
1706| ``ACS_SSBS`` | alternate name for bottom tee |
1707+------------------+------------------------------------------+
1708| ``ACS_SSSB`` | alternate name for left tee |
1709+------------------+------------------------------------------+
1710| ``ACS_SSSS`` | alternate name for crossover or big plus |
1711+------------------+------------------------------------------+
1712| ``ACS_STERLING`` | pound sterling |
1713+------------------+------------------------------------------+
1714| ``ACS_TTEE`` | top tee |
1715+------------------+------------------------------------------+
1716| ``ACS_UARROW`` | up arrow |
1717+------------------+------------------------------------------+
1718| ``ACS_ULCORNER`` | upper left corner |
1719+------------------+------------------------------------------+
1720| ``ACS_URCORNER`` | upper right corner |
1721+------------------+------------------------------------------+
1722| ``ACS_VLINE`` | vertical line |
1723+------------------+------------------------------------------+
1724
1725The following table lists the predefined colors:
1726
1727+-------------------+----------------------------+
1728| Constant | Color |
1729+===================+============================+
1730| ``COLOR_BLACK`` | Black |
1731+-------------------+----------------------------+
1732| ``COLOR_BLUE`` | Blue |
1733+-------------------+----------------------------+
1734| ``COLOR_CYAN`` | Cyan (light greenish blue) |
1735+-------------------+----------------------------+
1736| ``COLOR_GREEN`` | Green |
1737+-------------------+----------------------------+
1738| ``COLOR_MAGENTA`` | Magenta (purplish red) |
1739+-------------------+----------------------------+
1740| ``COLOR_RED`` | Red |
1741+-------------------+----------------------------+
1742| ``COLOR_WHITE`` | White |
1743+-------------------+----------------------------+
1744| ``COLOR_YELLOW`` | Yellow |
1745+-------------------+----------------------------+
1746
1747
1748:mod:`curses.textpad` --- Text input widget for curses programs
1749===============================================================
1750
1751.. module:: curses.textpad
1752 :synopsis: Emacs-like input editing in a curses window.
1753.. moduleauthor:: Eric Raymond <esr@thyrsus.com>
1754.. sectionauthor:: Eric Raymond <esr@thyrsus.com>
1755
1756
Georg Brandl116aa622007-08-15 14:28:22 +00001757The :mod:`curses.textpad` module provides a :class:`Textbox` class that handles
1758elementary text editing in a curses window, supporting a set of keybindings
1759resembling those of Emacs (thus, also of Netscape Navigator, BBedit 6.x,
1760FrameMaker, and many other programs). The module also provides a
1761rectangle-drawing function useful for framing text boxes or for other purposes.
1762
1763The module :mod:`curses.textpad` defines the following function:
1764
1765
1766.. function:: rectangle(win, uly, ulx, lry, lrx)
1767
1768 Draw a rectangle. The first argument must be a window object; the remaining
1769 arguments are coordinates relative to that window. The second and third
1770 arguments are the y and x coordinates of the upper left hand corner of the
1771 rectangle to be drawn; the fourth and fifth arguments are the y and x
1772 coordinates of the lower right hand corner. The rectangle will be drawn using
1773 VT100/IBM PC forms characters on terminals that make this possible (including
1774 xterm and most other software terminal emulators). Otherwise it will be drawn
1775 with ASCII dashes, vertical bars, and plus signs.
1776
1777
1778.. _curses-textpad-objects:
1779
1780Textbox objects
1781---------------
1782
1783You can instantiate a :class:`Textbox` object as follows:
1784
1785
1786.. class:: Textbox(win)
1787
1788 Return a textbox widget object. The *win* argument should be a curses
Berker Peksag93fc20b2017-05-23 03:16:07 +03001789 :ref:`window <curses-window-objects>` object in which the textbox is to
1790 be contained. The edit cursor of the textbox is initially located at the
1791 upper left hand corner of the containing window, with coordinates ``(0, 0)``.
1792 The instance's :attr:`stripspaces` flag is initially on.
Georg Brandl116aa622007-08-15 14:28:22 +00001793
Benjamin Petersone41251e2008-04-25 01:59:09 +00001794 :class:`Textbox` objects have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001795
1796
Benjamin Petersone41251e2008-04-25 01:59:09 +00001797 .. method:: edit([validator])
Georg Brandl116aa622007-08-15 14:28:22 +00001798
Benjamin Petersone41251e2008-04-25 01:59:09 +00001799 This is the entry point you will normally use. It accepts editing
1800 keystrokes until one of the termination keystrokes is entered. If
1801 *validator* is supplied, it must be a function. It will be called for
1802 each keystroke entered with the keystroke as a parameter; command dispatch
1803 is done on the result. This method returns the window contents as a
1804 string; whether blanks in the window are included is affected by the
Senthil Kumarana6bac952011-07-04 11:28:30 -07001805 :attr:`stripspaces` attribute.
Georg Brandl116aa622007-08-15 14:28:22 +00001806
1807
Benjamin Petersone41251e2008-04-25 01:59:09 +00001808 .. method:: do_command(ch)
Georg Brandl116aa622007-08-15 14:28:22 +00001809
Benjamin Petersone41251e2008-04-25 01:59:09 +00001810 Process a single command keystroke. Here are the supported special
1811 keystrokes:
Georg Brandl116aa622007-08-15 14:28:22 +00001812
Benjamin Petersone41251e2008-04-25 01:59:09 +00001813 +------------------+-------------------------------------------+
1814 | Keystroke | Action |
1815 +==================+===========================================+
1816 | :kbd:`Control-A` | Go to left edge of window. |
1817 +------------------+-------------------------------------------+
1818 | :kbd:`Control-B` | Cursor left, wrapping to previous line if |
1819 | | appropriate. |
1820 +------------------+-------------------------------------------+
1821 | :kbd:`Control-D` | Delete character under cursor. |
1822 +------------------+-------------------------------------------+
1823 | :kbd:`Control-E` | Go to right edge (stripspaces off) or end |
1824 | | of line (stripspaces on). |
1825 +------------------+-------------------------------------------+
1826 | :kbd:`Control-F` | Cursor right, wrapping to next line when |
1827 | | appropriate. |
1828 +------------------+-------------------------------------------+
1829 | :kbd:`Control-G` | Terminate, returning the window contents. |
1830 +------------------+-------------------------------------------+
1831 | :kbd:`Control-H` | Delete character backward. |
1832 +------------------+-------------------------------------------+
1833 | :kbd:`Control-J` | Terminate if the window is 1 line, |
1834 | | otherwise insert newline. |
1835 +------------------+-------------------------------------------+
1836 | :kbd:`Control-K` | If line is blank, delete it, otherwise |
1837 | | clear to end of line. |
1838 +------------------+-------------------------------------------+
1839 | :kbd:`Control-L` | Refresh screen. |
1840 +------------------+-------------------------------------------+
1841 | :kbd:`Control-N` | Cursor down; move down one line. |
1842 +------------------+-------------------------------------------+
1843 | :kbd:`Control-O` | Insert a blank line at cursor location. |
1844 +------------------+-------------------------------------------+
1845 | :kbd:`Control-P` | Cursor up; move up one line. |
1846 +------------------+-------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001847
Benjamin Petersone41251e2008-04-25 01:59:09 +00001848 Move operations do nothing if the cursor is at an edge where the movement
1849 is not possible. The following synonyms are supported where possible:
Georg Brandl116aa622007-08-15 14:28:22 +00001850
Benjamin Petersone41251e2008-04-25 01:59:09 +00001851 +------------------------+------------------+
1852 | Constant | Keystroke |
1853 +========================+==================+
1854 | :const:`KEY_LEFT` | :kbd:`Control-B` |
1855 +------------------------+------------------+
1856 | :const:`KEY_RIGHT` | :kbd:`Control-F` |
1857 +------------------------+------------------+
1858 | :const:`KEY_UP` | :kbd:`Control-P` |
1859 +------------------------+------------------+
1860 | :const:`KEY_DOWN` | :kbd:`Control-N` |
1861 +------------------------+------------------+
1862 | :const:`KEY_BACKSPACE` | :kbd:`Control-h` |
1863 +------------------------+------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001864
Benjamin Petersone41251e2008-04-25 01:59:09 +00001865 All other keystrokes are treated as a command to insert the given
1866 character and move right (with line wrapping).
Georg Brandl116aa622007-08-15 14:28:22 +00001867
1868
Benjamin Petersone41251e2008-04-25 01:59:09 +00001869 .. method:: gather()
Georg Brandl116aa622007-08-15 14:28:22 +00001870
Ezio Melottib6b73712011-06-26 13:38:11 +03001871 Return the window contents as a string; whether blanks in the
Benjamin Petersone41251e2008-04-25 01:59:09 +00001872 window are included is affected by the :attr:`stripspaces` member.
Georg Brandl116aa622007-08-15 14:28:22 +00001873
1874
Benjamin Petersone41251e2008-04-25 01:59:09 +00001875 .. attribute:: stripspaces
Georg Brandl116aa622007-08-15 14:28:22 +00001876
Senthil Kumarana6bac952011-07-04 11:28:30 -07001877 This attribute is a flag which controls the interpretation of blanks in
Benjamin Petersone41251e2008-04-25 01:59:09 +00001878 the window. When it is on, trailing blanks on each line are ignored; any
1879 cursor motion that would land the cursor on a trailing blank goes to the
1880 end of that line instead, and trailing blanks are stripped when the window
1881 contents are gathered.