[comp.lang.prolog] screen control

dbrod@mtfmi.UUCP (D.BRODERICK) (05/27/88)

If your version of Prolog does not have screen control predicates,
but you are familiar with the Unix(tm) tput(1) command 
(or willing to become so), there is a quick and dirty way to 
generate some character based screen control predicates.  
tput(1) makes use of the terminfo database to generate terminal 
specific escape sequences to control the screen.  For example, 
the following, invoked from the shell, will print out "hello" 
in standout mode:

tput smso; echo hello; tput rmso

As you can see, the argument names are intuitively obvious(?).
To use this information from Prolog, write a shell script 
called "get_tput" as follows:
--------------------------------------
echo "tput('$2',$1,'`tput -T$1 $2`')."
--------------------------------------
This is invoked, for example, as: 
	get_tput vt100 clear >> tput.pl
It is easy enough to use a for-loop to generate a prolog 
terminfo database for all the tput arguments/terminals you want.
For an interesting sight, cat the file. ("tput sgr0" sets to normal)
To use on a PC running an ansi.sys driver, generate a database 
for ansi and download.
To use: 
	consult('tput.pl'). 	and call 
	tput(clear).		, defined as:

tput(Cmd) :- tput(Cmd,_,EscSeq), atomic(EscSeq), write(EscSeq).
tput(Cmd) :- tput(Cmd,_,[Esc|Seq]), print_list([Esc|Seq]).
tput(Cmd) :- not tput(Cmd,_,_).

This assumes you have loaded info only for your current terminal.
The reason for the middle clause is that shell tput commands that
take more than one parameter return a sequence that needs further
parsing.  I have not written a general parser, but have done some
of these by hand.  In what follows, what looks like ^ followed by [ 
is actually a real Escape char (in case you retype this).

% cup - set cursor position.  this works for vt100 and ansi
tput(cup(Row,Col),att5425,['[',R1,';',C1,'H']) :- R1 is Row+1, C1 is Col+1.

% csr - change scroll region
tput(csr(Top,Bottom),att5425,['[',T1,';',B1,r]) :- 
	T1 is Top+1, B1 is Bottom+1.

% pln - set system function key  Not on many terminals
tput(pln(Key,Label,Command),att5425,
		['[',Key,';',Len,';0;0q',Label16,Command]) :-
	name(Command,CAscii),
	length(CAscii,Len),
	pad_atom(16,Label,Label16).

tput(user,att5425,'}'). % switch to user function keys
tput(system,att5425,'~'). % switch to system function keys

% tsl - write to status line
tput(tsl(Col),att5425,['7[25;',Col1,'H']) :- Col1 is Col + 8.

% auxiliary predicates

print_list([]).
print_list([X|Xs]) :- write(X), print_list(Xs).

pad_atom(Num,Atom,Padded) :-
	name(Atom,AAscii),
	length(AAscii,Len),
	Pads is Num - Len,
	pad_blanks(Pads,BString),
	append(AAscii,BString,PAscii),
	name(Padded,PAscii).

pad_blanks(0,[]).
pad_blanks(Num,[32|Blanks]) :-
	Num > 0,
	Num1 is Num-1,
	pad_blanks(Num1,Blanks).

% append/3 as usual

ok@quintus.UUCP (Richard A. O'Keefe) (05/29/88)

In article <701@mtfmi.UUCP>, dbrod@mtfmi.UUCP (D.BRODERICK) writes:
> If your version of Prolog does not have screen control predicates,
> but you are familiar with the Unix(tm) tput(1) command 

It's worth pointing out that
(a) tput is a System V feature, not present in most BSDs (but SunOS has it).
(b) There are some non-trivial differences between V.2 tput and V.3 tput
    (as I found the hard way when some scripts I wrote on a V.3 system
    didn't work on a V.2 system.)
(c) Some of the things tput returns are numbers, for example
	tput lines
    prints the number of lines on the screen.  Suppose that is 24.
    The "get_tput" script provided by D.BRODERICK will write this as '24',
    which is an atom.  You may want to have another script which writes
    things unquoted so that you can access such terminal properties.
(d) Some of the things tput reports are "boolean", for example
	tput hc		# is it a hard-copy terminal?
    always prints nothing.  Instead the answer is to be found in the exit
    code (0 means yes, non-zero means no).  You may want a third script
	if tput -T$1 $2 ; then
	    echo "tput($2, '$1', true)."
	else
	    echo "tput($2, '$1', false)."
	fi
    for use with boolean capabilities.
(e) tput writes things out verbatim, without escape characters.  Some
    Prolog systems may discard CRs or do other odd things with strange
    characters.  (Quintus Prolog is safe, but watch out for terminal
    capabilities containing apostrophes.)  

If the Prolog dialect you are using has an interface to C (as many have)
you would be better off writing an interface to 'curses'; since 'curses'
is available for IBM PCs and VAX/VMS as well as for UNIX this may be more
portable than using tput.  In particular, if you use curses, you don't
have to figure out how to parse the 'cup' capability (rather hairy).
Then too, I for one would rather hide the rather bizarre capability
names (quickly now:  what does eslok do?)  from my Prolog code.