📄 ncurses-programming-howto.html
字号:
<dt>20.4. <a href="#QUEENS">Queens Puzzle</a></dt><dt>20.5. <a href="#SHUFFLE">Shuffle</a></dt><dt>20.6. <a href="#TT">Typing Tutor</a></dt></dl></dd><dt>21. <a href="#REF">References</a></dt></dl></div><div class="SECT1"><h2 class="SECT1"><a name="INTRO" id="INTRO">1.Introduction</a></h2><p>In the olden days of teletype terminals, terminals were awayfrom computers and were connected to them through serial cables.The terminals could be configured by sending a series of bytes. Allthe capabilities (such as moving the cursor to a new location,erasing part of the screen, scrolling the screen, changing modesetc.) of terminals could be accessed through these series of bytes.These control seeuqnces are usually called escape sequences,because they start with an escape(0x1B) character. Even today, withproper emulation, we can send escape sequences to the emulator andachieve the same effect on a terminal window.</p><p>Suppose you wanted to print a line in color. Try typing this onyour console.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000">echo "^[[0;31;40mIn Color"</font></pre></td></tr></table><p>The first character is an escape character, which looks like twocharacters ^ and [. To be able to print it, you have to pressCTRL+V and then the ESC key. All the others are normal printablecharacters. You should be able to see the string "In Color" in red.It stays that way and to revert back to the original mode typethis.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000">echo "^[[0;37;40m"</font></pre></td></tr></table><p>Now, what do these magic characters mean? Difficult tocomprehend? They might even be different for different terminals.So the designers of UNIX invented a mechanism named <var class="LITERAL">termcap</var>. It is a file that lists all thecapabilities of a particular terminal, along with the escapesequences needed to achieve a particular effect. In the lateryears, this was replaced by <var class="LITERAL">terminfo</var>.Without delving too much into details, this mechanism allowsapplication programs to query the terminfo database and obtain thecontrol characters to be sent to a terminal or terminalemulator.</p><div class="SECT2"><hr><h3 class="SECT2"><a name="WHATIS" id="WHATIS">1.1. What isNCURSES?</a></h3><p>You might be wondering, what the import of all this technicalgibberish is. In the above scenario, every application program issupposed to query the terminfo and perform the necessary stuff(sending control characters etc.). It soon became difficult tomanage this complexity and this gave birth to 'CURSES'. Curses is apun on the name "cursor optimization". The Curses library forms awrapper over working with raw terminal codes, and provides highlyflexible and efficient API (Application Programming Interface). Itprovides functions to move the cursor, create windows, producecolors, play with mouse etc. The application programs need notworry about the underlying terminal capabilities.</p><p>So what is NCURSES? NCURSES is a clone of the original System VRelease 4.0 (SVr4) curses. It is a freely distributable library,fully compatible with older version of curses. In short, it is alibrary of functions that manages an application's display oncharacter-cell terminals. In the remainder of the document, theterms curses and ncurses are used interchangeably.</p><p>A detailed history of NCURSES can be found in the NEWS file fromthe source distribution. The current package is maintained by<a href="mailto:dickey@his.com" target="_top">Thomas Dickey</a>.You can contact the maintainers at <a href="mailto:bug-ncurses@gnu.org" target="_top">bug-ncurses@gnu.org</a>.</p></div><div class="SECT2"><hr><h3 class="SECT2"><a name="WHATCANWEDO" id="WHATCANWEDO">1.2. Whatwe can do with NCURSES</a></h3><p>NCURSES not only creates a wrapper over terminal capabilities,but also gives a robust framework to create nice looking UI (UserInterface)s in text mode. It provides functions to create windowsetc. Its sister libraries panel, menu and form provide an extensionto the basic curses library. These libraries usually come alongwith curses. One can create applications that contain multiplewindows, menus, panels and forms. Windows can be managedindependently, can provide 'scrollability' and even can behidden.</p><p>Menus provide the user with an easy command selection option.Forms allow the creation of easy-to-use data entry and displaywindows. Panels extend the capabilities of ncurses to deal withoverlapping and stacked windows.</p><p>These are just some of the basic things we can do with ncurses.As we move along, We will see all the capabilities of theselibraries.</p></div><div class="SECT2"><hr><h3 class="SECT2"><a name="WHERETOGETIT" id="WHERETOGETIT">1.3.Where to get it</a></h3><p>All right, now that you know what you can do with ncurses, youmust be rearing to get started. NCURSES is usually shipped withyour installation. In case you don't have the library or want tocompile it on your own, read on.</p><p><em>Compiling the package</em></p><p>NCURSES can be obtained from <a href="ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz" target="_top">ftp://ftp.gnu.org/pub/gnu/ncurses/ncurses.tar.gz</a> or anyof the ftp sites mentioned in <a href="http://www.gnu.org/order/ftp.html" target="_top">http://www.gnu.org/order/ftp.html</a>.</p><p>Read the README and INSTALL files for details on to how toinstall it. It usually involves the following operations.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000"> tar zxvf ncurses<version>.tar.gz # unzip and untar the archive cd ncurses<version> # cd to the directory ./configure # configure the build according to your # environment make # make it su root # become root make install # install it</font></pre></td></tr></table><p><em>Using the RPM</em></p><p>NCURSES RPM can be found and downloaded from <a href="http://rpmfind.net" target="_top">http://rpmfind.net</a> . The RPMcan be installed with the following command after becomingroot.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000"> rpm -i <downloaded rpm></font></pre></td></tr></table></div><div class="SECT2"><hr><h3 class="SECT2"><a name="PURPOSE" id="PURPOSE">1.4. Purpose/Scopeof the document</a></h3><p>This document is intended to be a "All in One" guide forprogramming with ncurses and its sister libraries. We graduate froma simple "Hello World" program to more complex form manipulation.No prior experience in ncurses is assumed. The writing is informal,but a lot of detail is provided for each of the examples.</p></div><div class="SECT2"><hr><h3 class="SECT2"><a name="ABOUTPROGRAMS" id="ABOUTPROGRAMS">1.5.About the Programs</a></h3><p>All the programs in the document are available in zipped form<a href="http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs.tar.gz"target="_top">here</a>. Unzip and untar it. The directory structurelooks like this.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000">ncurses | |----> JustForFun -- just for fun programs |----> basics -- basic programs |----> demo -- output files go into this directory after make | | | |----> exe -- exe files of all example programs |----> forms -- programs related to form library |----> menus -- programs related to menus library |----> panels -- programs related to panels library |----> perl -- perl equivalents of the examples (contributed | by Anuradha Ratnaweera) |----> Makefile -- the top level Makefile |----> README -- the top level README file. contains instructions |----> COPYING -- copyright notice</font></pre></td></tr></table><p>The individual directories contain the following files.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000">Description of files in each directory--------------------------------------JustForFun | |----> hanoi.c -- The Towers of Hanoi Solver |----> life.c -- The Game of Life demo |----> magic.c -- An Odd Order Magic Square builder |----> queens.c -- The famous N-Queens Solver |----> shuffle.c -- A fun game, if you have time to kill |----> tt.c -- A very trivial typing tutor basics | |----> acs_vars.c -- ACS_ variables example |----> hello_world.c -- Simple "Hello World" Program |----> init_func_example.c -- Initialization functions example |----> key_code.c -- Shows the scan code of the key pressed |----> mouse_menu.c -- A menu accessible by mouse |----> other_border.c -- Shows usage of other border functions apa | -- rt from box() |----> printw_example.c -- A very simple printw() example |----> scanw_example.c -- A very simple getstr() example |----> simple_attr.c -- A program that can print a c file with | -- comments in attribute |----> simple_color.c -- A simple example demonstrating colors |----> simple_key.c -- A menu accessible with keyboard UP, DOWN | -- arrows |----> temp_leave.c -- Demonstrates temporarily leaving curses mode |----> win_border.c -- Shows Creation of windows and borders |----> with_chgat.c -- chgat() usage example forms | |----> form_attrib.c -- Usage of field attributes |----> form_options.c -- Usage of field options |----> form_simple.c -- A simple form example |----> form_win.c -- Demo of windows associated with forms menus | |----> menu_attrib.c -- Usage of menu attributes |----> menu_item_data.c -- Usage of item_name() etc.. functions |----> menu_multi_column.c -- Creates multi columnar menus |----> menu_scroll.c -- Demonstrates scrolling capability of menus |----> menu_simple.c -- A simple menu accessed by arrow keys |----> menu_toggle.c -- Creates multi valued menus and explains | -- REQ_TOGGLE_ITEM |----> menu_userptr.c -- Usage of user pointer |----> menu_win.c -- Demo of windows associated with menus panels | |----> panel_browse.c -- Panel browsing through tab. Usage of user | -- pointer |----> panel_hide.c -- Hiding and Un hiding of panels |----> panel_resize.c -- Moving and resizing of panels |----> panel_simple.c -- A simple panel example perl |----> 01-10.pl -- Perl equivalents of first ten example programs</font></pre></td></tr></table><p>There is a top level Makefile included in the main directory. Itbuilds all the files and puts the ready-to-use exes in demo/exedirectory. You can also do selective make by going into thecorresponding directory. Each directory contains a README fileexplaining the purpose of each c file in the directory.</p><p>For every example, I have included path name for the filerelative to the examples directory.</p><p>If you prefer browsing individual programs, point your browserto <a href="http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/"target="_top">http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/ncurses_programs/</a></p><p>All the programs are released under the same license that isused by ncurses (MIT-style). This gives you the ability to dopretty much anything other than claiming them as yours. Feel freeto use them in your programs as appropriate.</p></div><div class="SECT2"><hr><h3 class="SECT2"><a name="OTHERFORMATS" id="OTHERFORMATS">1.6.Other Formats of the document</a></h3><p>This howto is also availabe in various other formats on thetldp.org site. Here are the links to other formats of thisdocument.</p><div class="SECT3"><hr><h4 class="SECT3"><a name="LISTFORMATS" id="LISTFORMATS">1.6.1.Readily available formats from tldp.org</a></h4><ul><li><p><a href="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/pdf/NCURSES-Programming-HOWTO.pdf"target="_top">Acrobat PDF Format</a></p></li><li><p><a href="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/ps/NCURSES-Programming-HOWTO.ps.gz"target="_top">PostScript Format</a></p></li><li><p><a href="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html/NCURSES-Programming-HOWTO-html.tar.gz"target="_top">In Multiple HTML pages</a></p></li><li><p><a href="http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html_single/NCURSES-Programming-HOWTO.html"target="_top">In One big HTML format</a></p></li></ul></div><div class="SECT3"><hr><h4 class="SECT3"><a name="BUILDSOURCE" id="BUILDSOURCE">1.6.2.Building from source</a></h4><p>If above links are broken or if you want to experiment with sgmlread on.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000"> Get both the source and the tar,gzipped programs, available at http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/ NCURSES-HOWTO/NCURSES-Programming-HOWTO.sgml http://cvsview.tldp.org/index.cgi/LDP/howto/docbook/ NCURSES-HOWTO/ncurses_programs.tar.gz Unzip ncurses_programs.tar.gz with tar zxvf ncurses_programs.tar.gz Use jade to create various formats. For example if you just want to create the multiple html files, you would use jade -t sgml -i html -d <path to docbook html stylesheet> NCURSES-Programming-HOWTO.sgml to get pdf, first create a single html file of the HOWTO with jade -t sgml -i html -d <path to docbook html stylesheet> -V nochunks NCURSES-Programming-HOWTO.sgml > NCURSES-ONE-BIG-FILE.html
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -