Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Views: 4183461[1X2 [33X[0;0YInterface to the [10Xncurses[110X[101X[1X Library[133X[101X23[33X[0;0YIn this chapter we describe the [5XGAP[105X interface to the [5XGNU[105X [10Xcurses[110X/[10Xncurses[110X4[10XC[110X-library. This library contains routines to manipulate the contents of5terminal windows. It allows one to write programs which should work on a6wide variety of terminal emulations with different sets of capabilities.[133X78[33X[0;0YThis technical chapter is intended for readers who want to program new9applications using the [10Xncurses[110X functionality. If you are only interested in10the function [2XNCurses.BrowseGeneric[102X ([14X4.3-1[114X) from this package or some of its11applications you can skip this chapter.[133X1213[33X[0;0YDetailed documentation of the [10Xncurses[110X library is probably available in your14operating system (try [10Xman ncurses[110X) and from the web (see for example [NCu]).15Here, we only give short reminders about the functions provided in the [5XGAP[105X16interface and explain how to use the [5XGAP[105X functions.[133X171819[1X2.1 [33X[0;0YThe [10Xncurses[110X[101X[1X Library[133X[101X2021[33X[0;0YIn this section we list the functions from the GNU [10Xncurses[110X library and its22[10Xpanel[110X extension which are made available in [5XGAP[105X via the [5XBrowse[105X package. See23the following section [14X2.2[114X for explanations how to use these functions from24within [5XGAP[105X.[133X2526[33X[0;0YThe basic objects to manipulate are called [13Xwindows[113X, they correspond to27rectangular regions of the terminal screen. Windows can overlap but [10Xncurses[110X28cannot handle this for the display. Therefore windows can be wrapped in29[13Xpanels[113X, they provide a display depth for windows and it is possible to move30panels to the top and bottom of the display or to hide a panel.[133X3132[33X[0;0YWe will not import all the functions of the [10Xncurses[110X library to [5XGAP[105X. For33example, there are many pairs of functions with the same name except for a34leading [10Xw[110X (like [10Xmove[110X and [10Xwmove[110X for moving the cursor in a window). Here, we35only import the versions with [10Xw[110X, which get a window as first argument. The36functions without [10Xw[110X are for the [10Xncurses[110X standard screen window [10Xstdscr[110X which37is available as window [10X0[110X in [5XGAP[105X. Similarly, there are functions with the38same name except for an extra [10Xn[110X (like [10Xwaddstr[110X and [10Xwaddnstr[110X for placing a39string into a window). Here, we only import the safer functions with [10Xn[110X which40get the number of characters to write as argument. (More convenient41functions are then implemented on the [5XGAP[105X level.)[133X424344[1X2.1-1 [33X[0;0YSetting the terminal[133X[101X4546[33X[0;0YWe first list flags for setting the basic behavior of a terminal. With47[10Xsavetty[110X/[10Xresetty[110X a setting can be stored and recovered.[133X4849[8X[10Xsavetty()[110X[8X[108X50[33X[0;6YThis stores the current setting of the terminal in a buffer.[133X5152[8X[10Xresetty()[110X[8X[108X53[33X[0;6YThis resets the terminal to what was stored in the last call to54[10Xsavetty[110X.[133X5556[8X [10Xcbreak()/nocbreak()[110X[8X[108X57[33X[0;6YIn [10Xcbreak[110X mode each input character from a terminal is directly58forwarded to the application (but see [10Xkeypad[110X). With [10Xnocbreak[110X this only59happens after a newline or return is typed.[133X6061[8X[10Xkeypad(win, bool)[110X[8X[108X62[33X[0;6YIf set to [9Xtrue[109X some special input like arrow or function keys can be63read as single characters from the input (such keys actually generate64certain sequences of characters), see also [14X2.1-4[114X. (The [3Xwin[103X argument is65irrelevant.)[133X6667[8X [10Xecho()[110X[8X/[10Xnoecho()[110X[8X[108X68[33X[0;6YThis determines if input characters are automatically echoed by the69terminal at the current cursor position.[133X7071[8X[10Xcurs_set(vis)[110X[8X[108X72[33X[0;6YThis determines the visibility of the cursor. The argument [3Xvis[103X=0 makes73the cursor invisible. With [3Xvis[103X=1 it becomes visible; some terminals74allow also higher levels of visibility.[133X7576[8X[10Xwtimeout(win, delay)[110X[8X[108X77[33X[0;6YHere [3Xdelay[103X determines a timeout in milliseconds for reading characters78from the input of a window. Negative values mean infinity, that is a79blocking read.[133X8081[8X [10Xnl()[110X[8X/[10Xnonl()[110X[8X[108X82[33X[0;6YWith [10Xnl[110X a return on input is translated to a newline character and a83newline on output is interpreted as return and linefeed.[133X8485[8X[10Xintrflush(win, bool)[110X[8X[108X86[33X[0;6YThis flag determines if after an interrupt pending output to the87terminal is flushed. (The [3Xwin[103X argument is irrelevant.)[133X8889[8X[10Xidlok(win, bool)[110X[8X[108X90[33X[0;6YWith [9Xtrue[109X the library tries to use a hardware line insertion91functionality (in particular for scrolling).[133X9293[8X[10Xscrollok(win, bool)[110X[8X[108X94[33X[0;6YIf set to [9Xtrue[109X moving the cursor down from the last line of a window95causes scrolling of the whole window, otherwise nothing happens.[133X9697[8X[10Xleaveok(win, bool)[110X[8X[108X98[33X[0;6YIf set to [9Xtrue[109X a refresh of the window leaves the cursor at its99current location, otherwise this is not guaranteed.[133X100101[8X[10Xclearok(win, bool)[110X[8X[108X102[33X[0;6YIf set to [9Xtrue[109X the next refresh of the window will clear the screen103completely and redraw everything.[133X104105[8X[10Ximmedok(win, bool)[110X[8X[108X106[33X[0;6YIf set to [9Xtrue[109X all changes of the window will automatically also call107a [10Xwrefresh[110X.[133X108109[8X [10Xraw()[110X[8X/[10Xnoraw()[110X[8X[108X110[33X[0;6YSimilar to [10Xcbreak[110X, usually not needed (see the [10Xncurses[110X documentation111for details).[133X112113114[1X2.1-2 [33X[0;0YManipulating windows[133X[101X115116[33X[0;0YIn [10Xncurses[110X an arbitrary number of windows which correspond to rectangular117regions (maybe overlapping) of the screen can be handled. You should always118delete windows which are no longer needed. To get a proper display of119overlapping windows (which may occur by recursively called functions using120this library) we suggest that you always wrap windows in panels, see [14X2.1-3[114X.[133X121122[33X[0;0YFor functions which involve coordinates recall that the upper left corner of123the screen or internally of any window has the coordinates (0,0).[133X124125[8X[10Xnewwin(nlines, ncols, y, x)[110X[8X[108X126[33X[0;6YThis creates a new window whose upper left corner has the coordinates127([3Xy[103X,[3Xx[103X) on the screen and has [3Xnlines[103X lines and [3Xncols[103X columns, if this is128possible. The arguments [3Xnlines[103X and [3Xncols[103X can be zero, then their129maximal possible values are assumed.[133X130131[8X[10Xdelwin(win)[110X[8X[108X132[33X[0;6YDeletes a window.[133X133134[8X[10Xmvwin(win, y, x)[110X[8X[108X135[33X[0;6YMoves the upper left corner of the window to the given coordinates, if136the window still fits on the screen. With panels don't use this137function, but use [10Xmove_panel[110X mentioned below.[133X138139[8X[10Xwrefresh(win)[110X[8X[108X140[33X[0;6YWriting to a window only changes some internal buffers, this function141copies the window content to the actual display screen. You don't need142this function if you wrap your windows in panels, use [10Xupdate_panels[110X143and [10Xdoupdate[110X instead.[133X144145[8X[10Xdoupdate()[110X[8X[108X146[33X[0;6YUse this function to update the content of your display screen to the147current content of all windows. If your terminal is not yet in visual148mode this function changes to visual mode.[133X149150[8X[10Xendwin()[110X[8X[108X151[33X[0;6YUse this function to leave the visual mode of your terminal. (Remark:152If you use this function while not in visual mode the cursor will be153moved to the line where the visual mode was started last time. To154avoid this use [10Xisendwin[110X first.)[133X155156[8X[10Xisendwin()[110X[8X[108X157[33X[0;6YReturns [9Xtrue[109X if called while not in visual mode and [9Xfalse[109X otherwise[133X158159[8X[10Xgetbegyx(win)[110X[8X[108X160[33X[0;6YGet the coordinates of the upper left corner of a window on the161screen.[133X162163[8X[10Xgetmaxyx(win)[110X[8X[108X164[33X[0;6YGet the number of lines and columns of a window.[133X165166167[1X2.1-3 [33X[0;0YManipulating panels[133X[101X168169[33X[0;0YWrap windows in panels to get a proper handling of overlapping windows on170the display. Don't forget to delete a panel before deleting the171corresponding window.[133X172173[8X[10Xnew_panel(win)[110X[8X[108X174[33X[0;6YCreate a panel for a window.[133X175176[8X[10Xdel_panel(pan)[110X[8X[108X177[33X[0;6YDelete a panel.[133X178179[8X[10Xupdate_panels()[110X[8X[108X180[33X[0;6YUse this function to copy changes of windows and panels to a screen181buffer. Then call [10Xdoupdate()[110X to update the display screen.[133X182183[8X[10Xmove_panel(pan, y, x)[110X[8X[108X184[33X[0;6YMove top left corner of a panel wrapped window to coordinates ([3Xy[103X,[3Xx[103X) if185possible.[133X186187[8X [10Xhide_panel(pan)[110X[8X/[10Xshow_panel(pan)[110X[8X[108X188[33X[0;6YHide or show, respectively, the content of a panel on the display189screen.[133X190191[8X [10Xtop_panel(pan)[110X[8X/[10Xbottom_panel(pan)[110X[8X[108X192[33X[0;6YMove a panel to the top or bottom of all panels, respectively.[133X193194[8X [10Xpanel_below(pan)[110X[8X/[10Xpanel_above(pan)[110X[8X[108X195[33X[0;6YReturn the panel directly below or above the given one, respectively.196With argument [10X0[110X the top or bottom panel are returned, respectively. If197argument is the bottom or top panel, respectively, then [9Xfalse[109X is198returned.[133X199200201[1X2.1-4 [33X[0;0YGetting keyboard input[133X[101X202203[33X[0;0YIf you want to read input from the user first adjust the terminal settings204of [10Xcbreak[110X, [10Xkeypad[110X, [10Xecho[110X, [10Xwtimeout[110X and [10Xcurs_set[110X to your needs, see [14X2.1-1[114X. The205basic functions are as follows.[133X206207[8X[10Xwgetch(win)[110X[8X[108X208[33X[0;6YReads one character from user input (returned as integer). If [10Xwtimeout[110X209was set with a positive [3Xdelay[103X then the function returns [9Xfalse[109X if there210was no input for [3Xdelay[103X milliseconds. Note that in [10Xnocbreak[110X mode typed211characters reach the application only after typing a return. If the212[10Xkeypad[110X flag is set to [9Xtrue[109X some special keys can be read like single213characters; the keys are explained below. (Note that there is only one214input queue for all windows.)[133X215216[8X[10Xungetch(char)[110X[8X[108X217[33X[0;6YPuts back the character [3Xchar[103X on the input queue.[133X218219[33X[0;0YSome terminals allow one to read special keys like one character, we import220some of the symbolic names of such keys into [5XGAP[105X. You can check for such221characters by comparing with the components of the record [10XNCurses.keys[110X,222these are[133X223224[8X[10XUP[110X[8X/[10XDOWN[110X[8X/[10XLEFT[110X[8X/[10XRIGHT[110X[8X[108X225[33X[0;6Ythe arrow keys[133X226227[8X[10XPPAGE[110X[8X/[10XNPAGE[110X[8X[108X228[33X[0;6Ythe page up and page down keys[133X229230[8X[10XHOME[110X[8X/[10XEND[110X[8X[108X231[33X[0;6Ythe home and end keys[133X232233[8X[10XBACKSPACE[110X[8X/[10XDC[110X[8X[108X234[33X[0;6Ythe backspace and delete keys[133X235236[8X[10XIC[110X[8X[108X237[33X[0;6Ythe insert key[133X238239[8X[10XENTER[110X[8X[108X240[33X[0;6Ythe enter key[133X241242[8X[10XF1[110X[8X/[10XF2[110X[8X/../[10XF24[110X[8X[108X243[33X[0;6Ythe function keys[133X244245[8X[10XMOUSE[110X[8X[108X246[33X[0;6Ya pseudo key to detect mouse events[133X247248[8X[10XA1[110X[8X/[10XA3[110X[8X/[10XB2[110X[8X/[10XC1[110X[8X/[10XC3[110X[8X[108X249[33X[0;6Ythe keys around the arrow keys on a num pad[133X250251[33X[0;0YIt can happen that on a specific keyboard there is no key for some of these.252Also, not all terminals can interpret all of these keys. You can check this253with the function[133X254255[8X[10Xhas_key(key)[110X[8X[108X256[33X[0;6YChecks if the special key [3Xkey[103X is recognized by the terminal.[133X257258259[1X2.1-5 [33X[0;0YWriting to windows[133X[101X260261[33X[0;0YThe display of text in [10Xncurses[110X windows has two aspects. The first is to get262actual characters on the screen. The second is to specify attributes which263influence the display, for example normal or bold fonts or colors. This264subsection is for the first aspect. Possible attributes are explained below265in [14X2.1-7[114X.[133X266267[8X[10Xwmove(win, y, x)[110X[8X[108X268[33X[0;6YMoves the cursor to position ([3Xy[103X,[3Xx[103X), recall that the coordinates are269zero based, (0,0) being the top left corner.[133X270271[8X[10Xwaddnstr(win, str, len)[110X[8X[108X272[33X[0;6YWrites the string [3Xstr[103X to the window starting from the current cursor273position. Writes at most [3Xlen[103X characters. At end of line the cursor274moves to the beginning of next line. The behavior at the end of the275window depends on the setting of [10Xscrollok[110X, see [14X2.1-1[114X.[133X276277[8X[10Xwaddch(win, char)[110X[8X[108X278[33X[0;6YWrites a character to the window at the current cursor position and279moves the cursor on. The character [3Xchar[103X is given as integer and can280include attribute information.[133X281282[8X[10Xwborder(win, charlist)[110X[8X[108X283[33X[0;6YDraws a border around the window. If [3Xcharlist[103X is a plain list of eight284[5XGAP[105X characters these are taken for left/right/top/bottom sides and285top-left/top-right/bottom-left/bottom-right corners. Otherwise default286characters are used. (See [2XNCurses.WBorder[102X ([14X2.2-9[114X) for a more user287friendly interface.)[133X288289[8X[10Xwvline(win, char, len)[110X[8X[108X290[33X[0;6YWrites a vertical line of length [3Xlen[103X (or as long as fitting into the291window) starting from the current cursor position to the bottom, using292the character [3Xchar[103X. If [3Xchar[103X=[10X0[110X the default character is used.[133X293294[8X[10Xwhline(win, char, len)[110X[8X[108X295[33X[0;6YSame as [10Xwvline[110X but for horizontal lines starting from the cursor296position to the right.[133X297298[8X[10Xwerase(win)[110X[8X[108X299[33X[0;6YDeletes all characters in the window.[133X300301[8X[10Xwclear(win)[110X[8X[108X302[33X[0;6YLike [10Xwerase[110X, but also calls [10Xclearok[110X.[133X303304[8X[10Xwclrtobot(win)[110X[8X[108X305[33X[0;6YDeletes all characters from cursor position to the right and bottom.[133X306307[8X[10Xwclrtoeol(win)[110X[8X[108X308[33X[0;6YDeletes all characters from cursor position to end of line.[133X309310[8X[10Xwinch(win)[110X[8X[108X311[33X[0;6YReturns the character at current cursor position, as integer and312including color and attribute information.[133X313314[8X[10Xgetyx(win)[110X[8X[108X315[33X[0;6YReturns the current cursor position.[133X316317[8X[10Xwaddstr(win, str)[110X[8X[108X318[33X[0;6YDelegates to [10Xwaddnstr(win, str, Length(str))[110X.[133X319320321[1X2.1-6 [33X[0;0YLine drawing characters[133X[101X322323[33X[0;0YFor drawing lines and grids in a terminal window you should use some324"virtual" characters which are available as components of the record325[10XNCurses.lineDraw[110X. On some terminals these are nicely displayed as proper326lines (on others they are simulated by ASCII characters). These are:[133X327328[8X[10XBLOCK[110X[8X[108X329[33X[0;6Ysolid block[133X330331[8X[10XBOARD[110X[8X[108X332[33X[0;6Yboard of squares[133X333334[8X[10XBTEE/LTEE/RTEE/TTEE[110X[8X[108X335[33X[0;6Ybottom/left/right/top tee[133X336337[8X[10XBULLET[110X[8X[108X338[33X[0;6Ybullet[133X339340[8X[10XCKBOARD[110X[8X[108X341[33X[0;6Ychecker board[133X342343[8X[10XDARROW/LARROW/RARROW/UARROW[110X[8X[108X344[33X[0;6Ydown/left/right/up arrow[133X345346[8X[10XDEGREE[110X[8X[108X347[33X[0;6Ydegree symbol[133X348349[8X[10XDIAMOND[110X[8X[108X350[33X[0;6Ydiamond[133X351352[8X[10XGEQUAL[110X[8X[108X353[33X[0;6Ygreater than or equal[133X354355[8X[10XHLINE/VLINE[110X[8X[108X356[33X[0;6Yhorizontal/vertical line[133X357358[8X[10XLANTERN[110X[8X[108X359[33X[0;6Ylantern symbol[133X360361[8X[10XLEQUAL[110X[8X[108X362[33X[0;6Yless than or equal[133X363364[8X[10XLLCORNER/LRCORNER/ULCORNER/URCORNER[110X[8X[108X365[33X[0;6Ylower left/lower right/upper left/upper right corner[133X366367[8X[10XNEQUAL[110X[8X[108X368[33X[0;6Ynot equal[133X369370[8X[10XPI[110X[8X[108X371[33X[0;6Yletter pi[133X372373[8X[10XPLMINUS[110X[8X[108X374[33X[0;6Yplus-minus[133X375376[8X[10XPLUS[110X[8X[108X377[33X[0;6Ycrossing lines like a plus[133X378379[8X[10XS1/S3/S7/S9[110X[8X[108X380[33X[0;6Yscan line 1/3/7/9[133X381382[8X[10XSTERLING[110X[8X[108X383[33X[0;6Ypound sterling[133X384385386[1X2.1-7 [33X[0;0YText attributes and colors[133X[101X387388[33X[0;0YIn addition to the actual characters to be written to the screen the way389they are displayed can be changed by additional [13Xattributes[113X. (There should be390no danger to mix up this notion of attributes with the one introduced391in [14X'Reference: Attributes'[114X.) The available attributes are stored in the392record [10XNCurses.attrs[110X, they are[133X393394[8X[10XNORMAL[110X[8X[108X395[33X[0;6Ynormal display with no extra attributes.[133X396397[8X[10XSTANDOUT[110X[8X[108X398[33X[0;6Ydisplays text in the best highlighting mode of the terminal.[133X399400[8X[10XUNDERLINE[110X[8X[108X401[33X[0;6Yunderlines the text.[133X402403[8X[10XREVERSE[110X[8X[108X404[33X[0;6Ydisplay in reverse video by exchanging the foreground and background405color.[133X406407[8X[10XBLINK[110X[8X[108X408[33X[0;6Ydisplays the text blinking.[133X409410[8X[10XDIM[110X[8X[108X411[33X[0;6Ydisplays the text half bright.[133X412413[8X[10XBOLD[110X[8X[108X414[33X[0;6Ydisplays the text in a bold font.[133X415416[33X[0;0YNote that not all of these work with all types of terminals, or some may417cause the same display. Furthermore, if [10XNCurses.attrs.has_colors[110X is [9Xtrue[109X418there is a list [10XNCurses.attrs.ColorPairs[110X of attributes to set the foreground419and background color. These should be accessed indirectly with420[2XNCurses.ColorAttr[102X ([14X2.2-1[114X). Attributes can be combined by adding their values421(internally, they are represented by integers). They can also be added to422the integer representing a character for use with [10Xwaddch[110X.[133X423424[33X[0;0YThe library functions for setting attributes are:[133X425426[8X[10Xwattrset(win, attr)[110X[8X[108X427[33X[0;6YThis sets the default (combined) attributes for a window which is428added to all characters written to it; using [10XNCurses.attrs.NORMAL[110X as429attribute is a reset.[133X430431[8X [10Xwattron(win, attr)[110X[8X/[10Xwattroff(win, attr)[110X[8X[108X432[33X[0;6YThis sets or unsets one or some default attributes of the window433without changing the others.[133X434435[8X[10Xwattr_get(win)[110X[8X[108X436[33X[0;6YThis returns the current default attribute and default color pair of a437window.[133X438439[8X[10Xwbkgdset(win, attr)[110X[8X[108X440[33X[0;6YThis is similar to [10Xwattrset[110X but you can also add a character to [3Xattr[103X441which is used as default instead of blanks.[133X442443[8X[10Xwbkgd(win, attr)[110X[8X[108X444[33X[0;6YThis function changes the attributes for all characters in the window445to [3Xattr[103X, also used for further characters written to that window.[133X446447448[1X2.1-8 [33X[0;0YLow level [10Xncurses[110X[101X[1X mouse support[133X[101X449450[33X[0;0YMany [10Xxterm[110X based terminals support mouse events. The recognition of mouse451events by the [10Xncurses[110X input queue can be switched on and off. If switched on452and a mouse event occurs, then [10XNCurses.wgetch[110X gets [10XNCurses.keys.MOUSE[110X if the453[10Xkeypad[110X flag is [9Xtrue[109X (see [14X2.1-4[114X). If this is read one must call454[10XNCurses.getmouse[110X which reads further characters from the input queue and455interprets them as details on the mouse event. In most cases the function456[2XNCurses.GetMouseEvent[102X ([14X2.2-10[114X) can be used in applications (it calls457[10XNCurses.getmouse[110X). The following low level functions are available as458components of the record [10XNCurses[110X.[133X459460[33X[0;0YThe names of mouse events which may be possible are stored in the list461[10XNCurses.mouseEvents[110X, which starts [10X[[110X [10X"BUTTON1_PRESSED",[110X [10X"BUTTON1_RELEASED",[110X462[10X"BUTTON1_CLICKED",[110X [10X"BUTTON1_DOUBLE_CLICKED",[110X [10X"BUTTON1_TRIPLE_CLICKED",[110X [10X...[110X463and contains the same for buttons number 2 to 5 and a few other events.[133X464465[8X [10Xmousemask(intlist)[110X[8X[108X466[33X[0;6YThe argument [3Xintlist[103X is a list of integers specifying mouse events. An467entry [10Xi[110X refers to the event described in [10XNCurses.mouseEvents[i+1][110X. It468returns a record with components [10X.new[110X (for the current setting) and469[10X.old[110X (for the previous setting) which are again lists of integers with470the same meaning. Note that [10X.new[110X may be different from [3Xintlist[103X, it is471always the empty list if the terminal does not support mouse events.472In applications use [2XNCurses.UseMouse[102X ([14X2.2-10[114X) instead of this low473level function.[133X474475[8X [10Xgetmouse()[110X[8X[108X476[33X[0;6YThis function must be called after a key [10XNCurses.keys.MOUSE[110X was read.477It returns a list with three entries [10X[y, x, intlist][110X where [10Xy[110X and [10Xx[110X are478the coordinates of the character cell where the mouse event occured479and [10Xintlist[110X describes the event, it should have length one and refers480to a position in [10XNCurses.mouseEvents[110X.[133X481482[8X [10Xwenclose(win, y, x)[110X[8X[108X483[33X[0;6YThis functions returns [9Xtrue[109X if the screen position [3Xy[103X, [3Xx[103X is within484window [3Xwin[103X and [9Xfalse[109X otherwise.[133X485486[8X [10Xmouseinterval(t)[110X[8X[108X487[33X[0;6YSets the time to recognize a press and release of a mouse button as a488click to [3Xt[103X milliseconds. (Note that this may have no effect because a489window manager may catch this.)[133X490491492[1X2.1-9 [33X[0;0YMiscellaneous function[133X[101X493494[33X[0;0YWe also provide the [10Xncurses[110X function [10Xmnap(msec)[110X which is a sleep for [3Xmsec[103X495milliseconds.[133X496497[33X[0;0YFurthermore, there a two utilities which can be useful for scripts and498testing, namely a check if standard input or standard output are connected499to terminals. These can be called as [10XNCurses.IsStdinATty()[110X or500[10XNCurses.IsStdoutATty()[110X, respectively.[133X501502503[1X2.2 [33X[0;0YThe [10Xncurses[110X[101X[1X [5XGAP[105X[101X[1X functions[133X[101X504505[33X[0;0YThe functions of the [10Xncurses[110X library are used within [5XGAP[105X very similarly to506their [10XC[110X equivalents. The functions are available as components of a record507[10XNCurses[110X with the name of the [10XC[110X function (e.g., [10XNCurses.newwin[110X).[133X508509[33X[0;0YIn [5XGAP[105X the [10Xncurses[110X windows are accessed via integers (as returned by510[10XNCurses.newwin[110X). The standard screen [10Xstdscr[110X from the [10Xncurses[110X library is511available as window number [10X0[110X. But this should not be used; to allow512recursive applications of [10Xncurses[110X always create a new window, wrap it in a513panel and delete both when they are no longer needed.[133X514515[33X[0;0YEach window can be wrapped in one panel which is accessed by the same516integer. (Window [10X0[110X cannot be used with a panel.)[133X517518[33X[0;0YCoordinates in windows are the same zero based integers as in the519corresponding [10XC[110X functions. The interface of functions which [13Xreturn[113X520coordinates is slightly different from the [10XC[110X version; they just return lists521of integers and you just give the window as argument, e.g.,522[10XNCurses.getmaxyx(win)[110X returns a list [10X[nrows, ncols][110X of two integers.[133X523524[33X[0;0YCharacters to be written to a window can be given either as [5XGAP[105X characters525like [10X'a'[110X or as integers like [10XINT_CHAR('a') = 97[110X. If you use the integer526version you can also add attributes including color settings to it for use527with [10XNCurses.waddch[110X.[133X528529[33X[0;0YWhen writing an application decide about an appropriate terminal setting for530your visual mode windows, see [14X2.1-1[114X and the utility function [2XNCurses.SetTerm[102X531([14X2.2-2[114X) below. Use [10XNCurses.savetty()[110X and [10XNCurses.resetty()[110X to save and532restore the previous setting.[133X533534[33X[0;0YWe also provide some higher level functionality for displaying marked up535text, see [2XNCurses.PutLine[102X ([14X2.2-6[114X) and [2XNCurses.IsAttributeLine[102X ([14X2.2-3[114X).[133X536537[33X[0;0YWe now describe some utility functions for putting text on a terminal538window.[133X539540[1X2.2-1 NCurses.ColorAttr[101X541542[29X[2XNCurses.ColorAttr[102X( [3Xfgcolor[103X, [3Xbgcolor[103X ) [32X function543[6XReturns:[106X [33X[0;10Yan attribute for setting the foreground and background color to be544used on a terminal window (it is a [5XGAP[105X integer).[133X545546[29X[2XNCurses.attrs.has_colors[102X[32X global variable547548[33X[0;0YThe return value can be used like any other attribute as described in [14X2.1-7[114X.549The arguments [3Xfgcolor[103X and [3Xbgcolor[103X can be given as strings, allowed are those550in [10X[ "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"551][110X. These are the default foreground colors 0 to 7 on ANSI terminals.552Alternatively, the numbers 0 to 7 can be used directly as arguments.[133X553554[33X[0;0YNote that terminals can be configured in a way such that these named colors555are not the colors which are actually displayed.[133X556557[33X[0;0YThe variable [2XNCurses.attrs.has_colors[102X is set to [9Xtrue[109X or [9Xfalse[109X if the558terminal supports colors or not, respectively. If a terminal does not559support colors then [2XNCurses.ColorAttr[102X always returns [10XNCurses.attrs.NORMAL[110X.[133X560561[33X[0;0YFor an attribute setting the foreground color with the default background562color of the terminal use [10X-1[110X as [3Xbgcolor[103X or the same as [3Xfgcolor[103X.[133X563564[4X[32X Example [32X[104X565[4X[25Xgap>[125X [27Xwin := NCurses.newwin(0,0,0,0);; pan := NCurses.new_panel(win);;[127X[104X566[4X[25Xgap>[125X [27Xdefc := NCurses.defaultColors;;[127X[104X567[4X[25Xgap>[125X [27XNCurses.wmove(win, 0, 0);;[127X[104X568[4X[25Xgap>[125X [27Xfor a in defc do for b in defc do[127X[104X569[4X[25X>[125X [27X NCurses.wattrset(win, NCurses.ColorAttr(a, b));[127X[104X570[4X[25X>[125X [27X NCurses.waddstr(win, Concatenation(a,"/",b,"\t"));[127X[104X571[4X[25X>[125X [27X od; od;[127X[104X572[4X[25Xgap>[125X [27Xif NCurses.IsStdoutATty() then[127X[104X573[4X[25X>[125X [27X NCurses.update_panels();; NCurses.doupdate();;[127X[104X574[4X[25X>[125X [27X NCurses.napms(5000);; # show for 5 seconds[127X[104X575[4X[25X>[125X [27X NCurses.endwin();; NCurses.del_panel(pan);; NCurses.delwin(win);;[127X[104X576[4X[25X>[125X [27X fi;[127X[104X577[4X[32X[104X578579[1X2.2-2 NCurses.SetTerm[101X580581[29X[2XNCurses.SetTerm[102X( [[3Xrecord[103X] ) [32X function582583[33X[0;0YThis function provides a unified interface to the various terminal setting584functions of [10Xncurses[110X listed in [14X2.1-1[114X. The optional argument is a record with585components which are assigned to [9Xtrue[109X or [9Xfalse[109X. Recognised components are:586[10Xcbreak[110X, [10Xecho[110X, [10Xnl[110X, [10Xintrflush[110X, [10Xleaveok[110X, [10Xscrollok[110X, [10Xkeypad[110X, [10Xraw[110X (with the587obvious meaning if set to [9Xtrue[109X or [9Xfalse[109X, respectively).[133X588589[33X[0;0YThe default, if no argument is given, is [10Xrec(cbreak := true, echo := false,590nl := false, intrflush := false, leaveok := true, scrollok := false, keypad591:= true)[110X. (This is a useful setting for many applications.) If there is an592argument [3Xrecord[103X, then the given components overwrite the corresponding593defaults.[133X594595[1X2.2-3 NCurses.IsAttributeLine[101X596597[29X[2XNCurses.IsAttributeLine[102X( [3Xobj[103X ) [32X function598[6XReturns:[106X [33X[0;10Y[9Xtrue[109X if the argument describes a string with attributes.[133X599600[33X[0;0YAn [13Xattribute line[113X describes a string with attributes. It is represented by601either a string or a dense list of strings, integers, and Booleans602immediately following integers, where at least one list entry must [13Xnot[113X be a603string. (The reason is that we want to be able to distinguish between an604attribute line and a list of such lines, and that the case of plain strings605is perhaps the most usual one, so we do not want to force wrapping each606string in a list.) The integers denote attribute values such as color or607font information, the Booleans denote that the attribute given by the608preceding integer is set or reset.[133X609610[33X[0;0YIf an integer is not followed by a Boolean then it is used as the attribute611for the following characters, that is it overwrites all previously set612attributes. Note that in some applications the variant with explicit Boolean613values is preferable, because such a line can nicely be highlighted just by614prepending a [10XNCurses.attrs.STANDOUT[110X attribute.[133X615616[33X[0;0YFor an overview of attributes, see [14X2.1-7[114X.[133X617618[4X[32X Example [32X[104X619[4X[25Xgap>[125X [27XNCurses.IsAttributeLine( "abc" );[127X[104X620[4X[28Xtrue[128X[104X621[4X[25Xgap>[125X [27XNCurses.IsAttributeLine( [ "abc", "def" ] );[127X[104X622[4X[28Xfalse[128X[104X623[4X[25Xgap>[125X [27XNCurses.IsAttributeLine( [ NCurses.attrs.UNDERLINE, true, "abc" ] );[127X[104X624[4X[28Xtrue[128X[104X625[4X[25Xgap>[125X [27XNCurses.IsAttributeLine( "" ); NCurses.IsAttributeLine( [] );[127X[104X626[4X[28Xtrue[128X[104X627[4X[28Xfalse[128X[104X628[4X[32X[104X629630[33X[0;0YThe [13Xempty string[113X is an attribute line whereas the [13Xempty list[113X (which is not631in [2XIsStringRep[102X ([14XReference: IsStringRep[114X)) is [13Xnot[113X an attribute line.[133X632633[1X2.2-4 NCurses.ConcatenationAttributeLines[101X634635[29X[2XNCurses.ConcatenationAttributeLines[102X( [3Xlines[103X[, [3Xkeep[103X] ) [32X function636[6XReturns:[106X [33X[0;10Yan attribute line.[133X637638[33X[0;0YFor a list [3Xlines[103X of attribute lines (see [2XNCurses.IsAttributeLine[102X ([14X2.2-3[114X)),639[10XNCurses.ConcatenationAttributeLines[110X returns the attribute line obtained by640concatenating the attribute lines in [3Xlines[103X.[133X641642[33X[0;0YIf the optional argument [3Xkeep[103X is [9Xtrue[109X then attributes set in an entry of643[3Xlines[103X are valid also for the following entries of [3Xlines[103X. Otherwise (in644particular if there is no second argument) the attributes are reset to645[10XNCurses.attrs.NORMAL[110X between the entries of [3Xlines[103X.[133X646647[4X[32X Example [32X[104X648[4X[25Xgap>[125X [27Xplain_str:= "hello";;[127X[104X649[4X[25Xgap>[125X [27Xwith_attr:= [ NCurses.attrs.BOLD, "bold" ];;[127X[104X650[4X[25Xgap>[125X [27XNCurses.ConcatenationAttributeLines( [ plain_str, plain_str ] );[127X[104X651[4X[28X"hellohello"[128X[104X652[4X[25Xgap>[125X [27XNCurses.ConcatenationAttributeLines( [ plain_str, with_attr ] );[127X[104X653[4X[28X[ "hello", 2097152, "bold" ][128X[104X654[4X[25Xgap>[125X [27XNCurses.ConcatenationAttributeLines( [ with_attr, plain_str ] );[127X[104X655[4X[28X[ 2097152, "bold", 0, "hello" ][128X[104X656[4X[25Xgap>[125X [27XNCurses.ConcatenationAttributeLines( [ with_attr, with_attr ] );[127X[104X657[4X[28X[ 2097152, "bold", 0, 2097152, "bold" ][128X[104X658[4X[25Xgap>[125X [27XNCurses.ConcatenationAttributeLines( [ with_attr, with_attr ], true );[127X[104X659[4X[28X[ 2097152, "bold", 2097152, "bold" ][128X[104X660[4X[32X[104X661662[1X2.2-5 NCurses.RepeatedAttributeLine[101X663664[29X[2XNCurses.RepeatedAttributeLine[102X( [3Xline[103X, [3Xwidth[103X ) [32X function665[6XReturns:[106X [33X[0;10Yan attribute line.[133X666667[33X[0;0YFor an attribute line [3Xline[103X (see [2XNCurses.IsAttributeLine[102X ([14X2.2-3[114X)) and a668positive integer [3Xwidth[103X, [10XNCurses.RepeatedAttributeLine[110X returns an attribute669line with [3Xwidth[103X displayed characters (see [2XNCurses.WidthAttributeLine[102X670([14X2.2-7[114X)) that is obtained by concatenating sufficiently many copies of [3Xline[103X671and cutting off a tail if applicable.[133X672673[4X[32X Example [32X[104X674[4X[25Xgap>[125X [27XNCurses.RepeatedAttributeLine( "12345", 23 );[127X[104X675[4X[28X"12345123451234512345123"[128X[104X676[4X[25Xgap>[125X [27XNCurses.RepeatedAttributeLine( [ NCurses.attrs.BOLD, "12345" ], 13 );[127X[104X677[4X[28X[ 2097152, "12345", 0, 2097152, "12345", 0, 2097152, "123" ][128X[104X678[4X[32X[104X679680[1X2.2-6 NCurses.PutLine[101X681682[29X[2XNCurses.PutLine[102X( [3Xwin[103X, [3Xy[103X, [3Xx[103X, [3Xlines[103X[, [3Xskip[103X] ) [32X function683[6XReturns:[106X [33X[0;10Y[9Xtrue[109X if [3Xlines[103X were written, otherwise [9Xfalse[109X.[133X684685[33X[0;0YThe argument [3Xlines[103X can be a list of attribute lines (see686[2XNCurses.IsAttributeLine[102X ([14X2.2-3[114X)) or a single attribute line. This function687writes the attribute lines to window [3Xwin[103X at and below of position [3Xy[103X, [3Xx[103X.[133X688689[33X[0;0YIf the argument [3Xskip[103X is given, it must be a nonnegative integer. In that690case the first [3Xskip[103X characters of each given line are not written to the691window (but the attributes are).[133X692693[1X2.2-7 NCurses.WidthAttributeLine[101X694695[29X[2XNCurses.WidthAttributeLine[102X( [3Xline[103X ) [32X function696[6XReturns:[106X [33X[0;10Ynumber of displayed characters in an attribute line.[133X697698[33X[0;0YFor an attribute line [3Xline[103X (see [2XNCurses.IsAttributeLine[102X ([14X2.2-3[114X)), the699function returns the number of displayed characters of [3Xline[103X.[133X700701[4X[32X Example [32X[104X702[4X[25Xgap>[125X [27XNCurses.WidthAttributeLine( "abcde" );[127X[104X703[4X[28X5[128X[104X704[4X[25Xgap>[125X [27XNCurses.WidthAttributeLine( [ NCurses.attrs.BOLD, "abc",[127X[104X705[4X[25X>[125X [27X NCurses.attrs.NORMAL, "de" ] );[127X[104X706[4X[28X5[128X[104X707[4X[32X[104X708709[1X2.2-8 NCurses.Grid[101X710711[29X[2XNCurses.Grid[102X( [3Xwin[103X, [3Xtrow[103X, [3Xbrow[103X, [3Xlcol[103X, [3Xrcol[103X, [3Xrowinds[103X, [3Xcolinds[103X ) [32X function712713[33X[0;0YThis function draws a grid of horizontal and vertical lines on the window714[3Xwin[103X, using the line drawing characters explained in [14X2.1-6[114X. The given715arguments specify the top and bottom row of the grid, its left and right716column, and lists of row and column numbers where lines should be drawn.[133X717718[4X[32X Example [32X[104X719[4X[25Xgap>[125X [27Xfun := function() local win, pan;[127X[104X720[4X[25X>[125X [27X win := NCurses.newwin(0,0,0,0);[127X[104X721[4X[25X>[125X [27X pan := NCurses.new_panel(win);[127X[104X722[4X[25X>[125X [27X NCurses.Grid(win, 2, 11, 5, 22, [5, 6], [13, 14]);[127X[104X723[4X[25X>[125X [27X NCurses.PutLine(win, 12, 0, "Press <Enter> to quit");[127X[104X724[4X[25X>[125X [27X NCurses.update_panels(); NCurses.doupdate();[127X[104X725[4X[25X>[125X [27X NCurses.wgetch(win);[127X[104X726[4X[25X>[125X [27X NCurses.endwin();[127X[104X727[4X[25X>[125X [27X NCurses.del_panel(pan); NCurses.delwin(win);[127X[104X728[4X[25X>[125X [27Xend;;[127X[104X729[4X[25Xgap>[125X [27Xfun();[127X[104X730[4X[32X[104X731732[1X2.2-9 NCurses.WBorder[101X733734[29X[2XNCurses.WBorder[102X( [3Xwin[103X[, [3Xchars[103X] ) [32X function735736[33X[0;0YThis is a convenient interface to the [10Xncurses[110X function [10Xwborder[110X. It draws a737border around the window [3Xwin[103X. If no second argument is given the default738line drawing characters are used, see [14X2.1-6[114X. Otherwise, [3Xchars[103X must be a list739of [5XGAP[105X characters or integers specifying characters, possibly with740attributes. If [3Xchars[103X has length 8 the characters are used for the741left/right/top/bottom sides and top-left/top-right/bottom-left/bottom-right742corners. If [3Xchars[103X contains 2 characters the first is used for the sides and743the second for all corners. If [3Xchars[103X contains just one character it is used744for all sides including the corners.[133X745746747[1X2.2-10 [33X[0;0YMouse support in [10Xncurses[110X[101X[1X applications[133X[101X748749[29X[2XNCurses.UseMouse[102X( [3Xon[103X ) [32X function750[6XReturns:[106X [33X[0;10Ya record[133X751752[29X[2XNCurses.GetMouseEvent[102X( ) [32X function753[6XReturns:[106X [33X[0;10Ya list of records[133X754755[33X[0;0Y[10Xncurses[110X allows on some terminals ([10Xxterm[110X and related) to catch mouse events.756In principle a subset of events can be caught, see [10Xmousemask[110X in [14X2.1-8[114X. But757this does not seem to work well with proper subsets of possible events758(probably due to intermediate processes X, window manager, terminal759application, ...). Therefore we suggest to catch either all or no mouse760events in applications.[133X761762[33X[0;0YThis can be done with [2XNCurses.UseMouse[102X with argument [9Xtrue[109X to switch on the763recognition of mouse events and [9Xfalse[109X to switch it off. The function returns764a record with components [10X.new[110X and [10X.old[110X which are both set to the status [9Xtrue[109X765or [9Xfalse[109X from after and before the call, respectively. (There does not seem766to be a possibility to get the current status without calling767[2XNCurses.UseMouse[102X.) If you call the function with argument [9Xtrue[109X and the [10X.new[110X768component of the result is [9Xfalse[109X, then the terminal does not support mouse769events.[133X770771[33X[0;0YWhen the recognition of mouse events is switched on and a mouse event occurs772then the key [10XNCurses.keys.MOUSE[110X is found in the input queue, see [10Xwgetch[110X in773[14X2.1-4[114X. If this key is read the low level function [10XNCurses.getmouse[110X must be774called to fetch further details about the event from the input queue, see775[14X2.1-8[114X. In many cases this can be done by calling the function776[2XNCurses.GetMouseEvent[102X which also generates additional information. The777return value is a list of records, one for each panel over which the event778occured, these panels sorted from top to bottom (so, often you will just779need the first entry if there is any). Each of these records has components780[10X.win[110X, the corresponding window of the panel, [10X.y[110X and [10X.x[110X, the relative781coordinates in window [10X.win[110X where the event occured, and [10X.event[110X, which is782bound to one of the strings in [10XNCurses.mouseEvents[110X which describes the783event.[133X784785[33X[0;0Y[13XSuggestion:[113X Always make the use of the mouse optional in your application.786Allow the user to switch mouse usage on and off while your application is787running. Some users may not like to give mouse control to your application,788for example the standard cut and paste functionality cannot be used while789mouse events are caught.[133X790791[1X2.2-11 NCurses.SaveWin[101X792793[29X[2XNCurses.SaveWin[102X( [3Xwin[103X ) [32X function794[29X[2XNCurses.StringsSaveWin[102X( [3Xcont[103X ) [32X function795[29X[2XNCurses.RestoreWin[102X( [3Xwin[103X, [3Xcont[103X ) [32X function796[29X[2XNCurses.ShowSaveWin[102X( [3Xcont[103X ) [32X function797[6XReturns:[106X [33X[0;10Ya [5XGAP[105X object describing the contents of a window.[133X798799[33X[0;0YThese functions can be used to save and restore the contents of [10Xncurses[110X800windows. [2XNCurses.SaveWin[102X returns a list [10X[nrows, ncols, chars][110X giving the801number of rows, number of columns, and a list of integers describing the802content of window [3Xwin[103X. The integers in the latter contain the displayed803characters plus the attributes for the display.[133X804805[33X[0;0YThe function [2XNCurses.StringsSaveWin[102X translates data [3Xcont[103X in form of the806output of [2XNCurses.SaveWin[102X to a list of [10Xnrows[110X strings giving the text of the807rows of the saved window, and ignoring the attributes. You can view the808result with [2XNCurses.Pager[102X ([14X3.1-4[114X).[133X809810[33X[0;0YThe argument [3Xcont[103X for [2XNCurses.RestoreWin[102X must be of the same format as the811output of [2XNCurses.SaveWin[102X. The content of the saved window is copied to the812window [3Xwin[103X, starting from the top-left corner as much as it fits.[133X813814[33X[0;0YThe utility [2XNCurses.ShowSaveWin[102X can be used to display the output of815[2XNCurses.SaveWin[102X (as much of the top-left corner as fits on the screen).[133X816817818819