Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

563501 views
1
2
3 Utilities using ncurses
3
4
In this chapter we describe the usage of some example applications of the
5
ncurses interface provided by the Browse package. They may be of interest by
6
themselves, or they may be used as utility functions within larger
7
applications.
8
9
10
3.1 ncurses utilities
11
12
If you call the functions NCurses.Alert (3.1-1), NCurses.Select (3.1-2),
13
NCurses.GetLineFromUser (3.1-3), or NCurses.Pager (3.1-4) from another
14
ncurses application in visual mode, you should refresh the windows that are
15
still open, by calling NCurses.update_panels and NCurses.doupdate
16
afterwards, see Section 2.1-3 and 2.1-2. Also, if the cursor shall be hidden
17
after that, you should call curs_set with argument 0, see Section 2.1-1,
18
since the cursor is automatically made visible in NCurses.endwin.
19
20
3.1-1 NCurses.Alert
21
22
NCurses.Alert( messages, timeout[, attrs] )  function
23
Returns: the integer corresponding to the character entered, or fail.
24
25
In visual mode, Print (Reference: Print) cannot be used for messages. An
26
alternative is given by NCurses.Alert.
27
28
Let messages be either an attribute line or a nonempty list of attribute
29
lines, and timeout be a nonnegative integer. NCurses.Alert shows messages in
30
a bordered box in the middle of the screen.
31
32
If timeout is zero then the box is closed after any user input, and the
33
integer corresponding to the entered key is returned. If timeout is a
34
positive number n, say, then the box is closed after n milliseconds, and
35
fail is returned.
36
37
If timeout is zero and mouse events are enabled (see NCurses.UseMouse
38
(2.2-10)) then the box can be moved inside the window via mouse events.
39
40
If the optional argument attrs is given, it must be an integer representing
41
attributes such as the components of NCurses.attrs (see Section 2.1-7) or
42
the return value of NCurses.ColorAttr (2.2-1); these attributes are used for
43
the border of the box. The default is NCurses.attrs.NORMAL.
44
45
 Example 
46
gap> NCurses.Alert( "Hello world!", 1000 );
47
fail
48
gap> NCurses.Alert( [ "Hello world!",
49
>  [ "Hello ", NCurses.attrs.BOLD, "bold!" ] ], 1500,
50
>  NCurses.ColorAttr( "red", -1 ) + NCurses.attrs.BOLD );
51
fail
52

53
54
3.1-2 NCurses.Select
55
56
NCurses.Select( poss[, single[, none]] )  function
57
Returns: Position or list of positions, or false.
58
59
This function allows the user to select one or several items from a given
60
list. In the simplest case poss is a list of attribute lines (see
61
NCurses.IsAttributeLine (2.2-3)), each of which should fit on one line. Then
62
NCurses.Select displays these lines and lets the user browse through them.
63
After pressing the Return key the index of the highlighted item is returned.
64
Note that attributes in your lines should be switched on and off separately
65
by true/false entries such that the lines can be nicely highlighted.
66
67
The optional argument single must be true (default) or false. In the second
68
case, an arbitrary number of items can be marked and the function returns
69
the list of their indices.
70
71
If single is true a third argument none can be given. If it is true then it
72
is possible to leave the selection without choosing an item, in this case
73
false is returned.
74
75
More details can be given to the function by giving a record as argument
76
poss. It can have the following components:
77
78
items
79
The list of attribute lines as described above.
80
81
single
82
Boolean with the same meaning as the optional argument single.
83
84
none
85
Boolean with the same meaning as the optional argument none.
86
87
size
88
The size of the window like the first two arguments of NCurses.newwin
89
(default is [0, 0], as big as possible), or the string "fit" which
90
means the smallest possible window.
91
92
align
93
A substring of "bclt", which describes the alignment of the window in
94
the terminal. The meaning and the default are the same as for
95
BrowseData.IsBrowseTableCellData (4.2-1).
96
97
begin
98
Top-left corner of the window like the last two arguments of
99
NCurses.newwin (default is [0, 0], top-left of the screen). This value
100
has priority over the align component.
101
102
attribute
103
An attribute used for the display of the window (default is
104
NCurses.attrs.NORMAL).
105
106
border
107
If the window should be displayed with a border then set to true
108
(default is false) or to an integer representing attributes such as
109
the components of NCurses.attrs (see Section 2.1-7) or the return
110
value of NCurses.ColorAttr (2.2-1); these attributes are used for the
111
border of the box. The default is NCurses.attrs.NORMAL.
112
113
header
114
An attribute line used as header line (the default depends on the
115
settings of single and none).
116
117
hint
118
An attribute line used as hint in the last line of the window (the
119
default depends on the settings of single and none).
120
121
onSubmitFunction
122
A function that is called when the user submits the selection; the
123
argument for this call is the current value of the record poss. If the
124
function returns true then the selected entries are returned as usual,
125
otherwise the selection window is kept open, waiting for new inputs;
126
if the function returns a nonempty list of attribute lines then these
127
messages are shown using NCurses.Alert (3.1-1).
128
129
If mouse events are enabled (see NCurses.UseMouse (2.2-10)) then the window
130
can be moved on the screen via mouse events, the focus can be moved to an
131
entry, and (if single is false) the selection of an entry can be toggled.
132
133
 Example 
134
gap> index := NCurses.Select(["Apples", "Pears", "Oranges"]);
135
gap> index := NCurses.Select(rec(
136
>  items := ["Apples", "Pears", "Oranges"],
137
>  single := false,
138
>  border := true,
139
>  begin := [5, 5],
140
>  size := [8, 60],
141
>  header := "Choose at least two fruits",
142
>  attribute := NCurses.ColorAttr("yellow","red"),
143
>  onSubmitFunction:= function( r )
144
>  if Length( r.RESULT ) < 2 then
145
>  return [ "Choose at least two fruits" ];
146
>  else
147
>  return true;
148
>  fi;
149
>  end ) );
150

