file: 10-C-utils.t package: muf status: alpha
This function prompts the user to select one of a given set (block) of strings. It loops until the user enters the number of a valid string, then returns both the selected string and its offset within the block (zero to n-1).
Note that it may easily be adapted to select from a set of nonstring values:
[ "Pat" "Kim" | "Pick mate" ]queryForChoice pop -> choice [ .u.pat .u.kim | choice dupNth -> choice ]pop choice
will return one of .u.pat
or .u.kim
.
It is currently implemented as:
: ]queryForChoice { [] $ -> $ $ } -> prompt |length -> len do{ prompt length 0 = if "\nPick one:\n" @$s.queryOutput writeStream "---------\n" @$s.queryOutput writeStream else [ "\n%s:\n" prompt | ]print @$s.queryOutput writeStream fi 0 -> i |for choice do{ i 1 + -> i [ "%d) %s\n" i choice | ]print @$s.queryOutput writeStream } @$s.queryInput readStreamLine pop trimString -> string string stringInt 1 - -> result result 0 <= if "Sorry, choice must be at least 1\n" @$s.queryOutput writeStream continue fi result len >= if [ "Sorry, choice must be at most %d\n" len | ]print @$s.queryOutput writeStream continue fi result |dupNth -> resultString ]pop result resultString return } ; ']queryForChoice export
Go to the first, previous, next, last section, table of contents.