1. Input Routines
My take on input routines. Nothing special; the only claim to fame is
clearing the input field after an error. Not having to type over a
previous mistake is less confusing, at least for me.
------------------------------------ cut here ------------------
-- Input Routines
-- Warren Evans
-- Begin input.e
include graphics.e
include get.e
global function input(sequence prompt) -- Print prompt, get a sequence
-- Use: sequence=input("Put your prompt here: ")
sequence buffer puts(1,prompt) buffer=gets(0)
return buffer[1..length(buffer)-1] -- Remove trailing newline
end function
global function inputx(sequence prompt) -- Print prompt, get a number
-- Returns an atom or a one-element sequence
-- Accepts numbers only; clears input field on an error
-- use: sequence=inputx("Put your prompt here: ")
-- or atom=inputx("Put your prompt here: ")
sequence pos,buffer
object x
buffer={1} -- Set to dummy value
x={1,0} -- Set to error value for while loop
-- The x=value(string) function converts a sequence to a number.
-- x[1] holds error status. x[2] holds the value.
-- x must be a sequence, not an atom.
puts(1,prompt) -- Show the prompt
pos=get_position() -- Remember reply position
while atom(x) or x[1]!=GET_SUCCESS do -- Repeat while invalid number
position(pos[1],pos[2]) -- Set cursor to input position
for i=1 to length(buffer) do -- Clear input field
puts(1," ")
end for
position(pos[1],pos[2]) -- Set cursor to input position
buffer=gets(0) -- Accept keystrokes
x=value(buffer) -- Convert input to number
end while
return x[2] -- Valid input back to calling routine
end function
-- end input.e
-- Test of input functions
-- include input.e
sequence line, lineout
object x, count
line="Typical prompt" x=1 count=0
-- Test regular alphanumeric input
clear_screen()
puts(1,"Input some strings (sequences); enter 'quit' to quit.\n\n")
while compare(line,"quit")!=0 and count<10 do
line=input("Type something: ")
lineout="\nYou entered: >> " & line & " <<\n\n"
puts(1,lineout)
count=count+1
end while
puts(1,"\n\nNow try entering numbers; '99' to quit.\n")
puts(1,"Note that <Enter> with no other keystrokes is invalid.\n\n")
count=0
-- Test numeric input
while x!=99 and count<10 do
x=inputx("Enter a number: ")
puts(1,"\nYou entered: >> ") print(1,x) puts(1," <<\n\n")
count=count+1
end while
puts(1,"End of input routines test..\n\n")
-- End input routines test