cinap_lenrekadd hg and python4bb9e2215 years agoHistory
1395 lines · 55.2 KB · TeX
RawDownload
\section{\module{curses} ---         Terminal handling for character-cell displays} \declaremodule{standard}{curses}\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}\sectionauthor{Eric Raymond}{esr@thyrsus.com}\modulesynopsis{An interface to the curses library, providing portable                terminal handling.} \versionchanged[Added support for the \code{ncurses} library and                converted to a package]{1.6} The \module{curses} module provides an interface to the curseslibrary, the de-facto standard for portable advanced terminalhandling. While curses is most widely used in the \UNIX{} environment, versionsare available for DOS, OS/2, and possibly other systems as well.  Thisextension module is designed to match the API of ncurses, anopen-source curses library hosted on Linux and the BSD variants of\UNIX. \begin{seealso}  \seemodule{curses.ascii}{Utilities for working with \ASCII{}                           characters, regardless of your locale                           settings.}  \seemodule{curses.panel}{A panel stack extension that adds depth to                            curses windows.}  \seemodule{curses.textpad}{Editable text widget for curses supporting                              \program{Emacs}-like bindings.}  \seemodule{curses.wrapper}{Convenience function to ensure proper                             terminal setup and resetting on                             application entry and exit.}  \seetitle[http://www.python.org/doc/howto/curses/curses.html]{Curses            Programming with Python}{Tutorial material on using curses            with Python, by Andrew Kuchling and Eric Raymond, is            available on the Python Web site.}  \seetext{The \file{Demo/curses/} directory in the Python source           distribution contains some example programs using the           curses bindings provided by this module.}\end{seealso}  \subsection{Functions \label{curses-functions}} The module \module{curses} defines the following exception: \begin{excdesc}{error}Exception raised when a curses library function returns an error.\end{excdesc} \note{Whenever \var{x} or \var{y} arguments to a functionor a method are optional, they default to the current cursor location.Whenever \var{attr} is optional, it defaults to \constant{A_NORMAL}.} The module \module{curses} defines the following functions: \begin{funcdesc}{baudrate}{}Returns the output speed of the terminal in bits per second.  Onsoftware terminal emulators it will have a fixed high value.Included for historical reasons; in former times, it was used to write output loops for time delays and occasionally to changeinterfaces depending on the line speed.\end{funcdesc} \begin{funcdesc}{beep}{}Emit a short attention sound.\end{funcdesc} \begin{funcdesc}{can_change_color}{}Returns true or false, depending on whether the programmer can changethe colors displayed by the terminal.\end{funcdesc} \begin{funcdesc}{cbreak}{}Enter cbreak mode.  In cbreak mode (sometimes called ``rare'' mode)normal tty line buffering is turned off and characters are availableto be read one by one.  However, unlike raw mode, special characters(interrupt, quit, suspend, and flow control) retain their effects onthe tty driver and calling program.  Calling first \function{raw()}then \function{cbreak()} leaves the terminal in cbreak mode.\end{funcdesc} \begin{funcdesc}{color_content}{color_number}Returns the intensity of the red, green, and blue (RGB) components inthe color \var{color_number}, which must be between \code{0} and\constant{COLORS}.  A 3-tuple is returned, containing the R,G,B valuesfor the given color, which will be between \code{0} (no component) and\code{1000} (maximum amount of component).\end{funcdesc} \begin{funcdesc}{color_pair}{color_number}Returns the attribute value for displaying text in the specifiedcolor.  This attribute value can be combined with\constant{A_STANDOUT}, \constant{A_REVERSE}, and the other\constant{A_*} attributes.  \function{pair_number()} is thecounterpart to this function.\end{funcdesc} \begin{funcdesc}{curs_set}{visibility}Sets the cursor state.  \var{visibility} can be set to 0, 1, or 2, forinvisible, normal, or very visible.  If the terminal supports thevisibility requested, the previous cursor state is returned;otherwise, an exception is raised.  On many terminals, the ``visible''mode is an underline cursor and the ``very visible'' mode is a block cursor.\end{funcdesc} \begin{funcdesc}{def_prog_mode}{}Saves the current terminal mode as the ``program'' mode, the mode whenthe running program is using curses.  (Its counterpart is the``shell'' mode, for when the program is not in curses.)  Subsequent callsto \function{reset_prog_mode()} will restore this mode.\end{funcdesc} \begin{funcdesc}{def_shell_mode}{}Saves the current terminal mode as the ``shell'' mode, the mode whenthe running program is not using curses.  (Its counterpart is the``program'' mode, when the program is using curses capabilities.)Subsequent callsto \function{reset_shell_mode()} will restore this mode.\end{funcdesc} \begin{funcdesc}{delay_output}{ms}Inserts an \var{ms} millisecond pause in output.  \end{funcdesc} \begin{funcdesc}{doupdate}{}Update the physical screen.  The curses library keeps two datastructures, one representing the current physical screen contentsand a virtual screen representing the desired next state.  The\function{doupdate()} ground updates the physical screen to match thevirtual screen. The virtual screen may be updated by a \method{noutrefresh()} callafter write operations such as \method{addstr()} have been performedon a window.  The normal \method{refresh()} call is simply\method{noutrefresh()} followed by \function{doupdate()}; if you haveto update multiple windows, you can speed performance and perhapsreduce screen flicker by issuing \method{noutrefresh()} calls onall windows, followed by a single \function{doupdate()}.\end{funcdesc} \begin{funcdesc}{echo}{}Enter echo mode.  In echo mode, each character input is echoed to thescreen as it is entered.  \end{funcdesc} \begin{funcdesc}{endwin}{}De-initialize the library, and return terminal to normal status.\end{funcdesc} \begin{funcdesc}{erasechar}{}Returns the user's current erase character.  Under \UNIX{} operatingsystems this is a property of the controlling tty of the cursesprogram, and is not set by the curses library itself.\end{funcdesc} \begin{funcdesc}{filter}{}The \function{filter()} routine, if used, must be called before\function{initscr()} is  called.  The effect is that, during thosecalls, LINES is set to 1; the capabilities clear, cup, cud, cud1,cuu1, cuu, vpa are disabled; and the home string is set to the value of cr.The effect is that the cursor is confined to the current line, and soare screen updates.  This may be used for enabling character-at-a-time line editing without touching the rest of the screen.\end{funcdesc} \begin{funcdesc}{flash}{}Flash the screen.  That is, change it to reverse-video and then changeit back in a short interval.  Some people prefer such as `visible bell'to the audible attention signal produced by \function{beep()}.\end{funcdesc} \begin{funcdesc}{flushinp}{}Flush all input buffers.  This throws away any  typeahead  that  hasbeen typed by the user and has not yet been processed by the program.\end{funcdesc} \begin{funcdesc}{getmouse}{}After \method{getch()} returns \constant{KEY_MOUSE} to signal a mouseevent, this method should be call to retrieve the queued mouse event,represented as a 5-tuple\code{(\var{id}, \var{x}, \var{y}, \var{z}, \var{bstate})}.\var{id} is an ID value used to distinguish multiple devices,and \var{x}, \var{y}, \var{z} are the event's coordinates.  (\var{z}is currently unused.).  \var{bstate} is an integer value whose bitswill be set to indicate the type of event, and will be the bitwise ORof one or more of the following constants, where \var{n} is the buttonnumber from 1 to 4:\constant{BUTTON\var{n}_PRESSED},\constant{BUTTON\var{n}_RELEASED},\constant{BUTTON\var{n}_CLICKED},\constant{BUTTON\var{n}_DOUBLE_CLICKED},\constant{BUTTON\var{n}_TRIPLE_CLICKED},\constant{BUTTON_SHIFT},\constant{BUTTON_CTRL},\constant{BUTTON_ALT}.\end{funcdesc} \begin{funcdesc}{getsyx}{}Returns the current coordinates of the virtual screen cursor in y andx.  If leaveok is currently true, then -1,-1 is returned.\end{funcdesc} \begin{funcdesc}{getwin}{file}Reads window related data stored in the file by an earlier\function{putwin()} call.  The routine then creates and initializes anew window using that data, returning the new window object.\end{funcdesc} \begin{funcdesc}{has_colors}{}Returns true if the terminal can display colors; otherwise, itreturns false. \end{funcdesc} \begin{funcdesc}{has_ic}{}Returns true if the terminal has insert- and delete- charactercapabilities.  This function is included for historical reasons only,as all modern software terminal emulators have such capabilities.\end{funcdesc} \begin{funcdesc}{has_il}{}Returns true if the terminal has insert- anddelete-line  capabilities,  or  can  simulate  them  usingscrolling regions. This function is included for historical reasons only,as all modern software terminal emulators have such capabilities.\end{funcdesc} \begin{funcdesc}{has_key}{ch}Takes a key value \var{ch}, and returns true if the current terminaltype recognizes a key with that value.\end{funcdesc} \begin{funcdesc}{halfdelay}{tenths}Used for half-delay mode, which is similar to cbreak mode in thatcharacters typed by the user are immediately available to the program.However, after blocking for \var{tenths} tenths of seconds, anexception is raised if nothing has been typed.  The value of\var{tenths} must be a number between 1 and 255.  Use\function{nocbreak()} to leave half-delay mode.\end{funcdesc} \begin{funcdesc}{init_color}{color_number, r, g, b}Changes the definition of a color, taking the number of the color tobe changed followed by three RGB values (for the amounts of red,green, and blue components).  The value of \var{color_number} must bebetween \code{0} and \constant{COLORS}.  Each of \var{r}, \var{g},\var{b}, must be a value between \code{0} and \code{1000}.  When\function{init_color()} is used, all occurrences of that color on thescreen immediately change to the new definition.  This function is ano-op on most terminals; it is active only if\function{can_change_color()} returns \code{1}.\end{funcdesc} \begin{funcdesc}{init_pair}{pair_number, fg, bg}Changes the definition of a color-pair.  It takes three arguments: thenumber of the color-pair to be changed, the foreground color number,and the background color number.  The value of \var{pair_number} mustbe between \code{1} and \code{COLOR_PAIRS - 1} (the \code{0} colorpair is wired to white on black and cannot be changed).  The value of\var{fg} and \var{bg} arguments must be between \code{0} and\constant{COLORS}.  If the color-pair was previously initialized, thescreen is refreshed and all occurrences of that color-pair are changedto the new definition.\end{funcdesc} \begin{funcdesc}{initscr}{}Initialize the library. Returns a \class{WindowObject} which representsthe whole screen.  \note{If there is an error opening the terminal,the underlying curses library may cause the interpreter to exit.}\end{funcdesc} \begin{funcdesc}{isendwin}{}Returns true if \function{endwin()} has been called (that is, the curses library has been deinitialized).\end{funcdesc} \begin{funcdesc}{keyname}{k}Return the name of the key numbered \var{k}.  The name of a keygenerating printable ASCII character is the key's character.  The nameof a control-key combination is a two-character string consisting of acaret followed by the corresponding printable ASCII character.  Thename of an alt-key combination (128-255) is a string consisting of theprefix `M-' followed by the name of the corresponding ASCII character.\end{funcdesc} \begin{funcdesc}{killchar}{}Returns the user's current line kill character. Under \UNIX{} operatingsystems this is a property of the controlling tty of the cursesprogram, and is not set by the curses library itself.\end{funcdesc} \begin{funcdesc}{longname}{}Returns a string containing the terminfo long name field describing the currentterminal.  The maximum length of a verbose description is 128characters.  It is defined only after the call to\function{initscr()}.\end{funcdesc} \begin{funcdesc}{meta}{yes}If \var{yes} is 1, allow 8-bit characters to be input. If \var{yes} is 0, allow only 7-bit chars.\end{funcdesc} \begin{funcdesc}{mouseinterval}{interval}Sets the maximum time in milliseconds that can elapse between press andrelease events in order for them to be recognized as a click, andreturns the previous interval value.  The default value is 200 msec,or one fifth of a second.\end{funcdesc} \begin{funcdesc}{mousemask}{mousemask}Sets the mouse events to be reported, and returns a tuple\code{(\var{availmask}, \var{oldmask})}.  \var{availmask} indicates which of thespecified mouse events can be reported; on complete failure it returns0.  \var{oldmask} is the previous value of the given window's mouseevent mask.  If this function is never called, no mouse events areever reported.\end{funcdesc} \begin{funcdesc}{napms}{ms}Sleep for \var{ms} milliseconds.\end{funcdesc} \begin{funcdesc}{newpad}{nlines, ncols}Creates and returns a pointer to a new pad data structure with thegiven number of lines and columns.  A pad is returned as awindow object. A pad is like a window, except that it is not restricted by the screensize, and is not necessarily associated with a particular part of thescreen.  Pads can be used when a large window is needed, and only apart of the window will be on the screen at one time.  Automaticrefreshes of pads (such as from scrolling or echoing of input) do notoccur.  The \method{refresh()} and \method{noutrefresh()} methods of apad require 6 arguments to specify the part of the pad to bedisplayed and the location on the screen to be used for the display.The arguments are pminrow, pmincol, sminrow, smincol, smaxrow,smaxcol; the p arguments refer to the upper left corner of the padregion to be displayed and the s arguments define a clipping box onthe screen within which the pad region is to be displayed.\end{funcdesc} \begin{funcdesc}{newwin}{\optional{nlines, ncols,} begin_y, begin_x}Return a new window, whose left-upper corner is at \code{(\var{begin_y}, \var{begin_x})}, and whose height/width is \var{nlines}/\var{ncols}.   By default, the window will extend from the specified position to the lower right corner of the screen.\end{funcdesc} \begin{funcdesc}{nl}{}Enter newline mode.  This mode translates the return key into newlineon input, and translates newline into return and line-feed on output.Newline mode is initially on.\end{funcdesc} \begin{funcdesc}{nocbreak}{}Leave cbreak mode.  Return to normal ``cooked'' mode with line buffering.\end{funcdesc} \begin{funcdesc}{noecho}{}Leave echo mode.  Echoing of input characters is turned off.\end{funcdesc} \begin{funcdesc}{nonl}{}Leave newline mode.  Disable translation of return into newline oninput, and disable low-level translation of newline intonewline/return on output (but this does not change the behavior of\code{addch('\e n')}, which always does the equivalent of return andline feed on the virtual screen).  With translation off, curses cansometimes speed up vertical motion a little; also, it will be able todetect the return key on input.\end{funcdesc} \begin{funcdesc}{noqiflush}{}When the noqiflush routine is used, normal flush of input andoutput queues associated with the INTR, QUIT and SUSPcharacters will not be done.  You may want to call\function{noqiflush()} in a signal handler if you want outputto continue as though the interrupt had not occurred, after thehandler exits.\end{funcdesc} \begin{funcdesc}{noraw}{}Leave raw mode. Return to normal ``cooked'' mode with line buffering.\end{funcdesc} \begin{funcdesc}{pair_content}{pair_number}Returns a tuple \code{(\var{fg}, \var{bg})} containing the colors forthe requested color pair.  The value of \var{pair_number} must bebetween \code{1} and \code{\constant{COLOR_PAIRS} - 1}.\end{funcdesc} \begin{funcdesc}{pair_number}{attr}Returns the number of the color-pair set by the attribute value\var{attr}.  \function{color_pair()} is the counterpart to thisfunction.\end{funcdesc} \begin{funcdesc}{putp}{string}Equivalent to \code{tputs(str, 1, putchar)}; emits the value of aspecified terminfo capability for the current terminal.  Note that theoutput of putp always goes to standard output.\end{funcdesc} \begin{funcdesc}{qiflush}{ \optional{flag} }If \var{flag} is false, the effect is the same as calling\function{noqiflush()}. If \var{flag} is true, or no argument isprovided, the queues will be flushed when these control characters areread.\end{funcdesc} \begin{funcdesc}{raw}{}Enter raw mode.  In raw mode, normal line buffering and processing of interrupt, quit, suspend, and flow control keys areturned off; characters are presented to curses input functions oneby one.\end{funcdesc} \begin{funcdesc}{reset_prog_mode}{}Restores the  terminal  to ``program'' mode, as previously saved by \function{def_prog_mode()}.\end{funcdesc} \begin{funcdesc}{reset_shell_mode}{}Restores the  terminal  to ``shell'' mode, as previously saved by \function{def_shell_mode()}.\end{funcdesc} \begin{funcdesc}{setsyx}{y, x}Sets the virtual screen cursor to \var{y}, \var{x}.If \var{y} and \var{x} are both -1, then leaveok is set.  \end{funcdesc} \begin{funcdesc}{setupterm}{\optional{termstr, fd}}Initializes the terminal.  \var{termstr} is a string giving theterminal name; if omitted, the value of the TERM environment variablewill be used.  \var{fd} is the file descriptor to which anyinitialization sequences will be sent; if not supplied, the filedescriptor for \code{sys.stdout} will be used.\end{funcdesc} \begin{funcdesc}{start_color}{}Must be called if the programmer wants to use colors, and before anyother color manipulation routine is called.  It is goodpractice to call this routine right after \function{initscr()}. \function{start_color()} initializes eight basic colors (black, red, green, yellow, blue, magenta, cyan, and white), and two globalvariables in the \module{curses} module, \constant{COLORS} and\constant{COLOR_PAIRS}, containing the maximum number of colors andcolor-pairs the terminal can support.  It also restores the colors onthe terminal to the values they had when the terminal was just turnedon.\end{funcdesc} \begin{funcdesc}{termattrs}{}Returns a logical OR of all video attributes supported by theterminal.  This information is useful when a curses program needscomplete control over the appearance of the screen.\end{funcdesc} \begin{funcdesc}{termname}{}Returns the value of the environment variable TERM, truncated to 14characters.\end{funcdesc} \begin{funcdesc}{tigetflag}{capname}Returns the value of the Boolean capability corresponding to theterminfo capability name \var{capname}.  The value \code{-1} isreturned if \var{capname} is not a Boolean capability, or \code{0} ifit is canceled or absent from the terminal description.\end{funcdesc} \begin{funcdesc}{tigetnum}{capname}Returns the value of the numeric capability corresponding to theterminfo capability name \var{capname}.  The value \code{-2} isreturned if \var{capname} is not a numeric capability, or \code{-1} ifit is canceled or absent from the terminal description.  \end{funcdesc} \begin{funcdesc}{tigetstr}{capname}Returns the value of the string capability corresponding to theterminfo capability name \var{capname}.  \code{None} is returned if\var{capname} is not a string capability, or is canceled or absentfrom the terminal description.\end{funcdesc} \begin{funcdesc}{tparm}{str\optional{,...}}Instantiates the string \var{str} with the supplied parameters, where \var{str} should be a parameterized string obtained from the terminfo database.  E.g. \code{tparm(tigetstr("cup"), 5, 3)} could result in \code{'\e{}033[6;4H'}, the exact result depending on terminal type.\end{funcdesc} \begin{funcdesc}{typeahead}{fd}Specifies that the file descriptor \var{fd} be used for typeahead

Lines 1–500 of 1395.