From: A.M. K. <aku...@us...> - 2000-08-11 14:55:43
|
Update of /cvsroot/py-howto/pyhowto In directory slayer.i.sourceforge.net:/tmp/cvs-serv8960 Modified Files: curses.tex Log Message: ESR's extensively reworked version Index: curses.tex =================================================================== RCS file: /cvsroot/py-howto/pyhowto/curses.tex,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -r1.5 -r1.6 *** curses.tex 2000/08/09 18:02:45 1.5 --- curses.tex 2000/08/11 14:55:39 1.6 *************** *** 3,11 **** \title{Curses Programming with Python} ! \release{1.0} - \author{A.M. Kuchling} \authoraddress{\email{am...@bi...}} \begin{document} --- 3,12 ---- \title{Curses Programming with Python} ! \release{2.0} \author{A.M. Kuchling} \authoraddress{\email{am...@bi...}} + \author{Eric S. Raymond} + \authoraddress{\email{es...@th...}} \begin{document} *************** *** 16,24 **** This document describes how to write text-mode programs with Python, using the curses extension module to control the display. - - This document is available in several formats, including PostScript, - PDF, HTML and plain ASCII, from the Python HOWTO page at - \url{http://www.python.org/doc/howto/}. - \end{abstract} --- 17,20 ---- *************** *** 27,31 **** \section{What is curses?} ! curses is a display library for text-based terminals; such terminals include VT100s, the Linux console, and the simulated terminal provided by X11 programs such as xterm and rxvt. Display terminals support --- 23,28 ---- \section{What is curses?} ! The curses library supplies a terminal-independent screen-painting and ! keyboard-handling facility for text-based terminals; such terminals include VT100s, the Linux console, and the simulated terminal provided by X11 programs such as xterm and rxvt. Display terminals support *************** *** 34,68 **** use widely differing codes, and often have their own minor quirks. ! curses hides all the details of different terminals, and provides the ! programmer with an abstraction of a display, containing multiple ! non-overlapping windows. The contents of a window can be changed in ! various ways--adding text, erasing it, changing its appearance--and ! the curses library will automagically figure out what control codes ! need to be sent to the terminal to produce the right output. ! curses was originally written for BSD Unix; the later System V versions of Unix from AT\&T added many enhancements and new functions. BSD curses is no longer maintained, having been replaced by ncurses, ! which is a GPLed implementation of the AT\&T interface. If you're ! using a freeware Unix such as Linux or FreeBSD, your system almost certainly uses ncurses. Since most current commercial Unix versions are based on System V code, all the functions described here will ! probably be available. Older systems may not support everything, ! though. \subsection{The Python curses module} ! The original Python curses module was written by Lance Ellinghouse, ! and is included with the Python source distribution, version 1.5 or ! earlier. It supports all the basic operations of displaying text, but ! doesn't support System V features such as colour. Later, Oliver ! Andrich enhanced this module by adding support for many of the missing ! features. ! ! Both modules are a fairly simple wrapper over the C functions provided ! by curses; if you're already familiar with curses programming in C, ! it's really easy to transfer that knowledge to Python. The biggest ! difference is that the Python interface makes things simpler, by ! merging different C functions such as \function{addstr}, \function{mvaddstr}, \function{mvwaddstr}, into a single \method{addstr()} method. You'll see this covered in more detail --- 31,59 ---- use widely differing codes, and often have their own minor quirks. ! The curses library hides all the details of different terminals, and ! provides the programmer with an abstraction of a display, containing ! multiple non-overlapping windows. The contents of a window can be ! changed in various ways--adding text, erasing it, changing its ! appearance--and the curses library will automagically figure out what ! control codes need to be sent to the terminal to produce the right ! output. ! The curses library was originally written for BSD Unix; the later System V versions of Unix from AT\&T added many enhancements and new functions. BSD curses is no longer maintained, having been replaced by ncurses, ! which is an open-source implementation of the AT\&T interface. If you're ! using an open-source Unix such as Linux or FreeBSD, your system almost certainly uses ncurses. Since most current commercial Unix versions are based on System V code, all the functions described here will ! probably be available. The older versions of curses carried by some ! proprietary Unixes may not support everything, though. \subsection{The Python curses module} ! Thy Python module is a fairly simple wrapper over the C functions ! provided by curses; if you're already familiar with curses programming ! in C, it's really easy to transfer that knowledge to Python. The ! biggest difference is that the Python interface makes things simpler, ! by merging different C functions such as \function{addstr}, \function{mvaddstr}, \function{mvwaddstr}, into a single \method{addstr()} method. You'll see this covered in more detail *************** *** 70,98 **** This HOWTO is simply an introduction to writing text-mode programs ! with curses and Python, and doesn't even attempt to be a complete ! guide to the curses API. It will, however, give you the basic ideas. ! This documentation doesn't even cover every single curses function ! available in the Python modules; that would make this a very long ! HOWTO, and would duplicate much of the information in the curses ! documentation. You should consult the documentation for your curses ! implementation to learn about its quirks. ! ! To install the regular curses extension, you only have to uncomment ! the corresponding line of Modules/Setup in your Python source tree, ! and recompile. You may have to tweak the required libraries, as ! described in the comment: ! ! \begin{verbatim} ! # Lance's curses module. This requires the System V version of ! # curses, sometimes known as ncurses (e.g. on Linux, link with ! # -lncurses instead of -lcurses; on SunOS 4.1.3, insert ! # -I/usr/5include -L/usr/5lib before -lcurses). ! ! curses cursesmodule.c -lcurses -ltermcap ! \end{verbatim} ! ! To install the newer module, fetch the most recent version from ! \url{http://andrich.net/python/selfmade.html\#ncursesmodule}, unpack ! it, and simply run \code{make}. \section{Starting and ending a curses Application} --- 61,68 ---- This HOWTO is simply an introduction to writing text-mode programs ! with curses and Python. It doesn't attempt to be a complete guide to ! the curses API; for that, see the Python library guide's serction on ! ncurses, and the C manual pages for ncurses. It will, however, give ! you the basic ideas. \section{Starting and ending a curses Application} *************** *** 140,148 **** \end{verbatim} ! Terminating a curses application is much easier than starting one. ! Simply call the \function{endwin()} function to restore the terminal ! to its original operating mode. \begin{verbatim} curses.endwin() \end{verbatim} --- 110,125 ---- \end{verbatim} ! Terminating a curses application is much easier than starting one. ! You'll need to call \begin{verbatim} + curses.nobreak(); curses.keypad(0); curses.echo() + \end{verbatim} + + to reverse the curses-friendly terminal settings. Then call the + \function{endwin()} function to restore the terminal to its original + operating mode. + + \begin{verbatim} curses.endwin() \end{verbatim} *************** *** 155,195 **** makes using the shell difficult. ! In Python you can avoid this and make debugging much easier by copying ! the following top-level code. It initializes the terminal and then ! calls the \function{main()} function, which will be your application's ! code. If \function{main()} raises an uncaught exception, the terminal ! is restored to its normal state before printing the traceback. ! ! \begin{verbatim} ! def main(stdscr): ! # Your code goes here ! ... ! ! if __name__=='__main__': ! import curses, traceback ! try: ! # Initialize curses ! stdscr=curses.initscr() ! # Turn off echoing of keys, and enter cbreak mode, ! # where no buffering is performed on keyboard input ! curses.noecho() ; curses.cbreak() ! ! # In keypad mode, escape sequences for special keys ! # (like the cursor keys) will be interpreted and ! # a special value like curses.KEY_LEFT will be returned ! stdscr.keypad(1) ! main(stdscr) # Enter the main loop ! # Set everything back to normal ! stdscr.keypad(0) ! curses.echo() ; curses.nocbreak() ! curses.endwin() # Terminate curses ! except: ! # In the event of an error, restore the terminal ! # to a sane state. ! stdscr.keypad(0) ! curses.echo() ; curses.nocbreak() ! curses.endwin() ! traceback.print_exc() # Print the exception ! \end{verbatim} \section{Windows and Pads} --- 132,144 ---- makes using the shell difficult. ! In Python you can avoid these complications and make debugging much ! easier by importing the module \module{curses.wrapper}. It supplies a ! function \function{wrapper} that takes a hook argument. It does the ! initializations described above, and also initializes colors if color ! support is present. It then runs your hook, and then finally ! deinitializes appropriately. The hook is called inside a try-catch ! hook which catches exceptions, performs curses deinitialization, and ! then passes the exception upwards. Thus, your terminal won't be left ! in a funny state on exception. \section{Windows and Pads} *************** *** 265,268 **** --- 214,226 ---- ordinary windows and support the same methods. + If you have multiple windows and pads on screen there is a more + efficient way to go, which will prevent annoying screen flicker at + refresh time. Use the methods \method{noutrefresh()} and/or + \method{noutrefresh()} of each window to update the data structure + representing the desired state of the screen; then change the physical + screen to match the desired state in one go with the function + \function{doupdate()}. The normal \method{refresh()} method calls + \function{doupdate()} as its last act. + \section{Displaying Text} *************** *** 314,324 **** ensure that the cursor is positioned in some location where it won't be distracting; it can be confusing to have the cursor blinking at ! some apparently random location. If your application doesn't need a ! blinking cursor at all, you can call the ! \function{leaveok(\var{bool})} method. When \var{bool} is true, the curses library will attempt to suppress the flashing cursor, and you won't need to worry about leaving it in odd locations. - \subsection{Attributes and Colour} --- 272,284 ---- ensure that the cursor is positioned in some location where it won't be distracting; it can be confusing to have the cursor blinking at ! some apparently random location. ! ! If your application doesn't need a blinking cursor at all, you can ! call \function{curs_set(0)} to make it invisible. Equivalently, and ! for compatibility with older curses versions, there's a ! \function{leaveok(\var{bool})} function. When \var{bool} is true, the curses library will attempt to suppress the flashing cursor, and you won't need to worry about leaving it in odd locations. \subsection{Attributes and Colour} *************** *** 354,377 **** \end{verbatim} ! SVr4 curses also supports colour on those terminals that provide it, ! and Oliver Andrich's extended Python curses allows you to use the ! relevant functions. The most common such terminal is probably the ! Linux console, followed by colour xterms. To use colour, you must call the \function{start_color()} function soon after calling \function{initscr()}, to initialize the default ! colour set. Once that's done, the \function{has_colors()} function ! returns TRUE if the terminal in use can actually display colour. ! (Note that curses uses the American spelling 'color', instead of the ! Canadian/British spelling 'colour'; if you're like me, you'll have to ! resign yourself to misspelling it for the sake of these functions.) ! ! curses maintains a finite number of colour pairs, containing a ! foreground (or text) colour and a background colour. You can get the ! attribute value corresponding to a colour pair with the \function{COLOR_PAIR()} function; this can be bitwise-OR'ed with other attributes such as \constant{A_REVERSE}, but again, such combinations ! are not ! guaranteed to work on all terminals. An example, which displays a line of text using colour pair 1: --- 314,337 ---- \end{verbatim} ! The curses library also supports colour on those terminals that ! provide it, The most common such terminal is probably the Linux ! console, followed by colour xterms. To use colour, you must call the \function{start_color()} function soon after calling \function{initscr()}, to initialize the default ! colour set (the \function{curses.wrapper\wrapper()} function does this ! automatically). Once that's done, the \function{has_colors()} ! function returns TRUE if the terminal in use can actually display ! colour. (Note that curses uses the American spelling 'color', instead ! of the Canadian/British spelling 'colour'; if you're like me, you'll ! have to resign yourself to misspelling it for the sake of these ! functions.) ! ! The curses library maintains a finite number of colour pairs, ! containing a foreground (or text) colour and a background colour. You ! can get the attribute value corresponding to a colour pair with the \function{COLOR_PAIR()} function; this can be bitwise-OR'ed with other attributes such as \constant{A_REVERSE}, but again, such combinations ! are not guaranteed to work on all terminals. An example, which displays a line of text using colour pair 1: *************** *** 422,436 **** \section{User Input} ! curses offers only very simple input mechanisms. Windows have a ! \function{getch()} method that pauses, and waits for the user to hit a ! key, displaying it if \function{echo()} has been called earlier. You ! can optionally specify a coordinate to which the cursor should be ! moved before pausing. \function{getch()} returns an integer; if it's ! between 0 and 255, it represents the ASCII code of the key pressed. ! Values greater than 255 are special keys such as Page Up, Home, or the ! cursor keys. You can compare the value returned to constants such as \constant{curses.KEY_PPAGE}, \constant{curses.KEY_HOME}, or \constant{curses.KEY_LEFT}. Usually the main loop of your program ! might look like this: \begin{verbatim} --- 382,410 ---- \section{User Input} ! The curses library itself offers only very simple input mechanisms. ! Python's support adds a text-input widget that makes up some of the ! lack. ! ! The most common way to get input to a window is to use its ! \method{getch()} method. that pauses, and waits for the user to hit ! a key, displaying it if \function{echo()} has been called earlier. ! You can optionally specify a coordinate to which the cursor should be ! moved before pausing. ! ! It's possible to change this behavior with the method ! \method{nodelay()}. After \method{nodelay(1)}, \method{getch()} for ! the window becomes non-blocking and returns ERR (-1) when no input is ! ready. There's also a \function{halfdelay()} function, which can be ! used to (in effect) set a timer on each \method{getch()}; if no input ! becomes available within the number of milliseconds specified as the ! argument to \function{halfdelay()}, curses throws an exception. ! ! The \method{getch()} method returns an integer; if it's between 0 and ! 255, it represents the ASCII code of the key pressed. Values greater ! than 255 are special keys such as Page Up, Home, or the cursor keys. ! You can compare the value returned to constants such as \constant{curses.KEY_PPAGE}, \constant{curses.KEY_HOME}, or \constant{curses.KEY_LEFT}. Usually the main loop of your program ! will look something like this: \begin{verbatim} *************** *** 442,445 **** --- 416,427 ---- \end{verbatim} + The \module{curses.ascii} module supplies ASCII class membership + functions that take either integer or 1-character-string + arguments; these may be useful in writing more readable tests for + your command interprers. It also supplies conversion functions + that take either integer or 1-character-string arguments and return + the same type. For example, \function{curses.ascii.ctrl()} returns + the control character corresponding to its argument. + There's also a method to retrieve an entire string, \constant{getstr()}. It isn't used very often, because its *************** *** 454,493 **** s = stdscr.getstr(0,0, 15) \end{verbatim} - In future versions of this HOWTO, I'd like to include an - implementation of a fancy string entry function that allows full - editing. If you write such a function, send me a copy and it'll be - added to the next version of this document. - \section{For More Information} ! Again, I urge you to consult the manual pages for your curses ! implementation, whether it's ncurses or a commercial vendor's. The ! manual pages will document any quirks, and provide complete lists of ! all the functions, attributes, and \constant{ACS_*} characters ! available to you. Because the curses API is so large, some functions aren't supported in the Python interface, not because they're difficult to implement, but because no one has needed them yet. Feel free to add them and then ! submit a patch. ! Oliver Andrich's curses module comes with a demonstration program ! which displays a Life board, written by the author of this HOWTO; you ! may want to look at its code for more examples. If you write an ! interesting little program, feel free to contribute it as another ! demo. We can always use more of them! The ncurses FAQ: \url{http://www.clark.net/pub/dickey/ncurses/ncurses.faq.html} - Oliver Andrich's cursesmodule: \url{http://andrich.net/python/selfmade.html\#ncursesmodule} - Any other suggested references? - \section{Python curses Module Reference} - - I haven't yet written complete reference documentation for the curses - module; hopefully it will be added to a future version of this HOWTO. - \end{document} - --- 436,473 ---- s = stdscr.getstr(0,0, 15) \end{verbatim} + + The Python \module{curses.textpad} module supplies something better. + With it, you can turn a window into a text box that supports an + Emacs-like set of keybindings. Various methods of \class{Textbox} + class support editing with input validation and gathering the edit + results either with or without trailing spaces. See the library + documentation on \module{curses.textpad} for the details. \section{For More Information} ! This HOWTO didn't cover some advanced topics, such as screen-scraping ! or capturing mouse events from an xterm inatance. But the Python ! library page for the curses modules is now pretty complete. You ! should browse it next. ! ! If you're in doubt about the detailed behavior of any of the ncurses ! entry points, consult the manual pages for your curses implementation, ! whether it's ncurses or a proprietary Unix vendor's. The manual pages ! will document any quirks, and provide complete lists of all the ! functions, attributes, and \constant{ACS_*} characters available to ! you. Because the curses API is so large, some functions aren't supported in the Python interface, not because they're difficult to implement, but because no one has needed them yet. Feel free to add them and then ! submit a patch. Also, we don't yet have support for the menus or ! panels libraries associated with ncurses; feel free to add that. ! If you write an interesting little program, feel free to contribute it ! as another demo. We can always use more of them! The ncurses FAQ: \url{http://www.clark.net/pub/dickey/ncurses/ncurses.faq.html} Any other suggested references? \end{document} |