[comp.lang.icon] library procedures

corre@csd4.csd.uwm.edu (Alan D Corre) (12/13/89)

Chris's useful little input() procedure is a good candidate for a library
module. Such modules are very easy to use in v.7 of Icon by compiling with
the -c switch and using the link declaration

This prompts me to publish a few useful procedures. The version here is for
the z-29 terminal, but you can modify it fairly easily. I also have a
version for the pc which I can post if someone wants it. I use ANSI mode on
the z-29 for a good reason. If you are in ANSI mode already, and you writes
the command to switch from ZENITH mode, it is simply ignored, so which ever
of the two modes you are in will be fine. Not so if you give the ANSI
command to switch to ZENITH mode and you are already in ZENITH! On my
installation the system hangs irretrievably, and if you already waited 20
minutes to get on that is pretty frustrating! I dont know why that occurs,
but my experience with computers indicates that if something blows up, dont
push the button again. Here is the documentation, followed by the code.

****
Some of these procedures assume the use of a terminal in ANSI mode. If
your terminal is in Zenith mode, it will be switched to ANSI mode and
left in that mode. It can be returned to Zenith mode by the use of the
setup key.

procedure banner(s1,s2..sn)
Takes an arbitrary number of strings and places them in a display box on the
screen. Strings should be a maximum of 78 characters.

procedure instructions(filename)
Asks the user if instructions are desired. If so, opens the file "filename"
and reads it by screenfuls until ended or user wishes to stop. User is told
if file is unavailable.

procedure randomize()
Sets the random seed to a number determined by the clock time. The seed may
be reset to initial value by the statement
&random := 0

procedure clear()
Clears the screen and puts the cursor in the home position.

procedure gotoxy(l,r)
Sets the cursor at line l, row r.


***
procedure banner(l[])
local n
  writes("\e<") #go into ansi mode
  writes("\e[10m") #graphics mode
  write();write();write()
  writes(char(102)) #top left right angle
  writes(repl(char(97),78)) #straight line
  writes(char(99)) #top right right angle
  writes(char(96)) #upright line at left
  writes(right(char(96),79)) #upright line at right
  every n := 1 to *l do {
    writes(char(96)) #upright line at left
    writes("\e[11m",center(l[n],78),"\e[10m",char(96)) #string centered followed by upright line, exit graphics mode for text
    writes(char(96)) #upright line at left
    writes(right(char(96),79)) #upright line at right
}
  writes(char(101)) #bottom left right angle
  writes(repl(char(97),78)) #straight line
  write(char(100)) #bottom right right angle
  write("\e[11m")
  write("Press return to continue.")
  read()
return
end


procedure instructions(filename)
local filvar,counter,line
  writes("Do you need instructions? y/n ")
  if upto('yY',read()) then {
#The following if-statement fails if the file is not available
  counter := 0
  if filvar := open(filename) then
#Read the help file. It's a good idea to keep this kind of info in a
#separate file because you can modify it easily without redoing the
#program. In general---keep data out of programs!
    while line := read(filvar) do {
#Write out a line and increment the counter
      write(line)
      counter +:= 1
#Now we have a screenful; ask if we should continue
      if counter >22 then {
        write()
        writes ("More? y/n ")
#User has had enough; break out of loop
        if upto('nN',read()) then break  else
#User wants more; reset counter and continue
          counter := 0}} else
#This else goes with the second if-statement; the attempt to open the
#help file failed:
      write("Sorry, instructions not available.")}
    write ("Press return to continue.")
    read()
#Close the file if it existed and was opened. If it was never opened
#the value of filvar will be null. This check has to be made because
#an attempt to use close() on a variable NOT valued at a file would
#cause an error. See Psalm 130.3
/filvar | close(filvar)
end

procedure randomize()
#extracts the seconds from  clock time, and uses the resulting number as seed
local clock
  clock := &clock
  clock[1:7] := ""
  &random := clock
end

procedure clear()
#clears the screen
  writes("\e<\e[2J")
end

procedure gotoxy(l,r)
#sets the cursor at line l, row r
  writes("\e<\e[",l,";",r,"H")
end
--
Alan D. Corre
Department of Hebrew Studies
University of Wisconsin-Milwaukee                     (414) 229-4245
PO Box 413, Milwaukee, WI 53201               corre@csd4.csd.uwm.edu