This file is indexed.

/usr/lib/swi-prolog/library/tty.pl is in swi-prolog-nox 5.10.4-3ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*  $Id$

    Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        jan@swi.psy.uva.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 1985-2002, University of Amsterdam

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

:- module(tty,
	[ tty_clear/0
	, tty_flash/0
	, menu/3
	]).
:- use_module(library(lists), [append/3, nth1/3]).

/** <module> Terminal operations

This library package defines some common operations on terminals. It is
based on the Unix termcap facility to perform terminal independant I/O
on video displays. The package consists of three sections:

  1. Predicates to perform simple operations on terminals
  2. Extenstions to format/2 to include cursor position and clearing
     sections of the screen.
  3. A generic predicate to build simple menus.

@bug	The stream information on the terminal related  streams
	is not maintained by these predicates.
*/

%%	tty_clear
%
%	Clear the display.

tty_clear :-
	string_action(cl).

%%	tty_flash
%
%	Give visual signal if possible, otherwise beep.

tty_flash :-
	tty_get_capability(vb, string, Vb), !,
	tty_put(Vb, 1).
tty_flash :-
	put(7).

%%	string_action(+Name)
%
%	Send string from the termcap library with specified name.

string_action(Name) :-
	tty_get_capability(Name, string, String),
	tty_put(String, 1).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
				 FORMAT

The functions below add some extras to the format facilities.  This to
simplify screen management.  It adds ~T to the set of format characters.
The argument to ~T is a (list of) tty control commands.  The ~l command
is defined to clear to the end of the line before generating a newline.

Example:

?- format('~T~3l', home),
   format('    1) Hello World~l'),
   format('    2) Exit~2l'),
   format('    Your choice? ~T', [clear_display, flush]),
   get_single_char(X).
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

:- format_predicate('T', tty_action(_Arg, _What)).
:- format_predicate('l', tty_nl(_Args)).

tty_action(_, What) :-
	tty_action(What).

tty_action([]) :- !.
tty_action([A|B]) :- !,
	tty_action(A),
	tty_action(B).
tty_action(goto(X,Y)) :- !,
	tty_goto(X, Y).
tty_action(home) :- !,
	tty_goto(0, 0).
tty_action(flush) :- !,
	ttyflush.
tty_action(center(Text)) :- !,
	tty_size(W, _),
	format('~t~a~t~*|', [Text, W]).
tty_action(back(N)) :- !,
	forall(between(1, N, _), put_code(8)).
tty_action(Long) :-
	abbreviation(Long, Short), !,
	string_action(Short).
tty_action(Short) :-
	string_action(Short).

abbreviation(clear,		cl).		% clear and home
abbreviation(clear_line,	ce).		% clear-to-end-of-line
abbreviation(clear_display,	cd).		% clear-to-end-of-display

tty_nl(default) :- !,
	tty_nl(1).
tty_nl(N) :-
	tty_get_capability(ce, string, Ce),
	forall(between(1, N, _),
	       (   tty_put(Ce, 1),
		   nl)).


		 /*******************************
		 *	       MENU		*
		 *******************************/

%%	menu(+Title, +Options, -Choice) is semidet.
%
%	Show a menu. The display is cleared,   the  title is centered at
%	the top, the options are displayed  and finally the user actions
%	are parsed and the user's choice   is returned. The screen looks
%	like this:
%
%	==
%    		--------------------------------------------
%		|                                          |
%		|                  Title	           |
%	    	|					   |
%		|   1) Option One                          |
%		|   2) Option Two			   |
%		|   3) Quit				   |
%		|					   |
%		|   Your Choice? *			   |
%		|					   |
%	==
%
%	The user selects an item by pressing the number of the item, or
%	the first letter of the option. If more then one option match,
%	the common prefix of the matching options is given and the user
%	is expected to type the next character.  On illegal input the
%	screen is flashed (or a beep is given if the terminal can't flash
%	the screen).
%
%	Text fields (the title and option texts) are either plain atoms
%	or terms Fmt/Args.  In the latter case the argument is transformed
%	into an atom using format/3.
%
%	The specification of an option is a term PrologName:UserName.
%	PrologName is an atom, which is returned as choice if the user
%	selects this menu item.  UserName is processed as a text field
%	(see above) and displayed.  The entries are numbered automatically.
%
%	The example above could be defined as:
%
%	==
%	get_action(Choice) :-
%		menu('Title',
%			[ option_1 : 'Option One'
%			, option_2 : 'Option Two'
%			, quit     : 'Quit'
%			], Choice).
% 	==


