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