blob: b1476700403ddb8d317924c234d54a1e40c126a5 [file] [log] [blame]
Fred Drakea4070ce1999-06-21 21:13:09 +00001\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
8The \module{curses} module provides an interface to the curses \UNIX{}
9library, the de-facto standard for portable advanced terminal
10handling.
11
12While curses is most widely used in the \UNIX{} environment, versions
13are available for DOS, OS/2, and possibly other systems as well. The
14extension module has not been tested with all available versions of
15curses.
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
27The \module{curses} module defines the following data members:
28
29\begin{datadesc}{version}
30A string representing the current version of the module.
31\end{datadesc}
32
33\begin{datadesc}{A_NORMAL}
34Normal attribute.
35\end{datadesc}
36
37\begin{datadesc}{A_STANDOUT}
38Standout mode.
39\end{datadesc}
40
41\begin{datadesc}{A_UNDERLINE}
42Underline mode.
43\end{datadesc}
44
45\begin{datadesc}{A_BLINK}
46Blink mode.
47\end{datadesc}
48
49\begin{datadesc}{A_DIM}
50Dim mode.
51\end{datadesc}
52
53\begin{datadesc}{A_BOLD}
54Bold mode.
55\end{datadesc}
56
57\begin{datadesc}{A_ALTCHARSET}
58Alternate character set mode.
59\end{datadesc}
60
61\begin{datadesc}{KEY_*}
62Names for various keys. The exact names available are system dependant.
63\end{datadesc}
64
65\begin{datadesc}{ACS_*}
66Names 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
77been called.
78\end{datadesc}
79
80The module \module{curses} defines the following exception:
81\begin{excdesc}{error}
82Curses function returned an error status.
83\end{excdesc}
84
Fred Drake0bccd731999-06-23 17:28:01 +000085\strong{Note:} Whenever \var{x} or \var{y} arguments to a function
86or a method are optional, they default to the current cursor location.
87Whenever \var{attr} is optional, it defaults to \constant{A_NORMAL}.
88
Fred Drakea4070ce1999-06-21 21:13:09 +000089The module \module{curses} defines the following functions:
90
91\begin{funcdesc}{initscr}{}
92Initialize the library. Returns a \class{WindowObject} which represents
93the whole screen.
94\end{funcdesc}
95
96\begin{funcdesc}{endwin}{}
97De-initialize the library, and return terminal to normal status.
98\end{funcdesc}
99
100\begin{funcdesc}{isendwin}{}
101Returns true if \function{endwin()} has been called.
102\end{funcdesc}
103
104\begin{funcdesc}{doupdate}{}
105Update the screen.
106\end{funcdesc}
107
108\begin{funcdesc}{newwin}{\optional{nlines, ncols,} begin_y, begin_x}
109Return a new window, whose left-upper corner is at
110\code{(\var{begin_y}, \var{begin_x})}, and whose height/width is
Fred Drake0bccd731999-06-23 17:28:01 +0000111\var{nlines}/\var{ncols}.
112
113By default, the window will extend from the
Fred Drakea4070ce1999-06-21 21:13:09 +0000114specified position to the lower right corner of the screen.
115\end{funcdesc}
116
117\begin{funcdesc}{beep}{}
118Emit a short sound.
119\end{funcdesc}
120
121\begin{funcdesc}{flash}{}
122Flash the screen.
123\end{funcdesc}
124
125\begin{funcdesc}{ungetch}{ch}
126Push \var{ch} so the next \method{getch()} will return it; \var{ch} is
127an integer specifying the character to be pushed.
128\strong{Note:} only one \var{ch} can be pushed before \method{getch()}
129is called.
130\end{funcdesc}
131
132\begin{funcdesc}{flushinp}{}
133Flush all input buffers.
134\end{funcdesc}
135
136\begin{funcdesc}{cbreak}{}
137Enter cbreak mode.
138\end{funcdesc}
139
140\begin{funcdesc}{nocbreak}{}
141Leave cbreak mode.
142\end{funcdesc}
143
144\begin{funcdesc}{echo}{}
145Enter echo mode.
146\end{funcdesc}
147
148\begin{funcdesc}{noecho}{}
149Leave echo mode.
150\end{funcdesc}
151
152\begin{funcdesc}{nl}{}
153Enter nl mode.
154\end{funcdesc}
155
156\begin{funcdesc}{nonl}{}
157Leave nl mode.
158\end{funcdesc}
159
160\begin{funcdesc}{raw}{}
161Enter raw mode.
162\end{funcdesc}
163
164\begin{funcdesc}{noraw}{}
165Leave raw mode.
166\end{funcdesc}
167
168\begin{funcdesc}{meta}{yes}
169If \var{yes} is 1, allow 8-bit characters. If \var{yes} is 0,
170allow only 7-bit chars.
171\end{funcdesc}
172
173\begin{funcdesc}{keyname}{k}
174Return the name of the key numbered \var{k}.
175\end{funcdesc}
176
177
178\subsection{Window Objects \label{curses-window-objects}}
179
180Window objects, as returned by \function{initscr()} and
181\function{newwin()} above, have the
182following methods:
183
184\begin{methoddesc}{refresh}{}
Fred Drake0bccd731999-06-23 17:28:01 +0000185Update the display immediately (sync actual screen with previous
186drawing/deleting methods).
Fred Drakea4070ce1999-06-21 21:13:09 +0000187\end{methoddesc}
188
189\begin{methoddesc}{nooutrefresh}{}
190Mark for refresh but wait.
191\end{methoddesc}
192
193\begin{methoddesc}{mvwin}{new_y, new_x}
Fred Drake0bccd731999-06-23 17:28:01 +0000194Move the window so its upper-left corner is at \code{(\var{new_y}, \var{new_x})}.
Fred Drakea4070ce1999-06-21 21:13:09 +0000195\end{methoddesc}
196
197\begin{methoddesc}{move}{new_y, new_x}
198Move cursor to \code{(\var{new_y}, \var{new_x})}.
199\end{methoddesc}
200
Fred Drake0bccd731999-06-23 17:28:01 +0000201\begin{methoddesc}{subwin}{\optional{nlines, ncols,} begin_y, begin_y}
Fred Drakea4070ce1999-06-21 21:13:09 +0000202Return 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 Drake0bccd731999-06-23 17:28:01 +0000205
206By default, the sub-window will extend from the
207specified position to the lower right corner of the window.
Fred Drakea4070ce1999-06-21 21:13:09 +0000208\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
215Paint character \var{ch} at \code{(\var{y}, \var{x})} with attributes
216\var{attr}, overwriting any character previously painter at that
217location. By default, the character position and attributes are the
218current settings for the window object.
219\end{methoddesc}
220
221\begin{methoddesc}{insch}{\optional{y, x,} ch\optional{, attr}}
222Paint character \var{ch} at \code{(\var{y}, \var{x})} with attributes
223\var{attr}, moving the line from position \var{x} right by one
224character.
225\end{methoddesc}
226
227\begin{methoddesc}{delch}{\optional{x, y}}
Fred Drake0bccd731999-06-23 17:28:01 +0000228Delete any character at \code{(\var{y}, \var{x})}.
Fred Drakea4070ce1999-06-21 21:13:09 +0000229\end{methoddesc}
230
231\begin{methoddesc}{echochar}{ch\optional{, attr}}
232Add character \var{ch} with attribute \var{attr}, and immediately
233call \method{refresh}.
234\end{methoddesc}
235
236\begin{methoddesc}{addstr}{\optional{y, x,} str\optional{, attr}}
Fred Drake0bccd731999-06-23 17:28:01 +0000237Paint string \var{str} at \code{(\var{y}, \var{x})} with attributes
238\var{attr}, overwriting anything previously on the display.
Fred Drakea4070ce1999-06-21 21:13:09 +0000239\end{methoddesc}
240
241\begin{methoddesc}{attron}{attr}
Fred Drake0bccd731999-06-23 17:28:01 +0000242Turn on attribute \var{attr}.
Fred Drakea4070ce1999-06-21 21:13:09 +0000243\end{methoddesc}
244
245\begin{methoddesc}{attroff}{attr}
Fred Drake0bccd731999-06-23 17:28:01 +0000246Turn off attribute \var{attr}.
Fred Drakea4070ce1999-06-21 21:13:09 +0000247\end{methoddesc}
248
249\begin{methoddesc}{setattr}{attr}
Fred Drake0bccd731999-06-23 17:28:01 +0000250Set the current attributes to \var{attr}.
Fred Drakea4070ce1999-06-21 21:13:09 +0000251\end{methoddesc}
252
253\begin{methoddesc}{standend}{}
Fred Drake0bccd731999-06-23 17:28:01 +0000254Turn off all attributes.
Fred Drakea4070ce1999-06-21 21:13:09 +0000255\end{methoddesc}
256
257\begin{methoddesc}{standout}{}
258Turn on attribute \var{A_STANDOUT}.
259\end{methoddesc}
260
Fred Drake0bccd731999-06-23 17:28:01 +0000261\begin{methoddesc}{border}{\optional{ls\optional{, rs\optional{, ts\optional{,
262 bs\optional{, tl\optional{, tr\optional{,
263 bl\optional{, br}}}}}}}}}
264Draw a border around the edges of the window. Each parameter specifies
265the character to use for a specific part of the border; see the table
266below for more details. The characters must be specified as integers;
267using one-character strings will cause \exception{TypeError} to be
268raised.
269
270\strong{Note:} A \code{0} value for any parameter will cause the
271default character to be used for that parameter. Keyword parameters
272can \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 Drakea4070ce1999-06-21 21:13:09 +0000284\end{methoddesc}
285
Fred Drake0bccd731999-06-23 17:28:01 +0000286\begin{methoddesc}{box}{\optional{vertch, horch}}
287Similar 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
289corner characters are always used by this function.
Fred Drakea4070ce1999-06-21 21:13:09 +0000290\end{methoddesc}
291
292\begin{methoddesc}{hline}{\optional{y, x,} ch, n}
293Display a horizontal line starting at \code{(\var{y}, \var{x})} with
294length \var{n} consisting of the character \var{ch}.
295\end{methoddesc}
296
297\begin{methoddesc}{vline}{\optional{y, x,} ch, n}
298Display a vertical line starting at \code{(\var{y}, \var{x})} with
299length \var{n} consisting of the character \var{ch}.
300\end{methoddesc}
301
302\begin{methoddesc}{erase}{}
303Clear the screen.
304\end{methoddesc}
305
306\begin{methoddesc}{deletln}{}
307Delete the line under the cursor. All following lines are moved up
308by 1 line.
309\end{methoddesc}
310
311\begin{methoddesc}{insertln}{}
312Insert a blank line under the cursor. All following lines are moved
313down by 1 line.
314\end{methoddesc}
315
316\begin{methoddesc}{getyx}{}
317Return a tuple \code{(\var{y}, \var{x})} of current cursor position.
318\end{methoddesc}
319
320\begin{methoddesc}{getbegyx}{}
321Return a tuple \code{(\var{y}, \var{x})} of co-ordinates of upper-left
322corner.
323\end{methoddesc}
324
325\begin{methoddesc}{getmaxyx}{}
326Return a tuple \code{(\var{y}, \var{x})} of the height and width of
327the window.
328\end{methoddesc}
329
330\begin{methoddesc}{clear}{}
331Like \method{erase()}, but also causes the whole screen to be repainted
332upon next call to \method{refresh()}.
333\end{methoddesc}
334
335\begin{methoddesc}{clrtobot}{}
336Erase from cursor to the end of the screen: all lines below the cursor
337are deleted, and then the equivalent of \method{clrtoeol()} is performed.
338\end{methoddesc}
339
340\begin{methoddesc}{clrtoeol}{}
341Erase from cursor to the end of the line.
342\end{methoddesc}
343
344\begin{methoddesc}{scroll}{\optional{lines\code{ = 1}}}
345Scroll the screen upward by \var{lines} lines.
346\end{methoddesc}
347
348\begin{methoddesc}{touchwin}{}
349Pretend the whole window has been changed, for purposes of drawing
350optimizations.
351\end{methoddesc}
352
353\begin{methoddesc}{touchline}{start, count}
354Pretend \var{count} lines have been changed, starting with line
355\var{start}.
356\end{methoddesc}
357
358\begin{methoddesc}{getch}{\optional{x, y}}
359Get a character. Note that the integer returned does \emph{not} have to
360be in \ASCII{} range: function keys, keypad keys and so on return numbers
361higher then 256. In no-delay mode, an exception is raised if there is
362no input.
363\end{methoddesc}
364
365\begin{methoddesc}{getstr}{\optional{x, y}}
366Read a string from the user, with primitive line editing capacity.
367\end{methoddesc}
368
369\begin{methoddesc}{inch}{\optional{x, y}}
370Return the character at the given position in the window. The bottom
3718 bits are the character proper, and upper bits are the attributes.
372\end{methoddesc}
373
374\begin{methoddesc}{clearok}{yes}
375If \var{yes} is 1, the next call to \method{refresh()}
376will clear the screen completely.
377\end{methoddesc}
378
379\begin{methoddesc}{idlok}{yes}
380If called with \var{yes} equal to 1, \module{curses} will try and use
381hardware line editing facilities. Otherwise, line insertion/deletion
382are disabled.
383\end{methoddesc}
384
385\begin{methoddesc}{leaveok}{yes}
386If \var{yes} is 1,
387cursor is left where it is, instead of being at ``cursor position.''
388This reduces cursor movement where possible. If possible it will be made
389invisible.
390
391If \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}
396Set the scrolling region from line \var{top} to line \var{bottom}. All
397scrolling actions will take place in this region.
398\end{methoddesc}
399
400\begin{methoddesc}{keypad}{yes}
401If \var{yes} is 1, escape sequences generated by some keys (keypad,
402function keys) will be interpreted by \module{curses}.
403
404If \var{yes} is 0, escape sequences will be left as is in the input
405stream.
406\end{methoddesc}
407
408\begin{methoddesc}{nodelay}{yes}
409If \var{yes} is 1, \method{getch()} will be non-blocking.
410\end{methoddesc}
411
412\begin{methoddesc}{notimeout}{yes}
413If \var{yes} is 1, escape sequences will not be timed out.
414
415If \var{yes} is 0, after a few milliseconds, an escape sequence will
416not be interpreted, and will be left in the input stream as is.
417\end{methoddesc}