menu(Title, List, Choice) :-
	show_title(Title),
	build_menu(List),
	get_answer(List, Choice).

show_title(Title) :-
	to_text(Title, T),
	format('~T~l~T~2l', [clear, center(T)]).

build_menu(List) :-
	build_menu(List, 1),
	format('~2n      Your choice? ~T', clear_display).

build_menu([], _).
build_menu([_:H|T], N) :-
	to_text(H, TH),
	format('~t~d~6|) ~a~l', [N, TH]),
	succ(N, NN),
	build_menu(T, NN).

to_text(Fmt/Args, Text) :- !,
	format(string(Text), Fmt, Args).
to_text(Text, Text).

:- dynamic
	menu_indent/1.

menu_indent(Old, New) :-
	(   retract(menu_indent(Old0))
	->  Old = Old0
	;   Old = 0
	),
	assert(menu_indent(New)).

get_answer(List, Choice) :-
	menu_indent(_, 0),
	get_answer(List, [], Choice).

get_answer(List, Prefix, Choice) :-
	get_single_char(A),
	process_answer(A, List, Prefix, NewPrefix, Ch, Ok),
	(   Ok == yes
	->  Ch = Choice
	;   get_answer(List, NewPrefix, Choice)
	).

process_answer(127, _, _, [], _, no) :- !,
	feedback('').
process_answer(D, List, _, _, Choice, yes) :-
	code_type(D, digit),
	name(N, [D]),
	nth1(N, List, Choice:Name), !,
	feedback(Name).
process_answer(D, _, _, [], _, no) :-
	code_type(D, digit),
	feedback(''),
	tty_flash.
process_answer(C, List, Prefix, NewPrefix, Choice, Ok) :-
	append(Prefix, [C], NPrefix),
	matching(List, NPrefix, Matching),
	(   Matching == []
	->  tty_flash,
	    NewPrefix = Prefix,
	    Ok = no
	;   Matching = [Choice:Name]
	->  Ok = yes,
	    feedback(Name)
	;   common_prefix(Matching, NewPrefix),
	    feedback(NewPrefix),
	    Ok = no
	).

matching([], _, []).
matching([H|T], Prefix, [H|R]) :-
	prefix(Prefix, H), !,
	matching(T, Prefix, R).
matching([_|T], Prefix, R) :-
	matching(T, Prefix, R).

prefix(Prefix, _:Name) :-
	name(Name, Chars),
	common_prefix_strings(Prefix, Chars, Prefix), !.

common_prefix([_:Name|T], Prefix) :-
	name(Name, Chars),
	common_prefix(T, Chars, Prefix).

common_prefix([], Prefix, Prefix).
common_prefix([_:Name|T], Sofar, Prefix) :-
	name(Name, Chars),
	common_prefix_strings(Chars, Sofar, NewSofar),
	common_prefix(T, NewSofar, Prefix).

common_prefix_strings([H1|T1], [H2|T2], [H1|R]) :-
	code_type(Lower, to_lower(H1)),
	code_type(Lower, to_lower(H2)), !,
	common_prefix_strings(T1, T2, R).
common_prefix_strings(_, _, []).

feedback(Text) :-
	atomic(Text), !,
	atom_length(Text, New),
	menu_indent(Old, New),
	format('~T~a~T', [back(Old), Text, clear_line]).
feedback(Text) :-
	length(Text, New),
	menu_indent(Old, New),
	format('~T~s~T', [back(Old), Text, clear_line]).