|
Making Console Applications: Prompts, Input Options, and Menus |
A console application talks to a person through the screen: it prints,
it asks, and it offers menus. The basics of asking are on
Asking the user for Information (INPUT) — this page is the reference for everything
around them: the three input statements and every option they take, and
the menu system with its full set of % directives.
Each section opens with the idea; click a Show me line for the details and examples, or Expand all.
input reads one or more values and stops at a comma or a
space, line input reads the whole line, and
key input reads a single keystroke without waiting for
Enter. All three take a prompt. A prompt written as a plain quoted string
gets a question mark appended — the classic BASIC convention —
while the explicit prompt option is shown exactly as written.
Three lessons in one run: the short form appended ? to
'Your name: ', the prompt form did not, and a
line input with no prompt shows nothing at all. Use the
prompt form whenever the exact text matters.
key input returns as soon as one key is pressed; the value is
that single character. It is the statement for "press any key" pauses and
single-letter choices.
Options follow the prompt, comma-separated, and the list ends with a colon
before the variables: line input 'Prompt', option, option: var.
They control where the prompt appears, how long the program waits, what is
accepted, and what happens at the end of input.
| Option | Argument | What it does |
|---|---|---|
prompt | string | The prompt, shown exactly as written (no question mark added). |
default | string | Text already in the field when the prompt appears; the person can accept it with Enter or type over it. For a menu, the path of the item to highlight (section 3). |
length | number | The width of the input field on the screen. |
valid | string | A validation rule the answer must pass before it is accepted; a failing answer is refused and the prompt repeats. Rule words include integer, number, digits, letters, date, required, ucase, lcase, minlength n, maxlength n, pattern, yes/no, and the date layouts ymd, mdy, dmy. |
timeout | number | Seconds to wait for an answer before the statement gives up. |
elapsed | numeric variable | Receives the seconds the person took to answer (a real gets hundredths, an integer is rounded). |
eof | boolean variable | Set to true when there is no more input (a closed pipe, Ctrl+Z) instead of raising an error. |
at | row, column | Where the prompt appears on the screen, 1-origin. |
area | top, left, bottom, right | A rectangle for multi-line text entry. |
erase | — | Erases the input area first. |
attributes | string | Display attributes for the field, such as 'bold' or 'reverse'. |
menu | string | The menu definition — the whole of section 3 onward. |
screen | string | A fill-in form definition: several fields on one screen. |
dialogbox | string | A dialog box definition; implies line input. |
id | string | An identifier for the field, used by screen forms. |
The options a program most often reaches for are timeout with
elapsed, and eof:
Without eof, reading past the end of input is an error;
with it, the variable comes back empty and the flag says why. That is the
idiom for a program that reads until its input runs out.
at places the prompt anywhere on the screen. This is the
screen image captured while the program waited, with the prompt at row 5,
column 10:
A menu is a string. Items are separated by commas, an item can carry a
value, an item can open a submenu, and % directives set the
title, the position, the layout and the behaviour. The statement is
line input menu spec$: choice$; the person moves with the
arrow keys and picks with Enter, Space or a click.
The first item is highlighted when the menu opens. %bar drew
the rule, and the > marks tropical as a
submenu — choosing it opens the second menu:
Choosing papaya closes both menus and the program continues:
apples becomes APPLES); a quoted item keeps
its case exactly — "Apples".text = value shows the text and returns the value:text = { items } is a submenu, nested as deeply as you
like. A submenu with no %title of its own is titled with
the item's text, as TROPICAL was above._string$ receives the path of the choice through the
levels: #3;#2 means the third item of the top menu, then
the second item of its submenu. Rules and headings are not counted._exit to true
— a console application should test that before trusting the
result:default option names the item to highlight when the
menu opens, by its path — the same form _string$
returns: default '#2' highlights the second choosable
item, default '#3;#2' opens the third item's submenu on
its second item. Keeping the last _string$ and passing it
back as the default reopens a menu on the person's previous choice.
A default that is not a path raises the illnum
exception.timeout and elapsed work with a menu exactly
as with a prompt.input menu spec$: choice$ is the same statement as
line input menu._exit.
A directive is a word starting with %, written among the
items. There are twenty-four; they fall into three groups — where and
how the menu is laid out, what appears in it besides the items, and how it
behaves. Here is a menu using several at once, captured while it waited:
%at 3,5 put the top-left corner at row 3, column 5;
%split started the second column; each %heading
labelled its column; %invalid peas shows an item that cannot be
chosen — the path #1 counts only choosable items.
| Directive | Meaning |
|---|---|
%at row, col | Position of the menu; either may be center, as in %at center, center. |
%split | Start a new column here. |
%columns n | Split the items evenly into n columns. |
%items n | At most n items per column before a new column starts. |
%size n | Rows visible in the current column (at least 2); more items scroll. |
%width n | Minimum width of the current column. |
%maxwidth n | Maximum width of the whole menu (at least 5). |
%vbar | A vertical bar after the current column. |
%autovbar on|off | Automatic bars between columns (on by default). |
%lockstep on|off | Columns scroll together (on by default). |
%left | Entries flush left in their column. |
%border on|off | Draw the frame (on by default). |
%menubar | A horizontal menu bar with pull-down submenus, one row per column. |
| Directive | Meaning |
|---|---|
%title "text" | The title. |
%message "text" | A message shown while the menu is up. |
%bar, %bar 'text' | A rule across the column, plain or carrying text. |
%heading "text" | A labelled separator, in the heading colour. |
%invalid item | The next item is shown but cannot be chosen. |
| Directive | Meaning |
|---|---|
%multi | Choose several items — section 5. |
%replace | A submenu that replaces its parent instead of opening beside it; its Back item returns. |
%autodisplay on|off | Submenus open as soon as their item is highlighted (on by default). |
%nomouseover | No highlighting under the mouse pointer. Top-level menu only. |
%inactive | The menu is shown as a normal window that others may overlap. Top-level only. |
%attached | The menu minimizes and restores with the console window. Top-level only. |
Invalid input menu format. When the reason is known it is in
_string$ — see the %multi rule below.
Each picture is the screen captured while the menu waited, with the part of
the menu string that made the difference. Six directives have no picture
because they change behaviour rather than appearance: %message
(a status line while the menu is up), %autodisplay,
%lockstep, %nomouseover, %inactive and
%attached. %title is on every picture, and
%heading, %invalid and %split are in
the Layout example above.
The parent, then the screen after choosing reports: the
submenu is where the parent was, not beside it. Its Back item returns.
%multi turns the menu into a checklist with Done, Back and
Exit buttons. Space toggles an item; Done returns every checked item, one
per line.
The checked items come back separated by line breaks, so
pieces() counts them and piece$() takes them one at
a time. The rule: a %multi menu cannot contain submenus. The
menu string is refused when it is parsed, and the exception says why:
When one question is not enough and you want a whole form at once —
several fields, a checkbox or two, a dropdown — reach for a
dialog box: a native window driven by the same
line input family, which also opens the native file and folder
pickers. See Dialog Boxes: Forms, Fields, and Native File Pickers and its companion reference
Dialog Box Reference: Every Tag and Attribute.
input stops at a comma or space, line input
takes the whole line, key input takes one keystroke.? appended; the
prompt option shows the text exactly as written.: — timeout with elapsed,
eof, at, default,
valid, length, area.text = value,
text = { submenu }, and % directives for
title, position, columns and behaviour._string$,
and _exit when the person pressed Ctrl+Z; Escape returns
an empty result.%multi returns every checked item, one per line, and
cannot contain submenus.Invalid input menu format,
with the reason in _string$ when it is known.|
Hide Description
|
|
|
Enter or modify the code below, and then click on RUN |
|
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use. |