blob: 26121acaacb7a04d4d444afa413389576ea67448 [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 Storchaka1470edd2021-01-03 22:51:11 +0200115 *color_number*, which must be between ``0`` and ``COLORS - 1``. Return a 3-tuple,
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300116 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
Serhiy Storchaka1470edd2021-01-03 22:51:11 +0200120.. function:: color_pair(pair_number)
Georg Brandl116aa622007-08-15 14:28:22 +0000121
Serhiy Storchaka1470edd2021-01-03 22:51:11 +0200122 Return the attribute value for displaying text in the specified color pair.
123 Only the first 256 color pairs are supported. This
Georg Brandl116aa622007-08-15 14:28:22 +0000124 attribute value can be combined with :const:`A_STANDOUT`, :const:`A_REVERSE`,
125 and the other :const:`A_\*` attributes. :func:`pair_number` is the counterpart
126 to this function.
127
128
129.. function:: curs_set(visibility)
130
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300131 Set the cursor state. *visibility* can be set to ``0``, ``1``, or ``2``, for invisible,
132 normal, or very visible. If the terminal supports the visibility requested, return the
133 previous cursor state; otherwise raise an exception. On many
Georg Brandl116aa622007-08-15 14:28:22 +0000134 terminals, the "visible" mode is an underline cursor and the "very visible" mode
135 is a block cursor.
136
137
138.. function:: def_prog_mode()
139
Ezio Melottib6b73712011-06-26 13:38:11 +0300140 Save the current terminal mode as the "program" mode, the mode when the running
Georg Brandl116aa622007-08-15 14:28:22 +0000141 program is using curses. (Its counterpart is the "shell" mode, for when the
142 program is not in curses.) Subsequent calls to :func:`reset_prog_mode` will
143 restore this mode.
144
145
146.. function:: def_shell_mode()
147
Ezio Melottib6b73712011-06-26 13:38:11 +0300148 Save the current terminal mode as the "shell" mode, the mode when the running
Georg Brandl116aa622007-08-15 14:28:22 +0000149 program is not using curses. (Its counterpart is the "program" mode, when the
150 program is using curses capabilities.) Subsequent calls to
151 :func:`reset_shell_mode` will restore this mode.
152
153
154.. function:: delay_output(ms)
155
Ezio Melottib6b73712011-06-26 13:38:11 +0300156 Insert an *ms* millisecond pause in output.
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158
159.. function:: doupdate()
160
161 Update the physical screen. The curses library keeps two data structures, one
162 representing the current physical screen contents and a virtual screen
163 representing the desired next state. The :func:`doupdate` ground updates the
164 physical screen to match the virtual screen.
165
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300166 The virtual screen may be updated by a :meth:`~window.noutrefresh` call after write
167 operations such as :meth:`~window.addstr` have been performed on a window. The normal
168 :meth:`~window.refresh` call is simply :meth:`!noutrefresh` followed by :func:`!doupdate`;
Georg Brandl116aa622007-08-15 14:28:22 +0000169 if you have to update multiple windows, you can speed performance and perhaps
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300170 reduce screen flicker by issuing :meth:`!noutrefresh` calls on all windows,
171 followed by a single :func:`!doupdate`.
Georg Brandl116aa622007-08-15 14:28:22 +0000172
173
174.. function:: echo()
175
176 Enter echo mode. In echo mode, each character input is echoed to the screen as
177 it is entered.
178
179
180.. function:: endwin()
181
182 De-initialize the library, and return terminal to normal status.
183
184
185.. function:: erasechar()
186
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300187 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 +0000188 is a property of the controlling tty of the curses program, and is not set by
189 the curses library itself.
190
191
192.. function:: filter()
193
Georg Brandl502d9a52009-07-26 15:02:41 +0000194 The :func:`.filter` routine, if used, must be called before :func:`initscr` is
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300195 called. The effect is that, during those calls, :envvar:`LINES` is set to ``1``; the
196 capabilities ``clear``, ``cup``, ``cud``, ``cud1``, ``cuu1``, ``cuu``, ``vpa`` are disabled; and the ``home``
197 string is set to the value of ``cr``. The effect is that the cursor is confined to
Georg Brandl116aa622007-08-15 14:28:22 +0000198 the current line, and so are screen updates. This may be used for enabling
199 character-at-a-time line editing without touching the rest of the screen.
200
201
202.. function:: flash()
203
204 Flash the screen. That is, change it to reverse-video and then change it back
205 in a short interval. Some people prefer such as 'visible bell' to the audible
206 attention signal produced by :func:`beep`.
207
208
209.. function:: flushinp()
210
211 Flush all input buffers. This throws away any typeahead that has been typed
212 by the user and has not yet been processed by the program.
213
214
215.. function:: getmouse()
216
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300217 After :meth:`~window.getch` returns :const:`KEY_MOUSE` to signal a mouse event, this
Sebastien Williams-Wynnba18c0b2020-07-29 19:36:46 +0100218 method should be called to retrieve the queued mouse event, represented as a
Georg Brandl116aa622007-08-15 14:28:22 +0000219 5-tuple ``(id, x, y, z, bstate)``. *id* is an ID value used to distinguish
220 multiple devices, and *x*, *y*, *z* are the event's coordinates. (*z* is
Ezio Melottib6b73712011-06-26 13:38:11 +0300221 currently unused.) *bstate* is an integer value whose bits will be set to
Georg Brandl116aa622007-08-15 14:28:22 +0000222 indicate the type of event, and will be the bitwise OR of one or more of the
223 following constants, where *n* is the button number from 1 to 4:
224 :const:`BUTTONn_PRESSED`, :const:`BUTTONn_RELEASED`, :const:`BUTTONn_CLICKED`,
225 :const:`BUTTONn_DOUBLE_CLICKED`, :const:`BUTTONn_TRIPLE_CLICKED`,
226 :const:`BUTTON_SHIFT`, :const:`BUTTON_CTRL`, :const:`BUTTON_ALT`.
227
228
229.. function:: getsyx()
230
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300231 Return the current coordinates of the virtual screen cursor as a tuple
232 ``(y, x)``. If :meth:`leaveok <window.leaveok>` is currently ``True``, then return ``(-1, -1)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000233
234
235.. function:: getwin(file)
236
Ezio Melottib6b73712011-06-26 13:38:11 +0300237 Read window related data stored in the file by an earlier :func:`putwin` call.
Georg Brandl116aa622007-08-15 14:28:22 +0000238 The routine then creates and initializes a new window using that data, returning
239 the new window object.
240
241
242.. function:: has_colors()
243
Ezio Melottib6b73712011-06-26 13:38:11 +0300244 Return ``True`` if the terminal can display colors; otherwise, return ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +0000245
Hans Petter Janssonda4e09f2020-08-03 22:51:33 -0500246.. function:: has_extended_color_support()
247
248 Return ``True`` if the module supports extended colors; otherwise, return
249 ``False``. Extended color support allows more than 256 color pairs for
250 terminals that support more than 16 colors (e.g. xterm-256color).
251
252 Extended color support requires ncurses version 6.1 or later.
253
254 .. versionadded:: 3.10
Georg Brandl116aa622007-08-15 14:28:22 +0000255
256.. function:: has_ic()
257
Ezio Melottib6b73712011-06-26 13:38:11 +0300258 Return ``True`` if the terminal has insert- and delete-character capabilities.
Georg Brandl116aa622007-08-15 14:28:22 +0000259 This function is included for historical reasons only, as all modern software
260 terminal emulators have such capabilities.
261
262
263.. function:: has_il()
264
Ezio Melottib6b73712011-06-26 13:38:11 +0300265 Return ``True`` if the terminal has insert- and delete-line capabilities, or can
Georg Brandl116aa622007-08-15 14:28:22 +0000266 simulate them using scrolling regions. This function is included for
267 historical reasons only, as all modern software terminal emulators have such
268 capabilities.
269
270
271.. function:: has_key(ch)
272
Ezio Melottib6b73712011-06-26 13:38:11 +0300273 Take a key value *ch*, and return ``True`` if the current terminal type recognizes
Georg Brandl116aa622007-08-15 14:28:22 +0000274 a key with that value.
275
276
277.. function:: halfdelay(tenths)
278
279 Used for half-delay mode, which is similar to cbreak mode in that characters
280 typed by the user are immediately available to the program. However, after
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300281 blocking for *tenths* tenths of seconds, raise an exception if nothing has
Ezio Melottib6b73712011-06-26 13:38:11 +0300282 been typed. The value of *tenths* must be a number between ``1`` and ``255``. Use
Georg Brandl116aa622007-08-15 14:28:22 +0000283 :func:`nocbreak` to leave half-delay mode.
284
285
286.. function:: init_color(color_number, r, g, b)
287
Ezio Melottib6b73712011-06-26 13:38:11 +0300288 Change the definition of a color, taking the number of the color to be changed
Georg Brandl116aa622007-08-15 14:28:22 +0000289 followed by three RGB values (for the amounts of red, green, and blue
290 components). The value of *color_number* must be between ``0`` and
Serhiy Storchaka1470edd2021-01-03 22:51:11 +0200291 `COLORS - 1`. Each of *r*, *g*, *b*, must be a value between ``0`` and
Georg Brandl116aa622007-08-15 14:28:22 +0000292 ``1000``. When :func:`init_color` is used, all occurrences of that color on the
293 screen immediately change to the new definition. This function is a no-op on
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300294 most terminals; it is active only if :func:`can_change_color` returns ``True``.
Georg Brandl116aa622007-08-15 14:28:22 +0000295
296
297.. function:: init_pair(pair_number, fg, bg)
298
Ezio Melottib6b73712011-06-26 13:38:11 +0300299 Change the definition of a color-pair. It takes three arguments: the number of
Georg Brandl116aa622007-08-15 14:28:22 +0000300 the color-pair to be changed, the foreground color number, and the background
301 color number. The value of *pair_number* must be between ``1`` and
302 ``COLOR_PAIRS - 1`` (the ``0`` color pair is wired to white on black and cannot
303 be changed). The value of *fg* and *bg* arguments must be between ``0`` and
Serhiy Storchaka1470edd2021-01-03 22:51:11 +0200304 ``COLORS - 1``, or, after calling :func:`use_default_colors`, ``-1``.
305 If the color-pair was previously initialized, the screen is
Georg Brandl116aa622007-08-15 14:28:22 +0000306 refreshed and all occurrences of that color-pair are changed to the new
307 definition.
308
309
310.. function:: initscr()
311
Berker Peksag93fc20b2017-05-23 03:16:07 +0300312 Initialize the library. Return a :ref:`window <curses-window-objects>` object
313 which represents the whole screen.
Georg Brandl116aa622007-08-15 14:28:22 +0000314
315 .. note::
316
317 If there is an error opening the terminal, the underlying curses library may
318 cause the interpreter to exit.
319
320
Ezio Melotti4850d522011-06-26 13:34:56 +0300321.. function:: is_term_resized(nlines, ncols)
322
323 Return ``True`` if :func:`resize_term` would modify the window structure,
324 ``False`` otherwise.
325
326
Georg Brandl116aa622007-08-15 14:28:22 +0000327.. function:: isendwin()
328
Ezio Melottib6b73712011-06-26 13:38:11 +0300329 Return ``True`` if :func:`endwin` has been called (that is, the curses library has
Georg Brandl116aa622007-08-15 14:28:22 +0000330 been deinitialized).
331
332
333.. function:: keyname(k)
334
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300335 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 +0000336 ASCII character is the key's character. The name of a control-key combination
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300337 is a two-byte bytes object consisting of a caret (``b'^'``) followed by the corresponding
Serhiy Storchakac7b1a0b2016-11-26 13:43:28 +0200338 printable ASCII character. The name of an alt-key combination (128--255) is a
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300339 bytes object consisting of the prefix ``b'M-'`` followed by the name of the corresponding
Georg Brandl116aa622007-08-15 14:28:22 +0000340 ASCII character.
341
342
343.. function:: killchar()
344
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300345 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 +0000346 this is a property of the controlling tty of the curses program, and is not set
347 by the curses library itself.
348
349
350.. function:: longname()
351
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300352 Return a bytes object containing the terminfo long name field describing the current
Georg Brandl116aa622007-08-15 14:28:22 +0000353 terminal. The maximum length of a verbose description is 128 characters. It is
354 defined only after the call to :func:`initscr`.
355
356
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300357.. function:: meta(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000358
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300359 If *flag* is ``True``, allow 8-bit characters to be input. If
360 *flag* is ``False``, allow only 7-bit chars.
Georg Brandl116aa622007-08-15 14:28:22 +0000361
362
363.. function:: mouseinterval(interval)
364
Ezio Melottib6b73712011-06-26 13:38:11 +0300365 Set the maximum time in milliseconds that can elapse between press and release
366 events in order for them to be recognized as a click, and return the previous
Georg Brandl116aa622007-08-15 14:28:22 +0000367 interval value. The default value is 200 msec, or one fifth of a second.
368
369
370.. function:: mousemask(mousemask)
371
Ezio Melottib6b73712011-06-26 13:38:11 +0300372 Set the mouse events to be reported, and return a tuple ``(availmask,
Georg Brandl116aa622007-08-15 14:28:22 +0000373 oldmask)``. *availmask* indicates which of the specified mouse events can be
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300374 reported; on complete failure it returns ``0``. *oldmask* is the previous value of
Georg Brandl116aa622007-08-15 14:28:22 +0000375 the given window's mouse event mask. If this function is never called, no mouse
376 events are ever reported.
377
378
379.. function:: napms(ms)
380
381 Sleep for *ms* milliseconds.
382
383
384.. function:: newpad(nlines, ncols)
385
Ezio Melottib6b73712011-06-26 13:38:11 +0300386 Create and return a pointer to a new pad data structure with the given number
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300387 of lines and columns. Return a pad as a window object.
Georg Brandl116aa622007-08-15 14:28:22 +0000388
389 A pad is like a window, except that it is not restricted by the screen size, and
390 is not necessarily associated with a particular part of the screen. Pads can be
391 used when a large window is needed, and only a part of the window will be on the
392 screen at one time. Automatic refreshes of pads (such as from scrolling or
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300393 echoing of input) do not occur. The :meth:`~window.refresh` and :meth:`~window.noutrefresh`
Georg Brandl116aa622007-08-15 14:28:22 +0000394 methods of a pad require 6 arguments to specify the part of the pad to be
395 displayed and the location on the screen to be used for the display. The
Ezio Melottib6b73712011-06-26 13:38:11 +0300396 arguments are *pminrow*, *pmincol*, *sminrow*, *smincol*, *smaxrow*, *smaxcol*; the *p*
Georg Brandl116aa622007-08-15 14:28:22 +0000397 arguments refer to the upper left corner of the pad region to be displayed and
Ezio Melottib6b73712011-06-26 13:38:11 +0300398 the *s* arguments define a clipping box on the screen within which the pad region
Georg Brandl116aa622007-08-15 14:28:22 +0000399 is to be displayed.
400
401
Georg Brandl1ed80b02013-10-29 08:10:36 +0100402.. function:: newwin(nlines, ncols)
Ezio Melottie0add762012-09-14 06:32:35 +0300403 newwin(nlines, ncols, begin_y, begin_x)
Georg Brandl116aa622007-08-15 14:28:22 +0000404
Berker Peksag93fc20b2017-05-23 03:16:07 +0300405 Return a new :ref:`window <curses-window-objects>`, whose left-upper corner
406 is at ``(begin_y, begin_x)``, and whose height/width is *nlines*/*ncols*.
Georg Brandl116aa622007-08-15 14:28:22 +0000407
408 By default, the window will extend from the specified position to the lower
409 right corner of the screen.
410
411
412.. function:: nl()
413
414 Enter newline mode. This mode translates the return key into newline on input,
415 and translates newline into return and line-feed on output. Newline mode is
416 initially on.
417
418
419.. function:: nocbreak()
420
421 Leave cbreak mode. Return to normal "cooked" mode with line buffering.
422
423
424.. function:: noecho()
425
426 Leave echo mode. Echoing of input characters is turned off.
427
428
429.. function:: nonl()
430
431 Leave newline mode. Disable translation of return into newline on input, and
432 disable low-level translation of newline into newline/return on output (but this
433 does not change the behavior of ``addch('\n')``, which always does the
434 equivalent of return and line feed on the virtual screen). With translation
435 off, curses can sometimes speed up vertical motion a little; also, it will be
436 able to detect the return key on input.
437
438
439.. function:: noqiflush()
440
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300441 When the :func:`!noqiflush` routine is used, normal flush of input and output queues
442 associated with the ``INTR``, ``QUIT`` and ``SUSP`` characters will not be done. You may
443 want to call :func:`!noqiflush` in a signal handler if you want output to
Georg Brandl116aa622007-08-15 14:28:22 +0000444 continue as though the interrupt had not occurred, after the handler exits.
445
446
447.. function:: noraw()
448
449 Leave raw mode. Return to normal "cooked" mode with line buffering.
450
451
452.. function:: pair_content(pair_number)
453
Ezio Melottib6b73712011-06-26 13:38:11 +0300454 Return a tuple ``(fg, bg)`` containing the colors for the requested color pair.
Serhiy Storchaka1470edd2021-01-03 22:51:11 +0200455 The value of *pair_number* must be between ``0`` and ``COLOR_PAIRS - 1``.
Georg Brandl116aa622007-08-15 14:28:22 +0000456
457
458.. function:: pair_number(attr)
459
Ezio Melottib6b73712011-06-26 13:38:11 +0300460 Return the number of the color-pair set by the attribute value *attr*.
Georg Brandl116aa622007-08-15 14:28:22 +0000461 :func:`color_pair` is the counterpart to this function.
462
463
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300464.. function:: putp(str)
Georg Brandl116aa622007-08-15 14:28:22 +0000465
Ezio Melottib6b73712011-06-26 13:38:11 +0300466 Equivalent to ``tputs(str, 1, putchar)``; emit the value of a specified
467 terminfo capability for the current terminal. Note that the output of :func:`putp`
Georg Brandl116aa622007-08-15 14:28:22 +0000468 always goes to standard output.
469
470
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300471.. function:: qiflush([flag])
Georg Brandl116aa622007-08-15 14:28:22 +0000472
Ezio Melottib6b73712011-06-26 13:38:11 +0300473 If *flag* is ``False``, the effect is the same as calling :func:`noqiflush`. If
474 *flag* is ``True``, or no argument is provided, the queues will be flushed when
Georg Brandl116aa622007-08-15 14:28:22 +0000475 these control characters are read.
476
477
478.. function:: raw()
479
480 Enter raw mode. In raw mode, normal line buffering and processing of
481 interrupt, quit, suspend, and flow control keys are turned off; characters are
482 presented to curses input functions one by one.
483
484
485.. function:: reset_prog_mode()
486
Ezio Melottib6b73712011-06-26 13:38:11 +0300487 Restore the terminal to "program" mode, as previously saved by
Georg Brandl116aa622007-08-15 14:28:22 +0000488 :func:`def_prog_mode`.
489
490
491.. function:: reset_shell_mode()
492
Ezio Melottib6b73712011-06-26 13:38:11 +0300493 Restore the terminal to "shell" mode, as previously saved by
Georg Brandl116aa622007-08-15 14:28:22 +0000494 :func:`def_shell_mode`.
495
496
Ezio Melotti4850d522011-06-26 13:34:56 +0300497.. function:: resetty()
498
499 Restore the state of the terminal modes to what it was at the last call to
500 :func:`savetty`.
501
502
503.. function:: resize_term(nlines, ncols)
504
505 Backend function used by :func:`resizeterm`, performing most of the work;
506 when resizing the windows, :func:`resize_term` blank-fills the areas that are
507 extended. The calling application should fill in these areas with
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300508 appropriate data. The :func:`!resize_term` function attempts to resize all
Ezio Melotti4850d522011-06-26 13:34:56 +0300509 windows. However, due to the calling convention of pads, it is not possible
510 to resize these without additional interaction with the application.
511
512
513.. function:: resizeterm(nlines, ncols)
514
515 Resize the standard and current windows to the specified dimensions, and
516 adjusts other bookkeeping data used by the curses library that record the
517 window dimensions (in particular the SIGWINCH handler).
518
519
520.. function:: savetty()
521
522 Save the current state of the terminal modes in a buffer, usable by
523 :func:`resetty`.
524
Anthony Sottileb32cb972019-10-31 02:13:48 -0700525.. function:: get_escdelay()
526
527 Retrieves the value set by :func:`set_escdelay`.
528
529 .. versionadded:: 3.9
530
531.. function:: set_escdelay(ms)
532
533 Sets the number of milliseconds to wait after reading an escape character,
534 to distinguish between an individual escape character entered on the
535 keyboard from escape sequences sent by cursor and function keys.
536
537 .. versionadded:: 3.9
538
539.. function:: get_tabsize()
540
541 Retrieves the value set by :func:`set_tabsize`.
542
543 .. versionadded:: 3.9
544
545.. function:: set_tabsize(size)
546
547 Sets the number of columns used by the curses library when converting a tab
548 character to spaces as it adds the tab to a window.
549
550 .. versionadded:: 3.9
Ezio Melotti4850d522011-06-26 13:34:56 +0300551
Georg Brandl116aa622007-08-15 14:28:22 +0000552.. function:: setsyx(y, x)
553
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300554 Set the virtual screen cursor to *y*, *x*. If *y* and *x* are both ``-1``, then
555 :meth:`leaveok <window.leaveok>` is set ``True``.
Georg Brandl116aa622007-08-15 14:28:22 +0000556
557
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300558.. function:: setupterm(term=None, fd=-1)
Georg Brandl116aa622007-08-15 14:28:22 +0000559
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300560 Initialize the terminal. *term* is a string giving
561 the terminal name, or ``None``; if omitted or ``None``, the value of the
562 :envvar:`TERM` environment variable will be used. *fd* is the
Georg Brandl116aa622007-08-15 14:28:22 +0000563 file descriptor to which any initialization sequences will be sent; if not
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300564 supplied or ``-1``, the file descriptor for ``sys.stdout`` will be used.
Georg Brandl116aa622007-08-15 14:28:22 +0000565
566
567.. function:: start_color()
568
569 Must be called if the programmer wants to use colors, and before any other color
570 manipulation routine is called. It is good practice to call this routine right
571 after :func:`initscr`.
572
573 :func:`start_color` initializes eight basic colors (black, red, green, yellow,
574 blue, magenta, cyan, and white), and two global variables in the :mod:`curses`
575 module, :const:`COLORS` and :const:`COLOR_PAIRS`, containing the maximum number
576 of colors and color-pairs the terminal can support. It also restores the colors
577 on the terminal to the values they had when the terminal was just turned on.
578
579
580.. function:: termattrs()
581
Ezio Melottib6b73712011-06-26 13:38:11 +0300582 Return a logical OR of all video attributes supported by the terminal. This
Georg Brandl116aa622007-08-15 14:28:22 +0000583 information is useful when a curses program needs complete control over the
584 appearance of the screen.
585
586
587.. function:: termname()
588
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300589 Return the value of the environment variable :envvar:`TERM`, as a bytes object,
590 truncated to 14 characters.
Georg Brandl116aa622007-08-15 14:28:22 +0000591
592
593.. function:: tigetflag(capname)
594
Ezio Melottib6b73712011-06-26 13:38:11 +0300595 Return the value of the Boolean capability corresponding to the terminfo
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300596 capability name *capname* as an integer. Return the value ``-1`` if *capname* is not a
Georg Brandl116aa622007-08-15 14:28:22 +0000597 Boolean capability, or ``0`` if it is canceled or absent from the terminal
598 description.
599
600
601.. function:: tigetnum(capname)
602
Ezio Melottib6b73712011-06-26 13:38:11 +0300603 Return the value of the numeric capability corresponding to the terminfo
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300604 capability name *capname* as an integer. Return the value ``-2`` if *capname* is not a
Georg Brandl116aa622007-08-15 14:28:22 +0000605 numeric capability, or ``-1`` if it is canceled or absent from the terminal
606 description.
607
608
609.. function:: tigetstr(capname)
610
Ezio Melottib6b73712011-06-26 13:38:11 +0300611 Return the value of the string capability corresponding to the terminfo
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300612 capability name *capname* as a bytes object. Return ``None`` if *capname*
613 is not a terminfo "string capability", or is canceled or absent from the
614 terminal description.
Georg Brandl116aa622007-08-15 14:28:22 +0000615
616
Georg Brandlc2a4f4f2009-04-10 09:03:43 +0000617.. function:: tparm(str[, ...])
Georg Brandl116aa622007-08-15 14:28:22 +0000618
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300619 Instantiate the bytes object *str* with the supplied parameters, where *str* should
Ezio Melottib6b73712011-06-26 13:38:11 +0300620 be a parameterized string obtained from the terminfo database. E.g.
Victor Stinner26621332011-11-02 23:45:29 +0100621 ``tparm(tigetstr("cup"), 5, 3)`` could result in ``b'\033[6;4H'``, the exact
Georg Brandl116aa622007-08-15 14:28:22 +0000622 result depending on terminal type.
623
624
625.. function:: typeahead(fd)
626
Ezio Melottib6b73712011-06-26 13:38:11 +0300627 Specify that the file descriptor *fd* be used for typeahead checking. If *fd*
Georg Brandl116aa622007-08-15 14:28:22 +0000628 is ``-1``, then no typeahead checking is done.
629
630 The curses library does "line-breakout optimization" by looking for typeahead
631 periodically while updating the screen. If input is found, and it is coming
632 from a tty, the current update is postponed until refresh or doupdate is called
633 again, allowing faster response to commands typed in advance. This function
634 allows specifying a different file descriptor for typeahead checking.
635
636
637.. function:: unctrl(ch)
638
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300639 Return a bytes object which is a printable representation of the character *ch*.
640 Control characters are represented as a caret followed by the character, for
641 example as ``b'^C'``. Printing characters are left as they are.
Georg Brandl116aa622007-08-15 14:28:22 +0000642
643
644.. function:: ungetch(ch)
645
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300646 Push *ch* so the next :meth:`~window.getch` will return it.
Georg Brandl116aa622007-08-15 14:28:22 +0000647
648 .. note::
649
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300650 Only one *ch* can be pushed before :meth:`!getch` is called.
Georg Brandl116aa622007-08-15 14:28:22 +0000651
652
Steve Dowerd2bc3892015-04-15 18:06:05 -0400653.. function:: update_lines_cols()
654
655 Update :envvar:`LINES` and :envvar:`COLS`. Useful for detecting manual screen resize.
656
657 .. versionadded:: 3.5
658
659
Victor Stinner71e44cb2011-09-06 01:53:03 +0200660.. function:: unget_wch(ch)
661
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300662 Push *ch* so the next :meth:`~window.get_wch` will return it.
Victor Stinner71e44cb2011-09-06 01:53:03 +0200663
664 .. note::
665
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300666 Only one *ch* can be pushed before :meth:`!get_wch` is called.
Victor Stinner71e44cb2011-09-06 01:53:03 +0200667
668 .. versionadded:: 3.3
669
670
Georg Brandl116aa622007-08-15 14:28:22 +0000671.. function:: ungetmouse(id, x, y, z, bstate)
672
673 Push a :const:`KEY_MOUSE` event onto the input queue, associating the given
674 state data with it.
675
676
677.. function:: use_env(flag)
678
679 If used, this function should be called before :func:`initscr` or newterm are
Ezio Melottib6b73712011-06-26 13:38:11 +0300680 called. When *flag* is ``False``, the values of lines and columns specified in the
Georg Brandl116aa622007-08-15 14:28:22 +0000681 terminfo database will be used, even if environment variables :envvar:`LINES`
682 and :envvar:`COLUMNS` (used by default) are set, or if curses is running in a
683 window (in which case default behavior would be to use the window size if
684 :envvar:`LINES` and :envvar:`COLUMNS` are not set).
685
686
687.. function:: use_default_colors()
688
689 Allow use of default values for colors on terminals supporting this feature. Use
690 this to support transparency in your application. The default color is assigned
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300691 to the color number ``-1``. After calling this function, ``init_pair(x,
Georg Brandl116aa622007-08-15 14:28:22 +0000692 curses.COLOR_RED, -1)`` initializes, for instance, color pair *x* to a red
693 foreground color on the default background.
694
695
Serhiy Storchaka142566c2019-06-05 18:22:31 +0300696.. function:: wrapper(func, /, *args, **kwargs)
R David Murray409c32f2011-06-18 19:34:12 -0400697
698 Initialize curses and call another callable object, *func*, which should be the
699 rest of your curses-using application. If the application raises an exception,
700 this function will restore the terminal to a sane state before re-raising the
701 exception and generating a traceback. The callable object *func* is then passed
702 the main window 'stdscr' as its first argument, followed by any other arguments
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300703 passed to :func:`!wrapper`. Before calling *func*, :func:`!wrapper` turns on
R David Murray409c32f2011-06-18 19:34:12 -0400704 cbreak mode, turns off echo, enables the terminal keypad, and initializes colors
705 if the terminal has color support. On exit (whether normally or by exception)
706 it restores cooked mode, turns on echo, and disables the terminal keypad.
707
708
Georg Brandl116aa622007-08-15 14:28:22 +0000709.. _curses-window-objects:
710
711Window Objects
712--------------
713
714Window objects, as returned by :func:`initscr` and :func:`newwin` above, have
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100715the following methods and attributes:
Georg Brandl116aa622007-08-15 14:28:22 +0000716
717
Ezio Melottie0add762012-09-14 06:32:35 +0300718.. method:: window.addch(ch[, attr])
719 window.addch(y, x, ch[, attr])
Georg Brandl116aa622007-08-15 14:28:22 +0000720
Georg Brandl116aa622007-08-15 14:28:22 +0000721 Paint character *ch* at ``(y, x)`` with attributes *attr*, overwriting any
Zackery Spytz398575c2020-08-27 05:28:16 -0600722 character previously painted at that location. By default, the character
Georg Brandl116aa622007-08-15 14:28:22 +0000723 position and attributes are the current settings for the window object.
724
Jay Crottsef5ce882018-04-06 20:27:07 -0500725 .. note::
726
727 Writing outside the window, subwindow, or pad raises a :exc:`curses.error`.
728 Attempting to write to the lower right corner of a window, subwindow,
729 or pad will cause an exception to be raised after the character is printed.
730
Georg Brandl116aa622007-08-15 14:28:22 +0000731
Ezio Melottie0add762012-09-14 06:32:35 +0300732.. method:: window.addnstr(str, n[, attr])
733 window.addnstr(y, x, str, n[, attr])
Georg Brandl116aa622007-08-15 14:28:22 +0000734
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300735 Paint at most *n* characters of the character string *str* at
736 ``(y, x)`` with attributes
Georg Brandl116aa622007-08-15 14:28:22 +0000737 *attr*, overwriting anything previously on the display.
738
739
Ezio Melottie0add762012-09-14 06:32:35 +0300740.. method:: window.addstr(str[, attr])
741 window.addstr(y, x, str[, attr])
Georg Brandl116aa622007-08-15 14:28:22 +0000742
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300743 Paint the character string *str* at ``(y, x)`` with attributes
744 *attr*, overwriting anything previously on the display.
Georg Brandl116aa622007-08-15 14:28:22 +0000745
Jay Crottsef5ce882018-04-06 20:27:07 -0500746 .. note::
747
Toshio Kuratomie7b11362019-05-17 18:54:02 -0400748 * Writing outside the window, subwindow, or pad raises :exc:`curses.error`.
749 Attempting to write to the lower right corner of a window, subwindow,
750 or pad will cause an exception to be raised after the string is printed.
751
752 * A `bug in ncurses <https://bugs.python.org/issue35924>`_, the backend
753 for this Python module, can cause SegFaults when resizing windows. This
754 is fixed in ncurses-6.1-20190511. If you are stuck with an earlier
755 ncurses, you can avoid triggering this if you do not call :func:`addstr`
756 with a *str* that has embedded newlines. Instead, call :func:`addstr`
757 separately for each line.
Jay Crottsef5ce882018-04-06 20:27:07 -0500758
Georg Brandl116aa622007-08-15 14:28:22 +0000759
760.. method:: window.attroff(attr)
761
762 Remove attribute *attr* from the "background" set applied to all writes to the
763 current window.
764
765
766.. method:: window.attron(attr)
767
768 Add attribute *attr* from the "background" set applied to all writes to the
769 current window.
770
771
772.. method:: window.attrset(attr)
773
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300774 Set the "background" set of attributes to *attr*. This set is initially
775 ``0`` (no attributes).
Georg Brandl116aa622007-08-15 14:28:22 +0000776
777
778.. method:: window.bkgd(ch[, attr])
779
Ezio Melottib6b73712011-06-26 13:38:11 +0300780 Set the background property of the window to the character *ch*, with
Georg Brandl116aa622007-08-15 14:28:22 +0000781 attributes *attr*. The change is then applied to every character position in
782 that window:
783
784 * The attribute of every character in the window is changed to the new
785 background attribute.
786
787 * Wherever the former background character appears, it is changed to the new
788 background character.
789
790
791.. method:: window.bkgdset(ch[, attr])
792
Ezio Melottib6b73712011-06-26 13:38:11 +0300793 Set the window's background. A window's background consists of a character and
Georg Brandl116aa622007-08-15 14:28:22 +0000794 any combination of attributes. The attribute part of the background is combined
795 (OR'ed) with all non-blank characters that are written into the window. Both
796 the character and attribute parts of the background are combined with the blank
797 characters. The background becomes a property of the character and moves with
798 the character through any scrolling and insert/delete line/character operations.
799
800
801.. method:: window.border([ls[, rs[, ts[, bs[, tl[, tr[, bl[, br]]]]]]]])
802
803 Draw a border around the edges of the window. Each parameter specifies the
804 character to use for a specific part of the border; see the table below for more
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300805 details.
Georg Brandl116aa622007-08-15 14:28:22 +0000806
807 .. note::
808
809 A ``0`` value for any parameter will cause the default character to be used for
810 that parameter. Keyword parameters can *not* be used. The defaults are listed
811 in this table:
812
813 +-----------+---------------------+-----------------------+
814 | Parameter | Description | Default value |
815 +===========+=====================+=======================+
816 | *ls* | Left side | :const:`ACS_VLINE` |
817 +-----------+---------------------+-----------------------+
818 | *rs* | Right side | :const:`ACS_VLINE` |
819 +-----------+---------------------+-----------------------+
820 | *ts* | Top | :const:`ACS_HLINE` |
821 +-----------+---------------------+-----------------------+
822 | *bs* | Bottom | :const:`ACS_HLINE` |
823 +-----------+---------------------+-----------------------+
824 | *tl* | Upper-left corner | :const:`ACS_ULCORNER` |
825 +-----------+---------------------+-----------------------+
826 | *tr* | Upper-right corner | :const:`ACS_URCORNER` |
827 +-----------+---------------------+-----------------------+
828 | *bl* | Bottom-left corner | :const:`ACS_LLCORNER` |
829 +-----------+---------------------+-----------------------+
830 | *br* | Bottom-right corner | :const:`ACS_LRCORNER` |
831 +-----------+---------------------+-----------------------+
832
833
834.. method:: window.box([vertch, horch])
835
836 Similar to :meth:`border`, but both *ls* and *rs* are *vertch* and both *ts* and
Ezio Melottib6b73712011-06-26 13:38:11 +0300837 *bs* are *horch*. The default corner characters are always used by this function.
Georg Brandl116aa622007-08-15 14:28:22 +0000838
839
Ezio Melottie0add762012-09-14 06:32:35 +0300840.. method:: window.chgat(attr)
841 window.chgat(num, attr)
842 window.chgat(y, x, attr)
843 window.chgat(y, x, num, attr)
Georg Brandl116aa622007-08-15 14:28:22 +0000844
Ezio Melottib6b73712011-06-26 13:38:11 +0300845 Set the attributes of *num* characters at the current cursor position, or at
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300846 position ``(y, x)`` if supplied. If *num* is not given or is ``-1``,
Chillar Anandb838cc32017-11-04 13:43:16 +0530847 the attribute will be set on all the characters to the end of the line. This
848 function moves cursor to position ``(y, x)`` if supplied. The changed line
849 will be touched using the :meth:`touchline` method so that the contents will
850 be redisplayed by the next window refresh.
Georg Brandl116aa622007-08-15 14:28:22 +0000851
852
853.. method:: window.clear()
854
Ezio Melottib6b73712011-06-26 13:38:11 +0300855 Like :meth:`erase`, but also cause the whole window to be repainted upon next
Georg Brandl116aa622007-08-15 14:28:22 +0000856 call to :meth:`refresh`.
857
858
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300859.. method:: window.clearok(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000860
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300861 If *flag* is ``True``, the next call to :meth:`refresh` will clear the window
Georg Brandl116aa622007-08-15 14:28:22 +0000862 completely.
863
864
865.. method:: window.clrtobot()
866
867 Erase from cursor to the end of the window: all lines below the cursor are
868 deleted, and then the equivalent of :meth:`clrtoeol` is performed.
869
870
871.. method:: window.clrtoeol()
872
873 Erase from cursor to the end of the line.
874
875
876.. method:: window.cursyncup()
877
Ezio Melottib6b73712011-06-26 13:38:11 +0300878 Update the current cursor position of all the ancestors of the window to
Georg Brandl116aa622007-08-15 14:28:22 +0000879 reflect the current cursor position of the window.
880
881
882.. method:: window.delch([y, x])
883
884 Delete any character at ``(y, x)``.
885
886
887.. method:: window.deleteln()
888
Ezio Melottib6b73712011-06-26 13:38:11 +0300889 Delete the line under the cursor. All following lines are moved up by one line.
Georg Brandl116aa622007-08-15 14:28:22 +0000890
891
Ezio Melottie0add762012-09-14 06:32:35 +0300892.. method:: window.derwin(begin_y, begin_x)
893 window.derwin(nlines, ncols, begin_y, begin_x)
Georg Brandl116aa622007-08-15 14:28:22 +0000894
895 An abbreviation for "derive window", :meth:`derwin` is the same as calling
896 :meth:`subwin`, except that *begin_y* and *begin_x* are relative to the origin
Ezio Melottib6b73712011-06-26 13:38:11 +0300897 of the window, rather than relative to the entire screen. Return a window
Georg Brandl116aa622007-08-15 14:28:22 +0000898 object for the derived window.
899
900
901.. method:: window.echochar(ch[, attr])
902
903 Add character *ch* with attribute *attr*, and immediately call :meth:`refresh`
904 on the window.
905
906
907.. method:: window.enclose(y, x)
908
Ezio Melottib6b73712011-06-26 13:38:11 +0300909 Test whether the given pair of screen-relative character-cell coordinates are
910 enclosed by the given window, returning ``True`` or ``False``. It is useful for
Georg Brandl116aa622007-08-15 14:28:22 +0000911 determining what subset of the screen windows enclose the location of a mouse
912 event.
913
914
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100915.. attribute:: window.encoding
916
917 Encoding used to encode method arguments (Unicode strings and characters).
Ross Lagerwallce66a3e2012-09-06 18:58:43 +0200918 The encoding attribute is inherited from the parent window when a subwindow
Victor Stinner0fdfceb2011-11-25 22:10:02 +0100919 is created, for example with :meth:`window.subwin`. By default, the locale
920 encoding is used (see :func:`locale.getpreferredencoding`).
921
922 .. versionadded:: 3.3
923
924
Georg Brandl116aa622007-08-15 14:28:22 +0000925.. method:: window.erase()
926
927 Clear the window.
928
929
930.. method:: window.getbegyx()
931
932 Return a tuple ``(y, x)`` of co-ordinates of upper-left corner.
933
934
Ezio Melotti4850d522011-06-26 13:34:56 +0300935.. method:: window.getbkgd()
936
937 Return the given window's current background character/attribute pair.
938
939
Georg Brandl116aa622007-08-15 14:28:22 +0000940.. method:: window.getch([y, x])
941
942 Get a character. Note that the integer returned does *not* have to be in ASCII
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300943 range: function keys, keypad keys and so on are represented by numbers higher
944 than 255. In no-delay mode, return ``-1`` if there is no input, otherwise
945 wait until a key is pressed.
Georg Brandl116aa622007-08-15 14:28:22 +0000946
947
Victor Stinnera7878b72011-07-14 23:07:44 +0200948.. method:: window.get_wch([y, x])
949
Victor Stinner1d39cde2012-08-29 01:40:57 +0200950 Get a wide character. Return a character for most keys, or an integer for
951 function keys, keypad keys, and other special keys.
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300952 In no-delay mode, raise an exception if there is no input.
Victor Stinnera7878b72011-07-14 23:07:44 +0200953
954 .. versionadded:: 3.3
955
956
Georg Brandl116aa622007-08-15 14:28:22 +0000957.. method:: window.getkey([y, x])
958
959 Get a character, returning a string instead of an integer, as :meth:`getch`
Victor Stinner1d39cde2012-08-29 01:40:57 +0200960 does. Function keys, keypad keys and other special keys return a multibyte
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300961 string containing the key name. In no-delay mode, raise an exception if
Victor Stinner1d39cde2012-08-29 01:40:57 +0200962 there is no input.
Georg Brandl116aa622007-08-15 14:28:22 +0000963
964
965.. method:: window.getmaxyx()
966
967 Return a tuple ``(y, x)`` of the height and width of the window.
968
969
970.. method:: window.getparyx()
971
Ezio Melottib6b73712011-06-26 13:38:11 +0300972 Return the beginning coordinates of this window relative to its parent window
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300973 as a tuple ``(y, x)``. Return ``(-1, -1)`` if this window has no
Georg Brandl116aa622007-08-15 14:28:22 +0000974 parent.
975
976
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300977.. method:: window.getstr()
978 window.getstr(n)
979 window.getstr(y, x)
980 window.getstr(y, x, n)
Georg Brandl116aa622007-08-15 14:28:22 +0000981
Serhiy Storchaka300dd552017-10-04 22:44:13 +0300982 Read a bytes object from the user, with primitive line editing capacity.
Georg Brandl116aa622007-08-15 14:28:22 +0000983
984
985.. method:: window.getyx()
986
987 Return a tuple ``(y, x)`` of current cursor position relative to the window's
988 upper-left corner.
989
990
Ezio Melottie0add762012-09-14 06:32:35 +0300991.. method:: window.hline(ch, n)
992 window.hline(y, x, ch, n)
Georg Brandl116aa622007-08-15 14:28:22 +0000993
994 Display a horizontal line starting at ``(y, x)`` with length *n* consisting of
995 the character *ch*.
996
997
998.. method:: window.idcok(flag)
999
Ezio Melottib6b73712011-06-26 13:38:11 +03001000 If *flag* is ``False``, curses no longer considers using the hardware insert/delete
1001 character feature of the terminal; if *flag* is ``True``, use of character insertion
Georg Brandl116aa622007-08-15 14:28:22 +00001002 and deletion is enabled. When curses is first initialized, use of character
1003 insert/delete is enabled by default.
1004
1005
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001006.. method:: window.idlok(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001007
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001008 If *flag* is ``True``, :mod:`curses` will try and use hardware line
Georg Brandl116aa622007-08-15 14:28:22 +00001009 editing facilities. Otherwise, line insertion/deletion are disabled.
1010
1011
1012.. method:: window.immedok(flag)
1013
Ezio Melottib6b73712011-06-26 13:38:11 +03001014 If *flag* is ``True``, any change in the window image automatically causes the
Georg Brandl116aa622007-08-15 14:28:22 +00001015 window to be refreshed; you no longer have to call :meth:`refresh` yourself.
1016 However, it may degrade performance considerably, due to repeated calls to
1017 wrefresh. This option is disabled by default.
1018
1019
1020.. method:: window.inch([y, x])
1021
1022 Return the character at the given position in the window. The bottom 8 bits are
1023 the character proper, and upper bits are the attributes.
1024
1025
Ezio Melottie0add762012-09-14 06:32:35 +03001026.. method:: window.insch(ch[, attr])
1027 window.insch(y, x, ch[, attr])
Georg Brandl116aa622007-08-15 14:28:22 +00001028
1029 Paint character *ch* at ``(y, x)`` with attributes *attr*, moving the line from
1030 position *x* right by one character.
1031
1032
1033.. method:: window.insdelln(nlines)
1034
Ezio Melottib6b73712011-06-26 13:38:11 +03001035 Insert *nlines* lines into the specified window above the current line. The
Georg Brandl116aa622007-08-15 14:28:22 +00001036 *nlines* bottom lines are lost. For negative *nlines*, delete *nlines* lines
1037 starting with the one under the cursor, and move the remaining lines up. The
1038 bottom *nlines* lines are cleared. The current cursor position remains the
1039 same.
1040
1041
1042.. method:: window.insertln()
1043
Ezio Melottib6b73712011-06-26 13:38:11 +03001044 Insert a blank line under the cursor. All following lines are moved down by one
Georg Brandl116aa622007-08-15 14:28:22 +00001045 line.
1046
1047
Ezio Melottie0add762012-09-14 06:32:35 +03001048.. method:: window.insnstr(str, n[, attr])
1049 window.insnstr(y, x, str, n[, attr])
Georg Brandl116aa622007-08-15 14:28:22 +00001050
1051 Insert a character string (as many characters as will fit on the line) before
1052 the character under the cursor, up to *n* characters. If *n* is zero or
1053 negative, the entire string is inserted. All characters to the right of the
1054 cursor are shifted right, with the rightmost characters on the line being lost.
1055 The cursor position does not change (after moving to *y*, *x*, if specified).
1056
1057
Ezio Melottie0add762012-09-14 06:32:35 +03001058.. method:: window.insstr(str[, attr])
1059 window.insstr(y, x, str[, attr])
Georg Brandl116aa622007-08-15 14:28:22 +00001060
1061 Insert a character string (as many characters as will fit on the line) before
1062 the character under the cursor. All characters to the right of the cursor are
1063 shifted right, with the rightmost characters on the line being lost. The cursor
1064 position does not change (after moving to *y*, *x*, if specified).
1065
1066
Ezio Melottie0add762012-09-14 06:32:35 +03001067.. method:: window.instr([n])
1068 window.instr(y, x[, n])
Georg Brandl116aa622007-08-15 14:28:22 +00001069
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001070 Return a bytes object of characters, extracted from the window starting at the
Georg Brandl116aa622007-08-15 14:28:22 +00001071 current cursor position, or at *y*, *x* if specified. Attributes are stripped
Ezio Melottib6b73712011-06-26 13:38:11 +03001072 from the characters. If *n* is specified, :meth:`instr` returns a string
Georg Brandl116aa622007-08-15 14:28:22 +00001073 at most *n* characters long (exclusive of the trailing NUL).
1074
1075
1076.. method:: window.is_linetouched(line)
1077
Ezio Melottib6b73712011-06-26 13:38:11 +03001078 Return ``True`` if the specified line was modified since the last call to
1079 :meth:`refresh`; otherwise return ``False``. Raise a :exc:`curses.error`
Georg Brandl116aa622007-08-15 14:28:22 +00001080 exception if *line* is not valid for the given window.
1081
1082
1083.. method:: window.is_wintouched()
1084
Ezio Melottib6b73712011-06-26 13:38:11 +03001085 Return ``True`` if the specified window was modified since the last call to
1086 :meth:`refresh`; otherwise return ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +00001087
1088
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001089.. method:: window.keypad(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001090
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001091 If *flag* is ``True``, escape sequences generated by some keys (keypad, function keys)
1092 will be interpreted by :mod:`curses`. If *flag* is ``False``, escape sequences will be
Georg Brandl116aa622007-08-15 14:28:22 +00001093 left as is in the input stream.
1094
1095
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001096.. method:: window.leaveok(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001097
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001098 If *flag* is ``True``, cursor is left where it is on update, instead of being at "cursor
Georg Brandl116aa622007-08-15 14:28:22 +00001099 position." This reduces cursor movement where possible. If possible the cursor
1100 will be made invisible.
1101
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001102 If *flag* is ``False``, cursor will always be at "cursor position" after an update.
Georg Brandl116aa622007-08-15 14:28:22 +00001103
1104
1105.. method:: window.move(new_y, new_x)
1106
1107 Move cursor to ``(new_y, new_x)``.
1108
1109
1110.. method:: window.mvderwin(y, x)
1111
Ezio Melottib6b73712011-06-26 13:38:11 +03001112 Move the window inside its parent window. The screen-relative parameters of
Georg Brandl116aa622007-08-15 14:28:22 +00001113 the window are not changed. This routine is used to display different parts of
1114 the parent window at the same physical position on the screen.
1115
1116
1117.. method:: window.mvwin(new_y, new_x)
1118
1119 Move the window so its upper-left corner is at ``(new_y, new_x)``.
1120
1121
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001122.. method:: window.nodelay(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001123
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001124 If *flag* is ``True``, :meth:`getch` will be non-blocking.
Georg Brandl116aa622007-08-15 14:28:22 +00001125
1126
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001127.. method:: window.notimeout(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001128
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001129 If *flag* is ``True``, escape sequences will not be timed out.
Georg Brandl116aa622007-08-15 14:28:22 +00001130
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001131 If *flag* is ``False``, after a few milliseconds, an escape sequence will not be
Georg Brandl116aa622007-08-15 14:28:22 +00001132 interpreted, and will be left in the input stream as is.
1133
1134
1135.. method:: window.noutrefresh()
1136
1137 Mark for refresh but wait. This function updates the data structure
1138 representing the desired state of the window, but does not force an update of
1139 the physical screen. To accomplish that, call :func:`doupdate`.
1140
1141
1142.. method:: window.overlay(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])
1143
1144 Overlay the window on top of *destwin*. The windows need not be the same size,
1145 only the overlapping region is copied. This copy is non-destructive, which means
1146 that the current background character does not overwrite the old contents of
1147 *destwin*.
1148
1149 To get fine-grained control over the copied region, the second form of
1150 :meth:`overlay` can be used. *sminrow* and *smincol* are the upper-left
1151 coordinates of the source window, and the other variables mark a rectangle in
1152 the destination window.
1153
1154
1155.. method:: window.overwrite(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])
1156
1157 Overwrite the window on top of *destwin*. The windows need not be the same size,
1158 in which case only the overlapping region is copied. This copy is destructive,
1159 which means that the current background character overwrites the old contents of
1160 *destwin*.
1161
1162 To get fine-grained control over the copied region, the second form of
1163 :meth:`overwrite` can be used. *sminrow* and *smincol* are the upper-left
1164 coordinates of the source window, the other variables mark a rectangle in the
1165 destination window.
1166
1167
1168.. method:: window.putwin(file)
1169
Ezio Melottib6b73712011-06-26 13:38:11 +03001170 Write all data associated with the window into the provided file object. This
Georg Brandl116aa622007-08-15 14:28:22 +00001171 information can be later retrieved using the :func:`getwin` function.
1172
1173
1174.. method:: window.redrawln(beg, num)
1175
Ezio Melottib6b73712011-06-26 13:38:11 +03001176 Indicate that the *num* screen lines, starting at line *beg*, are corrupted and
Georg Brandl116aa622007-08-15 14:28:22 +00001177 should be completely redrawn on the next :meth:`refresh` call.
1178
1179
1180.. method:: window.redrawwin()
1181
Ezio Melottib6b73712011-06-26 13:38:11 +03001182 Touch the entire window, causing it to be completely redrawn on the next
Georg Brandl116aa622007-08-15 14:28:22 +00001183 :meth:`refresh` call.
1184
1185
1186.. method:: window.refresh([pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol])
1187
1188 Update the display immediately (sync actual screen with previous
1189 drawing/deleting methods).
1190
1191 The 6 optional arguments can only be specified when the window is a pad created
1192 with :func:`newpad`. The additional parameters are needed to indicate what part
1193 of the pad and screen are involved. *pminrow* and *pmincol* specify the upper
1194 left-hand corner of the rectangle to be displayed in the pad. *sminrow*,
1195 *smincol*, *smaxrow*, and *smaxcol* specify the edges of the rectangle to be
1196 displayed on the screen. The lower right-hand corner of the rectangle to be
1197 displayed in the pad is calculated from the screen coordinates, since the
1198 rectangles must be the same size. Both rectangles must be entirely contained
1199 within their respective structures. Negative values of *pminrow*, *pmincol*,
1200 *sminrow*, or *smincol* are treated as if they were zero.
1201
1202
Ezio Melotti4850d522011-06-26 13:34:56 +03001203.. method:: window.resize(nlines, ncols)
1204
1205 Reallocate storage for a curses window to adjust its dimensions to the
1206 specified values. If either dimension is larger than the current values, the
1207 window's data is filled with blanks that have the current background
1208 rendition (as set by :meth:`bkgdset`) merged into them.
1209
1210
Hynek Schlawack979f37a2012-05-22 16:12:18 +02001211.. method:: window.scroll([lines=1])
Georg Brandl116aa622007-08-15 14:28:22 +00001212
1213 Scroll the screen or scrolling region upward by *lines* lines.
1214
1215
1216.. method:: window.scrollok(flag)
1217
Ezio Melottib6b73712011-06-26 13:38:11 +03001218 Control what happens when the cursor of a window is moved off the edge of the
Georg Brandl116aa622007-08-15 14:28:22 +00001219 window or scrolling region, either as a result of a newline action on the bottom
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001220 line, or typing the last character of the last line. If *flag* is ``False``, the
1221 cursor is left on the bottom line. If *flag* is ``True``, the window is scrolled up
Georg Brandl116aa622007-08-15 14:28:22 +00001222 one line. Note that in order to get the physical scrolling effect on the
1223 terminal, it is also necessary to call :meth:`idlok`.
1224
1225
1226.. method:: window.setscrreg(top, bottom)
1227
1228 Set the scrolling region from line *top* to line *bottom*. All scrolling actions
1229 will take place in this region.
1230
1231
1232.. method:: window.standend()
1233
1234 Turn off the standout attribute. On some terminals this has the side effect of
1235 turning off all attributes.
1236
1237
1238.. method:: window.standout()
1239
1240 Turn on attribute *A_STANDOUT*.
1241
1242
Ezio Melottie0add762012-09-14 06:32:35 +03001243.. method:: window.subpad(begin_y, begin_x)
1244 window.subpad(nlines, ncols, begin_y, begin_x)
Georg Brandl116aa622007-08-15 14:28:22 +00001245
1246 Return a sub-window, whose upper-left corner is at ``(begin_y, begin_x)``, and
1247 whose width/height is *ncols*/*nlines*.
1248
1249
Ezio Melottie0add762012-09-14 06:32:35 +03001250.. method:: window.subwin(begin_y, begin_x)
1251 window.subwin(nlines, ncols, begin_y, begin_x)
Georg Brandl116aa622007-08-15 14:28:22 +00001252
1253 Return a sub-window, whose upper-left corner is at ``(begin_y, begin_x)``, and
1254 whose width/height is *ncols*/*nlines*.
1255
1256 By default, the sub-window will extend from the specified position to the lower
1257 right corner of the window.
1258
1259
1260.. method:: window.syncdown()
1261
Ezio Melottib6b73712011-06-26 13:38:11 +03001262 Touch each location in the window that has been touched in any of its ancestor
Georg Brandl116aa622007-08-15 14:28:22 +00001263 windows. This routine is called by :meth:`refresh`, so it should almost never
1264 be necessary to call it manually.
1265
1266
1267.. method:: window.syncok(flag)
1268
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001269 If *flag* is ``True``, then :meth:`syncup` is called automatically
Georg Brandl116aa622007-08-15 14:28:22 +00001270 whenever there is a change in the window.
1271
1272
1273.. method:: window.syncup()
1274
Ezio Melottib6b73712011-06-26 13:38:11 +03001275 Touch all locations in ancestors of the window that have been changed in the
Georg Brandl116aa622007-08-15 14:28:22 +00001276 window.
1277
1278
1279.. method:: window.timeout(delay)
1280
Ezio Melottib6b73712011-06-26 13:38:11 +03001281 Set blocking or non-blocking read behavior for the window. If *delay* is
Georg Brandl116aa622007-08-15 14:28:22 +00001282 negative, blocking read is used (which will wait indefinitely for input). If
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001283 *delay* is zero, then non-blocking read is used, and :meth:`getch` will
1284 return ``-1`` if no input is waiting. If *delay* is positive, then
1285 :meth:`getch` will block for *delay* milliseconds, and return ``-1`` if there is
Georg Brandl116aa622007-08-15 14:28:22 +00001286 still no input at the end of that time.
1287
1288
1289.. method:: window.touchline(start, count[, changed])
1290
1291 Pretend *count* lines have been changed, starting with line *start*. If
1292 *changed* is supplied, it specifies whether the affected lines are marked as
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001293 having been changed (*changed*\ ``=True``) or unchanged (*changed*\ ``=False``).
Georg Brandl116aa622007-08-15 14:28:22 +00001294
1295
1296.. method:: window.touchwin()
1297
1298 Pretend the whole window has been changed, for purposes of drawing
1299 optimizations.
1300
1301
1302.. method:: window.untouchwin()
1303
Ezio Melottib6b73712011-06-26 13:38:11 +03001304 Mark all lines in the window as unchanged since the last call to
Georg Brandl116aa622007-08-15 14:28:22 +00001305 :meth:`refresh`.
1306
1307
Ezio Melottie0add762012-09-14 06:32:35 +03001308.. method:: window.vline(ch, n)
1309 window.vline(y, x, ch, n)
Georg Brandl116aa622007-08-15 14:28:22 +00001310
1311 Display a vertical line starting at ``(y, x)`` with length *n* consisting of the
1312 character *ch*.
1313
1314
1315Constants
1316---------
1317
1318The :mod:`curses` module defines the following data members:
1319
1320
1321.. data:: ERR
1322
Anthony Sottilea26ace12019-09-13 23:17:09 -07001323 Some curses routines that return an integer, such as :meth:`~window.getch`, return
Georg Brandl116aa622007-08-15 14:28:22 +00001324 :const:`ERR` upon failure.
1325
1326
1327.. data:: OK
1328
1329 Some curses routines that return an integer, such as :func:`napms`, return
1330 :const:`OK` upon success.
1331
1332
1333.. data:: version
1334
Serhiy Storchaka300dd552017-10-04 22:44:13 +03001335 A bytes object representing the current version of the module. Also available as
Georg Brandl116aa622007-08-15 14:28:22 +00001336 :const:`__version__`.
1337
Serhiy Storchakab232df92018-10-30 13:22:42 +02001338
1339.. data:: ncurses_version
1340
1341 A named tuple containing the three components of the ncurses library
1342 version: *major*, *minor*, and *patch*. All values are integers. The
1343 components can also be accessed by name, so ``curses.ncurses_version[0]``
1344 is equivalent to ``curses.ncurses_version.major`` and so on.
1345
1346 Availability: if the ncurses library is used.
1347
1348 .. versionadded:: 3.8
1349
1350
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001351Some constants are available to specify character cell attributes.
1352The exact constants available are system dependent.
Georg Brandl116aa622007-08-15 14:28:22 +00001353
1354+------------------+-------------------------------+
1355| Attribute | Meaning |
1356+==================+===============================+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001357| ``A_ALTCHARSET`` | Alternate character set mode |
Georg Brandl116aa622007-08-15 14:28:22 +00001358+------------------+-------------------------------+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001359| ``A_BLINK`` | Blink mode |
Georg Brandl116aa622007-08-15 14:28:22 +00001360+------------------+-------------------------------+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001361| ``A_BOLD`` | Bold mode |
Georg Brandl116aa622007-08-15 14:28:22 +00001362+------------------+-------------------------------+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001363| ``A_DIM`` | Dim mode |
Eijebongab7886b2017-04-26 17:17:12 +02001364+------------------+-------------------------------+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001365| ``A_INVIS`` | Invisible or blank mode |
Georg Brandl116aa622007-08-15 14:28:22 +00001366+------------------+-------------------------------+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001367| ``A_ITALIC`` | Italic mode |
1368+------------------+-------------------------------+
1369| ``A_NORMAL`` | Normal attribute |
1370+------------------+-------------------------------+
1371| ``A_PROTECT`` | Protected mode |
Georg Brandl116aa622007-08-15 14:28:22 +00001372+------------------+-------------------------------+
Georg Brandl931e5c12011-03-06 11:08:35 +01001373| ``A_REVERSE`` | Reverse background and |
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001374| | foreground colors |
Georg Brandl931e5c12011-03-06 11:08:35 +01001375+------------------+-------------------------------+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001376| ``A_STANDOUT`` | Standout mode |
Georg Brandl116aa622007-08-15 14:28:22 +00001377+------------------+-------------------------------+
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001378| ``A_UNDERLINE`` | Underline mode |
1379+------------------+-------------------------------+
1380| ``A_HORIZONTAL`` | Horizontal highlight |
1381+------------------+-------------------------------+
1382| ``A_LEFT`` | Left highlight |
1383+------------------+-------------------------------+
1384| ``A_LOW`` | Low highlight |
1385+------------------+-------------------------------+
1386| ``A_RIGHT`` | Right highlight |
1387+------------------+-------------------------------+
1388| ``A_TOP`` | Top highlight |
1389+------------------+-------------------------------+
1390| ``A_VERTICAL`` | Vertical highlight |
1391+------------------+-------------------------------+
1392| ``A_CHARTEXT`` | Bit-mask to extract a |
1393| | character |
Georg Brandl116aa622007-08-15 14:28:22 +00001394+------------------+-------------------------------+
1395
Eijebongab7886b2017-04-26 17:17:12 +02001396.. versionadded:: 3.7
1397 ``A_ITALIC`` was added.
1398
Xiang Zhang116dd5e2017-06-16 11:20:07 +08001399Several constants are available to extract corresponding attributes returned
1400by some methods.
1401
1402+------------------+-------------------------------+
1403| Bit-mask | Meaning |
1404+==================+===============================+
1405| ``A_ATTRIBUTES`` | Bit-mask to extract |
1406| | attributes |
1407+------------------+-------------------------------+
1408| ``A_CHARTEXT`` | Bit-mask to extract a |
1409| | character |
1410+------------------+-------------------------------+
1411| ``A_COLOR`` | Bit-mask to extract |
1412| | color-pair field information |
1413+------------------+-------------------------------+
1414
Georg Brandl116aa622007-08-15 14:28:22 +00001415Keys are referred to by integer constants with names starting with ``KEY_``.
1416The exact keycaps available are system dependent.
1417
Christian Heimes5b5e81c2007-12-31 16:14:33 +00001418.. XXX this table is far too large! should it be alphabetized?
Georg Brandl116aa622007-08-15 14:28:22 +00001419
1420+-------------------+--------------------------------------------+
1421| Key constant | Key |
1422+===================+============================================+
1423| ``KEY_MIN`` | Minimum key value |
1424+-------------------+--------------------------------------------+
1425| ``KEY_BREAK`` | Break key (unreliable) |
1426+-------------------+--------------------------------------------+
1427| ``KEY_DOWN`` | Down-arrow |
1428+-------------------+--------------------------------------------+
1429| ``KEY_UP`` | Up-arrow |
1430+-------------------+--------------------------------------------+
1431| ``KEY_LEFT`` | Left-arrow |
1432+-------------------+--------------------------------------------+
1433| ``KEY_RIGHT`` | Right-arrow |
1434+-------------------+--------------------------------------------+
1435| ``KEY_HOME`` | Home key (upward+left arrow) |
1436+-------------------+--------------------------------------------+
1437| ``KEY_BACKSPACE`` | Backspace (unreliable) |
1438+-------------------+--------------------------------------------+
1439| ``KEY_F0`` | Function keys. Up to 64 function keys are |
1440| | supported. |
1441+-------------------+--------------------------------------------+
1442| ``KEY_Fn`` | Value of function key *n* |
1443+-------------------+--------------------------------------------+
1444| ``KEY_DL`` | Delete line |
1445+-------------------+--------------------------------------------+
1446| ``KEY_IL`` | Insert line |
1447+-------------------+--------------------------------------------+
1448| ``KEY_DC`` | Delete character |
1449+-------------------+--------------------------------------------+
1450| ``KEY_IC`` | Insert char or enter insert mode |
1451+-------------------+--------------------------------------------+
1452| ``KEY_EIC`` | Exit insert char mode |
1453+-------------------+--------------------------------------------+
1454| ``KEY_CLEAR`` | Clear screen |
1455+-------------------+--------------------------------------------+
1456| ``KEY_EOS`` | Clear to end of screen |
1457+-------------------+--------------------------------------------+
1458| ``KEY_EOL`` | Clear to end of line |
1459+-------------------+--------------------------------------------+
1460| ``KEY_SF`` | Scroll 1 line forward |
1461+-------------------+--------------------------------------------+
1462| ``KEY_SR`` | Scroll 1 line backward (reverse) |
1463+-------------------+--------------------------------------------+
1464| ``KEY_NPAGE`` | Next page |
1465+-------------------+--------------------------------------------+
1466| ``KEY_PPAGE`` | Previous page |
1467+-------------------+--------------------------------------------+
1468| ``KEY_STAB`` | Set tab |
1469+-------------------+--------------------------------------------+
1470| ``KEY_CTAB`` | Clear tab |
1471+-------------------+--------------------------------------------+
1472| ``KEY_CATAB`` | Clear all tabs |
1473+-------------------+--------------------------------------------+
1474| ``KEY_ENTER`` | Enter or send (unreliable) |
1475+-------------------+--------------------------------------------+
1476| ``KEY_SRESET`` | Soft (partial) reset (unreliable) |
1477+-------------------+--------------------------------------------+
1478| ``KEY_RESET`` | Reset or hard reset (unreliable) |
1479+-------------------+--------------------------------------------+
1480| ``KEY_PRINT`` | Print |
1481+-------------------+--------------------------------------------+
1482| ``KEY_LL`` | Home down or bottom (lower left) |
1483+-------------------+--------------------------------------------+
1484| ``KEY_A1`` | Upper left of keypad |
1485+-------------------+--------------------------------------------+
1486| ``KEY_A3`` | Upper right of keypad |
1487+-------------------+--------------------------------------------+
1488| ``KEY_B2`` | Center of keypad |
1489+-------------------+--------------------------------------------+
1490| ``KEY_C1`` | Lower left of keypad |
1491+-------------------+--------------------------------------------+
1492| ``KEY_C3`` | Lower right of keypad |
1493+-------------------+--------------------------------------------+
1494| ``KEY_BTAB`` | Back tab |
1495+-------------------+--------------------------------------------+
1496| ``KEY_BEG`` | Beg (beginning) |
1497+-------------------+--------------------------------------------+
1498| ``KEY_CANCEL`` | Cancel |
1499+-------------------+--------------------------------------------+
1500| ``KEY_CLOSE`` | Close |
1501+-------------------+--------------------------------------------+
1502| ``KEY_COMMAND`` | Cmd (command) |
1503+-------------------+--------------------------------------------+
1504| ``KEY_COPY`` | Copy |
1505+-------------------+--------------------------------------------+
1506| ``KEY_CREATE`` | Create |
1507+-------------------+--------------------------------------------+
1508| ``KEY_END`` | End |
1509+-------------------+--------------------------------------------+
1510| ``KEY_EXIT`` | Exit |
1511+-------------------+--------------------------------------------+
1512| ``KEY_FIND`` | Find |
1513+-------------------+--------------------------------------------+
1514| ``KEY_HELP`` | Help |
1515+-------------------+--------------------------------------------+
1516| ``KEY_MARK`` | Mark |
1517+-------------------+--------------------------------------------+
1518| ``KEY_MESSAGE`` | Message |
1519+-------------------+--------------------------------------------+
1520| ``KEY_MOVE`` | Move |
1521+-------------------+--------------------------------------------+
1522| ``KEY_NEXT`` | Next |
1523+-------------------+--------------------------------------------+
1524| ``KEY_OPEN`` | Open |
1525+-------------------+--------------------------------------------+
1526| ``KEY_OPTIONS`` | Options |
1527+-------------------+--------------------------------------------+
1528| ``KEY_PREVIOUS`` | Prev (previous) |
1529+-------------------+--------------------------------------------+
1530| ``KEY_REDO`` | Redo |
1531+-------------------+--------------------------------------------+
1532| ``KEY_REFERENCE`` | Ref (reference) |
1533+-------------------+--------------------------------------------+
1534| ``KEY_REFRESH`` | Refresh |
1535+-------------------+--------------------------------------------+
1536| ``KEY_REPLACE`` | Replace |
1537+-------------------+--------------------------------------------+
1538| ``KEY_RESTART`` | Restart |
1539+-------------------+--------------------------------------------+
1540| ``KEY_RESUME`` | Resume |
1541+-------------------+--------------------------------------------+
1542| ``KEY_SAVE`` | Save |
1543+-------------------+--------------------------------------------+
1544| ``KEY_SBEG`` | Shifted Beg (beginning) |
1545+-------------------+--------------------------------------------+
1546| ``KEY_SCANCEL`` | Shifted Cancel |
1547+-------------------+--------------------------------------------+
1548| ``KEY_SCOMMAND`` | Shifted Command |
1549+-------------------+--------------------------------------------+
1550| ``KEY_SCOPY`` | Shifted Copy |
1551+-------------------+--------------------------------------------+
1552| ``KEY_SCREATE`` | Shifted Create |
1553+-------------------+--------------------------------------------+
1554| ``KEY_SDC`` | Shifted Delete char |
1555+-------------------+--------------------------------------------+
1556| ``KEY_SDL`` | Shifted Delete line |
1557+-------------------+--------------------------------------------+
1558| ``KEY_SELECT`` | Select |
1559+-------------------+--------------------------------------------+
1560| ``KEY_SEND`` | Shifted End |
1561+-------------------+--------------------------------------------+
1562| ``KEY_SEOL`` | Shifted Clear line |
1563+-------------------+--------------------------------------------+
Mariatta64508782017-03-19 20:48:04 -07001564| ``KEY_SEXIT`` | Shifted Exit |
Georg Brandl116aa622007-08-15 14:28:22 +00001565+-------------------+--------------------------------------------+
1566| ``KEY_SFIND`` | Shifted Find |
1567+-------------------+--------------------------------------------+
1568| ``KEY_SHELP`` | Shifted Help |
1569+-------------------+--------------------------------------------+
1570| ``KEY_SHOME`` | Shifted Home |
1571+-------------------+--------------------------------------------+
1572| ``KEY_SIC`` | Shifted Input |
1573+-------------------+--------------------------------------------+
1574| ``KEY_SLEFT`` | Shifted Left arrow |
1575+-------------------+--------------------------------------------+
1576| ``KEY_SMESSAGE`` | Shifted Message |
1577+-------------------+--------------------------------------------+
1578| ``KEY_SMOVE`` | Shifted Move |
1579+-------------------+--------------------------------------------+
1580| ``KEY_SNEXT`` | Shifted Next |
1581+-------------------+--------------------------------------------+
1582| ``KEY_SOPTIONS`` | Shifted Options |
1583+-------------------+--------------------------------------------+
1584| ``KEY_SPREVIOUS`` | Shifted Prev |
1585+-------------------+--------------------------------------------+
1586| ``KEY_SPRINT`` | Shifted Print |
1587+-------------------+--------------------------------------------+
1588| ``KEY_SREDO`` | Shifted Redo |
1589+-------------------+--------------------------------------------+
1590| ``KEY_SREPLACE`` | Shifted Replace |
1591+-------------------+--------------------------------------------+
1592| ``KEY_SRIGHT`` | Shifted Right arrow |
1593+-------------------+--------------------------------------------+
1594| ``KEY_SRSUME`` | Shifted Resume |
1595+-------------------+--------------------------------------------+
1596| ``KEY_SSAVE`` | Shifted Save |
1597+-------------------+--------------------------------------------+
1598| ``KEY_SSUSPEND`` | Shifted Suspend |
1599+-------------------+--------------------------------------------+
1600| ``KEY_SUNDO`` | Shifted Undo |
1601+-------------------+--------------------------------------------+
1602| ``KEY_SUSPEND`` | Suspend |
1603+-------------------+--------------------------------------------+
1604| ``KEY_UNDO`` | Undo |
1605+-------------------+--------------------------------------------+
1606| ``KEY_MOUSE`` | Mouse event has occurred |
1607+-------------------+--------------------------------------------+
1608| ``KEY_RESIZE`` | Terminal resize event |
1609+-------------------+--------------------------------------------+
1610| ``KEY_MAX`` | Maximum key value |
1611+-------------------+--------------------------------------------+
1612
1613On VT100s and their software emulations, such as X terminal emulators, there are
1614normally at least four function keys (:const:`KEY_F1`, :const:`KEY_F2`,
1615:const:`KEY_F3`, :const:`KEY_F4`) available, and the arrow keys mapped to
1616:const:`KEY_UP`, :const:`KEY_DOWN`, :const:`KEY_LEFT` and :const:`KEY_RIGHT` in
1617the obvious way. If your machine has a PC keyboard, it is safe to expect arrow
1618keys and twelve function keys (older PC keyboards may have only ten function
1619keys); also, the following keypad mappings are standard:
1620
1621+------------------+-----------+
1622| Keycap | Constant |
1623+==================+===========+
1624| :kbd:`Insert` | KEY_IC |
1625+------------------+-----------+
1626| :kbd:`Delete` | KEY_DC |
1627+------------------+-----------+
1628| :kbd:`Home` | KEY_HOME |
1629+------------------+-----------+
1630| :kbd:`End` | KEY_END |
1631+------------------+-----------+
Berker Peksagaf836392016-04-02 04:48:27 +03001632| :kbd:`Page Up` | KEY_PPAGE |
Georg Brandl116aa622007-08-15 14:28:22 +00001633+------------------+-----------+
Berker Peksagaf836392016-04-02 04:48:27 +03001634| :kbd:`Page Down` | KEY_NPAGE |
Georg Brandl116aa622007-08-15 14:28:22 +00001635+------------------+-----------+
1636
1637The following table lists characters from the alternate character set. These are
1638inherited from the VT100 terminal, and will generally be available on software
1639emulations such as X terminals. When there is no graphic available, curses
1640falls back on a crude printable ASCII approximation.
1641
1642.. note::
1643
1644 These are available only after :func:`initscr` has been called.
1645
1646+------------------+------------------------------------------+
1647| ACS code | Meaning |
1648+==================+==========================================+
1649| ``ACS_BBSS`` | alternate name for upper right corner |
1650+------------------+------------------------------------------+
1651| ``ACS_BLOCK`` | solid square block |
1652+------------------+------------------------------------------+
1653| ``ACS_BOARD`` | board of squares |
1654+------------------+------------------------------------------+
1655| ``ACS_BSBS`` | alternate name for horizontal line |
1656+------------------+------------------------------------------+
1657| ``ACS_BSSB`` | alternate name for upper left corner |
1658+------------------+------------------------------------------+
1659| ``ACS_BSSS`` | alternate name for top tee |
1660+------------------+------------------------------------------+
1661| ``ACS_BTEE`` | bottom tee |
1662+------------------+------------------------------------------+
1663| ``ACS_BULLET`` | bullet |
1664+------------------+------------------------------------------+
1665| ``ACS_CKBOARD`` | checker board (stipple) |
1666+------------------+------------------------------------------+
1667| ``ACS_DARROW`` | arrow pointing down |
1668+------------------+------------------------------------------+
1669| ``ACS_DEGREE`` | degree symbol |
1670+------------------+------------------------------------------+
1671| ``ACS_DIAMOND`` | diamond |
1672+------------------+------------------------------------------+
1673| ``ACS_GEQUAL`` | greater-than-or-equal-to |
1674+------------------+------------------------------------------+
1675| ``ACS_HLINE`` | horizontal line |
1676+------------------+------------------------------------------+
1677| ``ACS_LANTERN`` | lantern symbol |
1678+------------------+------------------------------------------+
1679| ``ACS_LARROW`` | left arrow |
1680+------------------+------------------------------------------+
1681| ``ACS_LEQUAL`` | less-than-or-equal-to |
1682+------------------+------------------------------------------+
1683| ``ACS_LLCORNER`` | lower left-hand corner |
1684+------------------+------------------------------------------+
1685| ``ACS_LRCORNER`` | lower right-hand corner |
1686+------------------+------------------------------------------+
1687| ``ACS_LTEE`` | left tee |
1688+------------------+------------------------------------------+
1689| ``ACS_NEQUAL`` | not-equal sign |
1690+------------------+------------------------------------------+
1691| ``ACS_PI`` | letter pi |
1692+------------------+------------------------------------------+
1693| ``ACS_PLMINUS`` | plus-or-minus sign |
1694+------------------+------------------------------------------+
1695| ``ACS_PLUS`` | big plus sign |
1696+------------------+------------------------------------------+
1697| ``ACS_RARROW`` | right arrow |
1698+------------------+------------------------------------------+
1699| ``ACS_RTEE`` | right tee |
1700+------------------+------------------------------------------+
1701| ``ACS_S1`` | scan line 1 |
1702+------------------+------------------------------------------+
1703| ``ACS_S3`` | scan line 3 |
1704+------------------+------------------------------------------+
1705| ``ACS_S7`` | scan line 7 |
1706+------------------+------------------------------------------+
1707| ``ACS_S9`` | scan line 9 |
1708+------------------+------------------------------------------+
1709| ``ACS_SBBS`` | alternate name for lower right corner |
1710+------------------+------------------------------------------+
1711| ``ACS_SBSB`` | alternate name for vertical line |
1712+------------------+------------------------------------------+
1713| ``ACS_SBSS`` | alternate name for right tee |
1714+------------------+------------------------------------------+
1715| ``ACS_SSBB`` | alternate name for lower left corner |
1716+------------------+------------------------------------------+
1717| ``ACS_SSBS`` | alternate name for bottom tee |
1718+------------------+------------------------------------------+
1719| ``ACS_SSSB`` | alternate name for left tee |
1720+------------------+------------------------------------------+
1721| ``ACS_SSSS`` | alternate name for crossover or big plus |
1722+------------------+------------------------------------------+
1723| ``ACS_STERLING`` | pound sterling |
1724+------------------+------------------------------------------+
1725| ``ACS_TTEE`` | top tee |
1726+------------------+------------------------------------------+
1727| ``ACS_UARROW`` | up arrow |
1728+------------------+------------------------------------------+
1729| ``ACS_ULCORNER`` | upper left corner |
1730+------------------+------------------------------------------+
1731| ``ACS_URCORNER`` | upper right corner |
1732+------------------+------------------------------------------+
1733| ``ACS_VLINE`` | vertical line |
1734+------------------+------------------------------------------+
1735
1736The following table lists the predefined colors:
1737
1738+-------------------+----------------------------+
1739| Constant | Color |
1740+===================+============================+
1741| ``COLOR_BLACK`` | Black |
1742+-------------------+----------------------------+
1743| ``COLOR_BLUE`` | Blue |
1744+-------------------+----------------------------+
1745| ``COLOR_CYAN`` | Cyan (light greenish blue) |
1746+-------------------+----------------------------+
1747| ``COLOR_GREEN`` | Green |
1748+-------------------+----------------------------+
1749| ``COLOR_MAGENTA`` | Magenta (purplish red) |
1750+-------------------+----------------------------+
1751| ``COLOR_RED`` | Red |
1752+-------------------+----------------------------+
1753| ``COLOR_WHITE`` | White |
1754+-------------------+----------------------------+
1755| ``COLOR_YELLOW`` | Yellow |
1756+-------------------+----------------------------+
1757
1758
1759:mod:`curses.textpad` --- Text input widget for curses programs
1760===============================================================
1761
1762.. module:: curses.textpad
1763 :synopsis: Emacs-like input editing in a curses window.
1764.. moduleauthor:: Eric Raymond <esr@thyrsus.com>
1765.. sectionauthor:: Eric Raymond <esr@thyrsus.com>
1766
1767
Georg Brandl116aa622007-08-15 14:28:22 +00001768The :mod:`curses.textpad` module provides a :class:`Textbox` class that handles
1769elementary text editing in a curses window, supporting a set of keybindings
1770resembling those of Emacs (thus, also of Netscape Navigator, BBedit 6.x,
1771FrameMaker, and many other programs). The module also provides a
1772rectangle-drawing function useful for framing text boxes or for other purposes.
1773
1774The module :mod:`curses.textpad` defines the following function:
1775
1776
1777.. function:: rectangle(win, uly, ulx, lry, lrx)
1778
1779 Draw a rectangle. The first argument must be a window object; the remaining
1780 arguments are coordinates relative to that window. The second and third
1781 arguments are the y and x coordinates of the upper left hand corner of the
1782 rectangle to be drawn; the fourth and fifth arguments are the y and x
1783 coordinates of the lower right hand corner. The rectangle will be drawn using
1784 VT100/IBM PC forms characters on terminals that make this possible (including
1785 xterm and most other software terminal emulators). Otherwise it will be drawn
1786 with ASCII dashes, vertical bars, and plus signs.
1787
1788
1789.. _curses-textpad-objects:
1790
1791Textbox objects
1792---------------
1793
1794You can instantiate a :class:`Textbox` object as follows:
1795
1796
1797.. class:: Textbox(win)
1798
1799 Return a textbox widget object. The *win* argument should be a curses
Berker Peksag93fc20b2017-05-23 03:16:07 +03001800 :ref:`window <curses-window-objects>` object in which the textbox is to
1801 be contained. The edit cursor of the textbox is initially located at the
1802 upper left hand corner of the containing window, with coordinates ``(0, 0)``.
1803 The instance's :attr:`stripspaces` flag is initially on.
Georg Brandl116aa622007-08-15 14:28:22 +00001804
Benjamin Petersone41251e2008-04-25 01:59:09 +00001805 :class:`Textbox` objects have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001806
1807
Benjamin Petersone41251e2008-04-25 01:59:09 +00001808 .. method:: edit([validator])
Georg Brandl116aa622007-08-15 14:28:22 +00001809
Benjamin Petersone41251e2008-04-25 01:59:09 +00001810 This is the entry point you will normally use. It accepts editing
1811 keystrokes until one of the termination keystrokes is entered. If
1812 *validator* is supplied, it must be a function. It will be called for
1813 each keystroke entered with the keystroke as a parameter; command dispatch
1814 is done on the result. This method returns the window contents as a
1815 string; whether blanks in the window are included is affected by the
Senthil Kumarana6bac952011-07-04 11:28:30 -07001816 :attr:`stripspaces` attribute.
Georg Brandl116aa622007-08-15 14:28:22 +00001817
1818
Benjamin Petersone41251e2008-04-25 01:59:09 +00001819 .. method:: do_command(ch)
Georg Brandl116aa622007-08-15 14:28:22 +00001820
Benjamin Petersone41251e2008-04-25 01:59:09 +00001821 Process a single command keystroke. Here are the supported special
1822 keystrokes:
Georg Brandl116aa622007-08-15 14:28:22 +00001823
Benjamin Petersone41251e2008-04-25 01:59:09 +00001824 +------------------+-------------------------------------------+
1825 | Keystroke | Action |
1826 +==================+===========================================+
1827 | :kbd:`Control-A` | Go to left edge of window. |
1828 +------------------+-------------------------------------------+
1829 | :kbd:`Control-B` | Cursor left, wrapping to previous line if |
1830 | | appropriate. |
1831 +------------------+-------------------------------------------+
1832 | :kbd:`Control-D` | Delete character under cursor. |
1833 +------------------+-------------------------------------------+
1834 | :kbd:`Control-E` | Go to right edge (stripspaces off) or end |
1835 | | of line (stripspaces on). |
1836 +------------------+-------------------------------------------+
1837 | :kbd:`Control-F` | Cursor right, wrapping to next line when |
1838 | | appropriate. |
1839 +------------------+-------------------------------------------+
1840 | :kbd:`Control-G` | Terminate, returning the window contents. |
1841 +------------------+-------------------------------------------+
1842 | :kbd:`Control-H` | Delete character backward. |
1843 +------------------+-------------------------------------------+
1844 | :kbd:`Control-J` | Terminate if the window is 1 line, |
1845 | | otherwise insert newline. |
1846 +------------------+-------------------------------------------+
1847 | :kbd:`Control-K` | If line is blank, delete it, otherwise |
1848 | | clear to end of line. |
1849 +------------------+-------------------------------------------+
1850 | :kbd:`Control-L` | Refresh screen. |
1851 +------------------+-------------------------------------------+
1852 | :kbd:`Control-N` | Cursor down; move down one line. |
1853 +------------------+-------------------------------------------+
1854 | :kbd:`Control-O` | Insert a blank line at cursor location. |
1855 +------------------+-------------------------------------------+
1856 | :kbd:`Control-P` | Cursor up; move up one line. |
1857 +------------------+-------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001858
Benjamin Petersone41251e2008-04-25 01:59:09 +00001859 Move operations do nothing if the cursor is at an edge where the movement
1860 is not possible. The following synonyms are supported where possible:
Georg Brandl116aa622007-08-15 14:28:22 +00001861
Benjamin Petersone41251e2008-04-25 01:59:09 +00001862 +------------------------+------------------+
1863 | Constant | Keystroke |
1864 +========================+==================+
1865 | :const:`KEY_LEFT` | :kbd:`Control-B` |
1866 +------------------------+------------------+
1867 | :const:`KEY_RIGHT` | :kbd:`Control-F` |
1868 +------------------------+------------------+
1869 | :const:`KEY_UP` | :kbd:`Control-P` |
1870 +------------------------+------------------+
1871 | :const:`KEY_DOWN` | :kbd:`Control-N` |
1872 +------------------------+------------------+
1873 | :const:`KEY_BACKSPACE` | :kbd:`Control-h` |
1874 +------------------------+------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001875
Benjamin Petersone41251e2008-04-25 01:59:09 +00001876 All other keystrokes are treated as a command to insert the given
1877 character and move right (with line wrapping).
Georg Brandl116aa622007-08-15 14:28:22 +00001878
1879
Benjamin Petersone41251e2008-04-25 01:59:09 +00001880 .. method:: gather()
Georg Brandl116aa622007-08-15 14:28:22 +00001881
Ezio Melottib6b73712011-06-26 13:38:11 +03001882 Return the window contents as a string; whether blanks in the
Benjamin Petersone41251e2008-04-25 01:59:09 +00001883 window are included is affected by the :attr:`stripspaces` member.
Georg Brandl116aa622007-08-15 14:28:22 +00001884
1885
Benjamin Petersone41251e2008-04-25 01:59:09 +00001886 .. attribute:: stripspaces
Georg Brandl116aa622007-08-15 14:28:22 +00001887
Senthil Kumarana6bac952011-07-04 11:28:30 -07001888 This attribute is a flag which controls the interpretation of blanks in
Benjamin Petersone41251e2008-04-25 01:59:09 +00001889 the window. When it is on, trailing blanks on each line are ignored; any
1890 cursor motion that would land the cursor on a trailing blank goes to the
1891 end of that line instead, and trailing blanks are stripped when the window
1892 contents are gathered.