151
152
3.1-3 NCurses.GetLineFromUser
153
154
NCurses.GetLineFromUser( pre )  function
155
Returns: User input as string.
156
157
This function can be used to get an input string from the user. It opens a
158
one line window and writes the given string pre into it. Then it waits for
159
user input. After hitting the Return key the typed line is returned as a
160
string to GAP. If the user exits via hitting the Esc key instead of hitting
161
the Return key, the function returns false. (The Esc key may be recognized
162
as input only after a delay of about a second.)
163
164
Some simple editing is possible during user input: The Left, Right, Home and
165
End keys, the Insert/Replace keys, and the Backspace/Delete keys are
166
supported.
167
168
Instead of a string, pre can also be a record with the component prefix,
169
whose value is the string described above. The following optional components
170
of this record are supported.
171
172
window
173
The window with the input field is created relative to this window,
174
the default is 0.
175
176
begin
177
This is a list with the coordinates of the upper left corner of the
178
window with the input field, relative to the window described by the
179
window component; the default is [ y-4, 2 ], where y is the height of
180
this window.
181
182
default
183
This string appears as result when the window is opened, the default
184
is an empty string.
185
186
 Example 
187
gap> str := NCurses.GetLineFromUser("Your Name: ");;
188
gap> Print("Hello ", str, "!\n");
189

190
191
3.1-4 NCurses.Pager
192
193
NCurses.Pager( lines[, border[, ly, lx, y, x]] )  function
194
195
This is a simple pager utility for displaying and scrolling text. The
196
argument lines can be a list of attribute lines (see NCurses.IsAttributeLine
197
(2.2-3)) or a string (the lines are separated by newline characters) or a
198
record. In case of a record the following components are recognized:
199
200
lines
201
The list of attribute lines or a string as described above.
202
203
start
204
Line number to start the display.
205
206
size
207
The size [ly, lx] of the window like the first two arguments of
208
NCurses.newwin (default is [0, 0], as big as possible).
209
210
begin
211
Top-left corner [y, x] of the window like the last two arguments of
212
NCurses.newwin (default is [0, 0], top-left of the screen).
213
214
attribute
215
An attribute used for the display of the window (default is
216
NCurses.attrs.NORMAL).
217
218
border
219
Either one of true/false to show the pager window with or without a
220
standard border. Or it can be string with eight, two or one
221
characters, giving characters to be used for a border, see
222
NCurses.WBorder (2.2-9).
223
224
hint
225
A text for usage info in the last line of the window.
226
227
As an abbreviation the information from border, size and begin can also be
228
specified in optional arguments.
229
230
 Example 
231
gap> lines := List([1..100],i-> ["line ",NCurses.attrs.BOLD,String(i)]);;
232
gap> NCurses.Pager(lines);
233

234
235
236
3.1-5 Selection of help matches
237
238
After loading the Browse package GAP's help system behaves slightly
239
different when a request yields several matches. The matches are shown via
240
NCurses.Select (3.1-2), the list can be searched and filtered, and one can
241
choose one match for immediate display. It is possible to not choose a match
242
and the ?<nr> syntax still works.
243
244
If you want the original behavior call SetUserPreference( "Browse",
245
"SelectHelpMatches", false ); in your GAP session or gap.ini file,
246
see 'Reference: Configuring User preferences'.
247
248
249
3.1-6 Selection of package names
250
251
The function LoadPackage (Reference: LoadPackage) shows a list of matches if
252
only a prefix of a package name is given. After loading the Browse package,
253
NCurses.Select (3.1-2) is used for that, and one can choose a match.
254
255
If you want the original behavior call SetUserPreference( "Browse",
256
"SelectPackageName", false ); in your GAP session or gap.ini file,
257
see 'Reference: Configuring User preferences'.
258
259
260
3.2 A Demo Function
261
262
3.2-1 NCurses.Demo
263
264
NCurses.Demo( [inputs] )  function
265
266
Let inputs be a list of records, each with the components title (a string),
267
inputblocks (a list of strings, each describing some GAP statements), and
268
optionally footer (a string) and cleanup (a string describing GAP
269
statements). The default is NCurses.DemoDefaults.
270
271
NCurses.Demo lets the user choose an entry from inputs, via NCurses.Select
272
(3.1-2), and then executes the GAP statements in the first entry of the
273
inputblocks list of this entry; these strings, together with the values of
274
title and footer, are shown in a window, at the bottom of the screen. The
275
effects of calls to functions using ncurses are shown in the rest of the
276
screen. After the execution of the statements (which may require user
277
input), the user can continue with the next entry of inputblocks, or return
278
to the inputs selection (and thus cancel the current inputs entry), or
279
return to the execution of the beginning of the current inputs entry. At the
280
end of the current entry of inputs, the user returns to the inputs
281
selection.
282
283
The GAP statements in the cleanup component, if available, are executed
284
whenever the user does not continue; this is needed for deleting panels and
285
windows that are defined in the statements of the current entry.
286
287
Note that the GAP statements are executed in the global scope, that is, they
288
have the same effect as if they would be entered at the GAP prompt.
289
Initially, NCurses.Demo sets the value of
290
BrowseData.defaults.work.windowParameters to the parameters that describe
291
the part of the screen above the window that shows the inputs; so
292
applications of NCurses.BrowseGeneric (4.3-1) use automatically the maximal
293
part of the screen as their window. It is recommended to use a screen with
294
at least 80 columns and at least 37 rows.
295
296
297