Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 1 | \section{\module{curses} --- |
| 2 | Terminal independant console handling} |
| 3 | |
| 4 | \declaremodule{extension}{curses} |
| 5 | \sectionauthor{Moshe Zadka}{mzadka@geocities.com} |
| 6 | \modulesynopsis{An interface to the curses library.} |
| 7 | |
| 8 | The \module{curses} module provides an interface to the curses \UNIX{} |
| 9 | library, the de-facto standard for portable advanced terminal |
| 10 | handling. |
| 11 | |
| 12 | While curses is most widely used in the \UNIX{} environment, versions |
| 13 | are available for DOS, OS/2, and possibly other systems as well. The |
| 14 | extension module has not been tested with all available versions of |
| 15 | curses. |
| 16 | |
| 17 | \begin{seealso} |
| 18 | \seetext{Tutorial material on using curses with Python is available |
| 19 | on the Python Web site as Andrew Kuchling's \emph{Curses |
| 20 | Programming with Python}, at |
| 21 | \url{http://www.python.org/doc/howto/curses/curses.html}.} |
| 22 | \end{seealso} |
| 23 | |
| 24 | |
| 25 | \subsection{Constants and Functions \label{curses-functions}} |
| 26 | |
| 27 | The \module{curses} module defines the following data members: |
| 28 | |
| 29 | \begin{datadesc}{version} |
| 30 | A string representing the current version of the module. |
| 31 | \end{datadesc} |
| 32 | |
| 33 | \begin{datadesc}{A_NORMAL} |
| 34 | Normal attribute. |
| 35 | \end{datadesc} |
| 36 | |
| 37 | \begin{datadesc}{A_STANDOUT} |
| 38 | Standout mode. |
| 39 | \end{datadesc} |
| 40 | |
| 41 | \begin{datadesc}{A_UNDERLINE} |
| 42 | Underline mode. |
| 43 | \end{datadesc} |
| 44 | |
| 45 | \begin{datadesc}{A_BLINK} |
| 46 | Blink mode. |
| 47 | \end{datadesc} |
| 48 | |
| 49 | \begin{datadesc}{A_DIM} |
| 50 | Dim mode. |
| 51 | \end{datadesc} |
| 52 | |
| 53 | \begin{datadesc}{A_BOLD} |
| 54 | Bold mode. |
| 55 | \end{datadesc} |
| 56 | |
| 57 | \begin{datadesc}{A_ALTCHARSET} |
| 58 | Alternate character set mode. |
| 59 | \end{datadesc} |
| 60 | |
| 61 | \begin{datadesc}{KEY_*} |
| 62 | Names for various keys. The exact names available are system dependant. |
| 63 | \end{datadesc} |
| 64 | |
| 65 | \begin{datadesc}{ACS_*} |
| 66 | Names for various characters: |
| 67 | \constant{ACS_ULCORNER}, \constant{ACS_LLCORNER}, |
| 68 | \constant{ACS_URCORNER}, \constant{ACS_LRCORNER}, \constant{ACS_RTEE}, |
| 69 | \constant{ACS_LTEE}, \constant{ACS_BTEE}, \constant{ACS_TTEE}, |
| 70 | \constant{ACS_HLINE}, \constant{ACS_VLINE}, \constant{ACS_PLUS}, |
| 71 | \constant{ACS_S1}, \constant{ACS_S9}, \constant{ACS_DIAMOND}, |
| 72 | \constant{ACS_CKBOARD}, \constant{ACS_DEGREE}, \constant{ACS_PLMINUS}, |
| 73 | \constant{ACS_BULLET}, \constant{ACS_LARROW}, \constant{ACS_RARROW}, |
| 74 | \constant{ACS_DARROW}. |
| 75 | |
| 76 | \strong{Note:} These are available only after \function{initscr()} has |
| 77 | been called. |
| 78 | \end{datadesc} |
| 79 | |
| 80 | The module \module{curses} defines the following exception: |
| 81 | \begin{excdesc}{error} |
| 82 | Curses function returned an error status. |
| 83 | \end{excdesc} |
| 84 | |
Fred Drake | 0bccd73 | 1999-06-23 17:28:01 +0000 | [diff] [blame] | 85 | \strong{Note:} Whenever \var{x} or \var{y} arguments to a function |
| 86 | or a method are optional, they default to the current cursor location. |
| 87 | Whenever \var{attr} is optional, it defaults to \constant{A_NORMAL}. |
| 88 | |
Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 89 | The module \module{curses} defines the following functions: |
| 90 | |
| 91 | \begin{funcdesc}{initscr}{} |
| 92 | Initialize the library. Returns a \class{WindowObject} which represents |
| 93 | the whole screen. |
| 94 | \end{funcdesc} |
| 95 | |
| 96 | \begin{funcdesc}{endwin}{} |
| 97 | De-initialize the library, and return terminal to normal status. |
| 98 | \end{funcdesc} |
| 99 | |
| 100 | \begin{funcdesc}{isendwin}{} |
| 101 | Returns true if \function{endwin()} has been called. |
| 102 | \end{funcdesc} |
| 103 | |
| 104 | \begin{funcdesc}{doupdate}{} |
| 105 | Update the screen. |
| 106 | \end{funcdesc} |
| 107 | |
| 108 | \begin{funcdesc}{newwin}{\optional{nlines, ncols,} begin_y, begin_x} |
| 109 | Return a new window, whose left-upper corner is at |
| 110 | \code{(\var{begin_y}, \var{begin_x})}, and whose height/width is |
Fred Drake | 0bccd73 | 1999-06-23 17:28:01 +0000 | [diff] [blame] | 111 | \var{nlines}/\var{ncols}. |
| 112 | |
| 113 | By default, the window will extend from the |
Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 114 | specified position to the lower right corner of the screen. |
| 115 | \end{funcdesc} |
| 116 | |
| 117 | \begin{funcdesc}{beep}{} |
| 118 | Emit a short sound. |
| 119 | \end{funcdesc} |
| 120 | |
| 121 | \begin{funcdesc}{flash}{} |
| 122 | Flash the screen. |
| 123 | \end{funcdesc} |
| 124 | |
| 125 | \begin{funcdesc}{ungetch}{ch} |
| 126 | Push \var{ch} so the next \method{getch()} will return it; \var{ch} is |
| 127 | an integer specifying the character to be pushed. |
| 128 | \strong{Note:} only one \var{ch} can be pushed before \method{getch()} |
| 129 | is called. |
| 130 | \end{funcdesc} |
| 131 | |
| 132 | \begin{funcdesc}{flushinp}{} |
| 133 | Flush all input buffers. |
| 134 | \end{funcdesc} |
| 135 | |
| 136 | \begin{funcdesc}{cbreak}{} |
| 137 | Enter cbreak mode. |
| 138 | \end{funcdesc} |
| 139 | |
| 140 | \begin{funcdesc}{nocbreak}{} |
| 141 | Leave cbreak mode. |
| 142 | \end{funcdesc} |
| 143 | |
| 144 | \begin{funcdesc}{echo}{} |
| 145 | Enter echo mode. |
| 146 | \end{funcdesc} |
| 147 | |
| 148 | \begin{funcdesc}{noecho}{} |
| 149 | Leave echo mode. |
| 150 | \end{funcdesc} |
| 151 | |
| 152 | \begin{funcdesc}{nl}{} |
| 153 | Enter nl mode. |
| 154 | \end{funcdesc} |
| 155 | |
| 156 | \begin{funcdesc}{nonl}{} |
| 157 | Leave nl mode. |
| 158 | \end{funcdesc} |
| 159 | |
| 160 | \begin{funcdesc}{raw}{} |
| 161 | Enter raw mode. |
| 162 | \end{funcdesc} |
| 163 | |
| 164 | \begin{funcdesc}{noraw}{} |
| 165 | Leave raw mode. |
| 166 | \end{funcdesc} |
| 167 | |
| 168 | \begin{funcdesc}{meta}{yes} |
| 169 | If \var{yes} is 1, allow 8-bit characters. If \var{yes} is 0, |
| 170 | allow only 7-bit chars. |
| 171 | \end{funcdesc} |
| 172 | |
| 173 | \begin{funcdesc}{keyname}{k} |
| 174 | Return the name of the key numbered \var{k}. |
| 175 | \end{funcdesc} |
| 176 | |
| 177 | |
| 178 | \subsection{Window Objects \label{curses-window-objects}} |
| 179 | |
| 180 | Window objects, as returned by \function{initscr()} and |
| 181 | \function{newwin()} above, have the |
| 182 | following methods: |
| 183 | |
| 184 | \begin{methoddesc}{refresh}{} |
Fred Drake | 0bccd73 | 1999-06-23 17:28:01 +0000 | [diff] [blame] | 185 | Update the display immediately (sync actual screen with previous |
| 186 | drawing/deleting methods). |
Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 187 | \end{methoddesc} |
| 188 | |
| 189 | \begin{methoddesc}{nooutrefresh}{} |
| 190 | Mark for refresh but wait. |
| 191 | \end{methoddesc} |
| 192 | |
| 193 | \begin{methoddesc}{mvwin}{new_y, new_x} |
Fred Drake | 0bccd73 | 1999-06-23 17:28:01 +0000 | [diff] [blame] | 194 | Move the window so its upper-left corner is at \code{(\var{new_y}, \var{new_x})}. |
Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 195 | \end{methoddesc} |
| 196 | |
| 197 | \begin{methoddesc}{move}{new_y, new_x} |
| 198 | Move cursor to \code{(\var{new_y}, \var{new_x})}. |
| 199 | \end{methoddesc} |
| 200 | |
Fred Drake | 0bccd73 | 1999-06-23 17:28:01 +0000 | [diff] [blame] | 201 | \begin{methoddesc}{subwin}{\optional{nlines, ncols,} begin_y, begin_y} |
Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 202 | Return a sub-window, whose upper-left corner is at |
| 203 | \code{(\var{begin_y}, \var{begin_x})}, and whose width/height is |
| 204 | \var{ncols}/\var{nlines}. |
Fred Drake | 0bccd73 | 1999-06-23 17:28:01 +0000 | [diff] [blame] | 205 | |
| 206 | By default, the sub-window will extend from the |
| 207 | specified position to the lower right corner of the window. |
Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 208 | \end{methoddesc} |
| 209 | |
| 210 | \begin{methoddesc}{addch}{\optional{y, x,} ch\optional{, attr}} |
| 211 | \strong{Note:} A \emph{character} means a C character (i.e., an |
| 212 | \ASCII{} code), rather then a Python character (a string of length 1). |
| 213 | (This note is true whenever the documentation mentions a character.) |
| 214 | |
| 215 | Paint character \var{ch} at \code{(\var{y}, \var{x})} with attributes |
| 216 | \var{attr}, overwriting any character previously painter at that |
| 217 | location. By default, the character position and attributes are the |
| 218 | current settings for the window object. |
| 219 | \end{methoddesc} |
| 220 | |
| 221 | \begin{methoddesc}{insch}{\optional{y, x,} ch\optional{, attr}} |
| 222 | Paint character \var{ch} at \code{(\var{y}, \var{x})} with attributes |
| 223 | \var{attr}, moving the line from position \var{x} right by one |
| 224 | character. |
| 225 | \end{methoddesc} |
| 226 | |
| 227 | \begin{methoddesc}{delch}{\optional{x, y}} |
Fred Drake | 0bccd73 | 1999-06-23 17:28:01 +0000 | [diff] [blame] | 228 | Delete any character at \code{(\var{y}, \var{x})}. |
Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 229 | \end{methoddesc} |
| 230 | |
| 231 | \begin{methoddesc}{echochar}{ch\optional{, attr}} |
| 232 | Add character \var{ch} with attribute \var{attr}, and immediately |
| 233 | call \method{refresh}. |
| 234 | \end{methoddesc} |
| 235 | |
| 236 | \begin{methoddesc}{addstr}{\optional{y, x,} str\optional{, attr}} |
Fred Drake | 0bccd73 | 1999-06-23 17:28:01 +0000 | [diff] [blame] | 237 | Paint string \var{str} at \code{(\var{y}, \var{x})} with attributes |
| 238 | \var{attr}, overwriting anything previously on the display. |
Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 239 | \end{methoddesc} |
| 240 | |
| 241 | \begin{methoddesc}{attron}{attr} |
Fred Drake | 0bccd73 | 1999-06-23 17:28:01 +0000 | [diff] [blame] | 242 | Turn on attribute \var{attr}. |
Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 243 | \end{methoddesc} |
| 244 | |
| 245 | \begin{methoddesc}{attroff}{attr} |
Fred Drake | 0bccd73 | 1999-06-23 17:28:01 +0000 | [diff] [blame] | 246 | Turn off attribute \var{attr}. |
Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 247 | \end{methoddesc} |
| 248 | |
| 249 | \begin{methoddesc}{setattr}{attr} |
Fred Drake | 0bccd73 | 1999-06-23 17:28:01 +0000 | [diff] [blame] | 250 | Set the current attributes to \var{attr}. |
Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 251 | \end{methoddesc} |
| 252 | |
| 253 | \begin{methoddesc}{standend}{} |
Fred Drake | 0bccd73 | 1999-06-23 17:28:01 +0000 | [diff] [blame] | 254 | Turn off all attributes. |
Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 255 | \end{methoddesc} |
| 256 | |
| 257 | \begin{methoddesc}{standout}{} |
| 258 | Turn on attribute \var{A_STANDOUT}. |
| 259 | \end{methoddesc} |
| 260 | |
Fred Drake | 0bccd73 | 1999-06-23 17:28:01 +0000 | [diff] [blame] | 261 | \begin{methoddesc}{border}{\optional{ls\optional{, rs\optional{, ts\optional{, |
| 262 | bs\optional{, tl\optional{, tr\optional{, |
| 263 | bl\optional{, br}}}}}}}}} |
| 264 | Draw a border around the edges of the window. Each parameter specifies |
| 265 | the character to use for a specific part of the border; see the table |
| 266 | below for more details. The characters must be specified as integers; |
| 267 | using one-character strings will cause \exception{TypeError} to be |
| 268 | raised. |
| 269 | |
| 270 | \strong{Note:} A \code{0} value for any parameter will cause the |
| 271 | default character to be used for that parameter. Keyword parameters |
| 272 | can \emph{not} be used. The defaults are listed in this table: |
| 273 | |
| 274 | \begin{tableiii}{l|l|l}{var}{Parameter}{Description}{Default value} |
| 275 | \lineiii{ls}{Left side}{\constant{ACS_VLINE}} |
| 276 | \lineiii{rs}{Right side}{\constant{ACS_VLINE}} |
| 277 | \lineiii{ts}{Top}{\constant{ACS_HLINE}} |
| 278 | \lineiii{bs}{Bottom}{\constant{ACS_HLINE}} |
| 279 | \lineiii{tl}{Upper-left corner}{\constant{ACS_ULCORNER}} |
| 280 | \lineiii{tr}{Upper-right corner}{\constant{ACS_URCORNER}} |
| 281 | \lineiii{bl}{Bottom-left corner}{\constant{ACS_BLCORNER}} |
| 282 | \lineiii{br}{Bottom-right corner}{\constant{ACS_BRCORNER}} |
| 283 | \end{tableiii} |
Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 284 | \end{methoddesc} |
| 285 | |
Fred Drake | 0bccd73 | 1999-06-23 17:28:01 +0000 | [diff] [blame] | 286 | \begin{methoddesc}{box}{\optional{vertch, horch}} |
| 287 | Similar to \method{border()}, but both \var{ls} and \var{rs} are |
| 288 | \var{vertch} and both \var{ts} and {bs} are \var{horch}. The default |
| 289 | corner characters are always used by this function. |
Fred Drake | a4070ce | 1999-06-21 21:13:09 +0000 | [diff] [blame] | 290 | \end{methoddesc} |
| 291 | |
| 292 | \begin{methoddesc}{hline}{\optional{y, x,} ch, n} |
| 293 | Display a horizontal line starting at \code{(\var{y}, \var{x})} with |
| 294 | length \var{n} consisting of the character \var{ch}. |
| 295 | \end{methoddesc} |
| 296 | |
| 297 | \begin{methoddesc}{vline}{\optional{y, x,} ch, n} |
| 298 | Display a vertical line starting at \code{(\var{y}, \var{x})} with |
| 299 | length \var{n} consisting of the character \var{ch}. |
| 300 | \end{methoddesc} |
| 301 | |
| 302 | \begin{methoddesc}{erase}{} |
| 303 | Clear the screen. |
| 304 | \end{methoddesc} |
| 305 | |
| 306 | \begin{methoddesc}{deletln}{} |
| 307 | Delete the line under the cursor. All following lines are moved up |
| 308 | by 1 line. |
| 309 | \end{methoddesc} |
| 310 | |
| 311 | \begin{methoddesc}{insertln}{} |
| 312 | Insert a blank line under the cursor. All following lines are moved |
| 313 | down by 1 line. |
| 314 | \end{methoddesc} |
| 315 | |
| 316 | \begin{methoddesc}{getyx}{} |
| 317 | Return a tuple \code{(\var{y}, \var{x})} of current cursor position. |
| 318 | \end{methoddesc} |
| 319 | |
| 320 | \begin{methoddesc}{getbegyx}{} |
| 321 | Return a tuple \code{(\var{y}, \var{x})} of co-ordinates of upper-left |
| 322 | corner. |
| 323 | \end{methoddesc} |
| 324 | |
| 325 | \begin{methoddesc}{getmaxyx}{} |
| 326 | Return a tuple \code{(\var{y}, \var{x})} of the height and width of |
| 327 | the window. |
| 328 | \end{methoddesc} |
| 329 | |
| 330 | \begin{methoddesc}{clear}{} |
| 331 | Like \method{erase()}, but also causes the whole screen to be repainted |
| 332 | upon next call to \method{refresh()}. |
| 333 | \end{methoddesc} |
| 334 | |
| 335 | \begin{methoddesc}{clrtobot}{} |
| 336 | Erase from cursor to the end of the screen: all lines below the cursor |
| 337 | are deleted, and then the equivalent of \method{clrtoeol()} is performed. |
| 338 | \end{methoddesc} |
| 339 | |
| 340 | \begin{methoddesc}{clrtoeol}{} |
| 341 | Erase from cursor to the end of the line. |
| 342 | \end{methoddesc} |
| 343 | |
| 344 | \begin{methoddesc}{scroll}{\optional{lines\code{ = 1}}} |
| 345 | Scroll the screen upward by \var{lines} lines. |
| 346 | \end{methoddesc} |
| 347 | |
| 348 | \begin{methoddesc}{touchwin}{} |
| 349 | Pretend the whole window has been changed, for purposes of drawing |
| 350 | optimizations. |
| 351 | \end{methoddesc} |
| 352 | |
| 353 | \begin{methoddesc}{touchline}{start, count} |
| 354 | Pretend \var{count} lines have been changed, starting with line |
| 355 | \var{start}. |
| 356 | \end{methoddesc} |
| 357 | |
| 358 | \begin{methoddesc}{getch}{\optional{x, y}} |
| 359 | Get a character. Note that the integer returned does \emph{not} have to |
| 360 | be in \ASCII{} range: function keys, keypad keys and so on return numbers |
| 361 | higher then 256. In no-delay mode, an exception is raised if there is |
| 362 | no input. |
| 363 | \end{methoddesc} |
| 364 | |
| 365 | \begin{methoddesc}{getstr}{\optional{x, y}} |
| 366 | Read a string from the user, with primitive line editing capacity. |
| 367 | \end{methoddesc} |
| 368 | |
| 369 | \begin{methoddesc}{inch}{\optional{x, y}} |
| 370 | Return the character at the given position in the window. The bottom |
| 371 | 8 bits are the character proper, and upper bits are the attributes. |
| 372 | \end{methoddesc} |
| 373 | |
| 374 | \begin{methoddesc}{clearok}{yes} |
| 375 | If \var{yes} is 1, the next call to \method{refresh()} |
| 376 | will clear the screen completely. |
| 377 | \end{methoddesc} |
| 378 | |
| 379 | \begin{methoddesc}{idlok}{yes} |
| 380 | If called with \var{yes} equal to 1, \module{curses} will try and use |
| 381 | hardware line editing facilities. Otherwise, line insertion/deletion |
| 382 | are disabled. |
| 383 | \end{methoddesc} |
| 384 | |
| 385 | \begin{methoddesc}{leaveok}{yes} |
| 386 | If \var{yes} is 1, |
| 387 | cursor is left where it is, instead of being at ``cursor position.'' |
| 388 | This reduces cursor movement where possible. If possible it will be made |
| 389 | invisible. |
| 390 | |
| 391 | If \var{yes} is 0, cursor will always be at |
| 392 | ``cursor position'' after an update. |
| 393 | \end{methoddesc} |
| 394 | |
| 395 | \begin{methoddesc}{setscrreg}{top, bottom} |
| 396 | Set the scrolling region from line \var{top} to line \var{bottom}. All |
| 397 | scrolling actions will take place in this region. |
| 398 | \end{methoddesc} |
| 399 | |
| 400 | \begin{methoddesc}{keypad}{yes} |
| 401 | If \var{yes} is 1, escape sequences generated by some keys (keypad, |
| 402 | function keys) will be interpreted by \module{curses}. |
| 403 | |
| 404 | If \var{yes} is 0, escape sequences will be left as is in the input |
| 405 | stream. |
| 406 | \end{methoddesc} |
| 407 | |
| 408 | \begin{methoddesc}{nodelay}{yes} |
| 409 | If \var{yes} is 1, \method{getch()} will be non-blocking. |
| 410 | \end{methoddesc} |
| 411 | |
| 412 | \begin{methoddesc}{notimeout}{yes} |
| 413 | If \var{yes} is 1, escape sequences will not be timed out. |
| 414 | |
| 415 | If \var{yes} is 0, after a few milliseconds, an escape sequence will |
| 416 | not be interpreted, and will be left in the input stream as is. |
| 417 | \end{methoddesc} |