Skip to content

flap_menu_t ​

Interactive menus for terminal programs (F23 of #125, the wmenu plan of #78): numbered options, one answer line.

Opt-in: the argument parser never uses this module, so parsing stays non-interactive and safe in batch and MPI jobs. A menu prints its options numbered from 1, then the question, reads one answer line and returns the chosen index (the indexes, with multiple selection); the caller dispatches with select case. The units are the caller's: the menu never opens nor closes them. At the end of the input (standard input redirected from /dev/null or closed, as in a batch job) run returns ERROR_MENU_EOF at once: standard Fortran cannot tell whether the input is a terminal, the end of file is the portable signal. yes_no asks the question alone (no options) and returns a logical. Colours and styles (FACE names, none by default) apply to the option lines, the question and the word "error" of the messages.

Source: src/lib/flap_menu_t.F90

Dependencies

Contents ​

Variables ​

NameTypeAttributesDescription
ERROR_MENU_INVALIDinteger(kind=I4P)parameterNot a number, out of range, or an empty field.
ERROR_MENU_TOO_MANYinteger(kind=I4P)parameterSeveral answers to a single choice.
ERROR_MENU_DUPLICATEinteger(kind=I4P)parameterThe same option chosen twice.
ERROR_MENU_NO_RESPONSEinteger(kind=I4P)parameterEmpty answer, and no default option.
ERROR_MENU_EOFinteger(kind=I4P)parameterEnd of input: no answer can come.
ERROR_MENU_DEFINITIONinteger(kind=I4P)parameterInvalid menu or use (no options, empty text, ...).

Derived Types ​

An option of a menu.

Components ​

NameTypeAttributesDescription
textcharacter(len=:)allocatableText shown.
is_defaultlogicalChosen by an empty answer.

Interactive menu: numbered options, a question, one answer line.

Components ​

NameTypeAttributesDescription
questioncharacter(len=:)allocatableQuestion asked after the options.
optionstype(menu_option)allocatableOptions; the index is the number shown.
default_iconcharacter(len=:)allocatableMark of the default options.
input_unitinteger(kind=I4P)Unit of the answers.
output_unitinteger(kind=I4P)Unit of the options and the question.
error_unitinteger(kind=I4P)Unit of the error messages.
loop_on_invalidlogicalAsk again after an invalid answer.
triesinteger(kind=I4P)Attempts in total with loop_on_invalid.
multiplelogicalSeveral options can be chosen.
separatorcharacter(len=:)allocatableSeparator of the answers (blank: runs of blanks).
option_colorcharacter(len=:)allocatableANSI colour of the option lines (FACE names).
option_stylecharacter(len=:)allocatableANSI style of the option lines.
question_colorcharacter(len=:)allocatableANSI colour of the question.
question_stylecharacter(len=:)allocatableANSI style of the question.
error_colorcharacter(len=:)allocatableANSI colour of the word "error" of the messages.
error_stylecharacter(len=:)allocatableANSI style of the word "error" of the messages.

Type-Bound Procedures ​

NameAttributesDescription
add_optionpass(self)Append an option.
freepass(self)Free dynamic memory.
initpass(self)Initialize the menu.
runShow the menu and read the answer.
yes_nopass(self)Ask the question as a yes/no one.
askpass(self)Show the menu and read the chosen indexes.
default_indexpass(self)Index of the (first) default option.
evaluatepass(self)The indexes of an answer.
raisepass(self)Write an error message and return its code.
run_multiplepass(self)Show the menu and read the choices.
run_singlepass(self)Show the menu and read one choice.
showpass(self)Write the options (unless yes/no) and the question.

Subroutines ​

init ​

Initialize the menu: every previous setting and option is dropped.

fortran
subroutine init(self, question, multiple, separator, loop_on_invalid, tries, default_icon, input_unit, output_unit, error_unit, option_color, option_style, question_color, question_style, error_color, error_style, error)

Arguments

NameTypeIntentAttributesDescription
selfclass(menu)inoutMenu.
questioncharacter(len=*)inQuestion asked after the options.
multiplelogicalinoptionalSeveral options can be chosen (default: no).
separatorcharacter(len=*)inoptionalSeparator of the answers (default: blank, runs of blanks).
loop_on_invalidlogicalinoptionalAsk again after an invalid answer (default: no).
triesinteger(kind=I4P)inoptionalAttempts in total with loop_on_invalid (default: 3).
default_iconcharacter(len=*)inoptionalMark of the default options (default: '*').
input_unitinteger(kind=I4P)inoptionalUnit of the answers (default: standard input).
output_unitinteger(kind=I4P)inoptionalUnit of the options and the question (default: standard output).
error_unitinteger(kind=I4P)inoptionalUnit of the error messages (default: standard error).
option_colorcharacter(len=*)inoptionalANSI colour of the option lines (FACE names; default: none).
option_stylecharacter(len=*)inoptionalANSI style of the option lines.
question_colorcharacter(len=*)inoptionalANSI colour of the question.
question_stylecharacter(len=*)inoptionalANSI style of the question.
error_colorcharacter(len=*)inoptionalANSI colour of the word "error" of the messages.
error_stylecharacter(len=*)inoptionalANSI style of the word "error" of the messages.
errorinteger(kind=I4P)outoptionalError trapping flag.

Call graph

add_option ​

Append an option: its index is the number shown. An empty (or blank) text, or a second default, is not added.

fortran
subroutine add_option(self, text, is_default, error)

Arguments

NameTypeIntentAttributesDescription
selfclass(menu)inoutMenu.
textcharacter(len=*)inText shown.
is_defaultlogicalinoptionalChosen by an empty answer (at most one option).
errorinteger(kind=I4P)outoptionalError trapping flag.

Call graph

free ​

Free dynamic memory and restore the default units.

Attributes: elemental

fortran
subroutine free(self)

Arguments

NameTypeIntentAttributesDescription
selfclass(menu)inoutMenu.

ask ​

Show the menu and read the chosen indexes (one without multiple selection); none on error.

With loop_on_invalid an invalid (or empty) answer is reported with the tries left and the menu is asked again, up to tries attempts; the last error is returned. The end of the input and a read error are never retried. With yes_no (its default: 'Y', 'N', or ' ' for none) the question is asked alone and the index is 1 for yes, 2 for no.

fortran
subroutine ask(self, choices, error, yes_no)

Arguments

NameTypeIntentAttributesDescription
selfclass(menu)inoutMenu.
choicesinteger(kind=I4P)outallocatableChosen indexes (none on error).
errorinteger(kind=I4P)outError trapping flag.
yes_nocharacter(len=1)inoptionalYes/no question, with its default.

Call graph

run_multiple ​

Show the menu and read the choices: the indexes of the chosen options in the order typed, none on error.

On a single-choice menu it returns one index (several answers are ERROR_MENU_TOO_MANY).

fortran
subroutine run_multiple(self, choices, error)

Arguments

NameTypeIntentAttributesDescription
selfclass(menu)inoutMenu.
choicesinteger(kind=I4P)outallocatableChosen indexes (none on error).
errorinteger(kind=I4P)outoptionalError trapping flag.

Call graph

run_single ​

Show the menu and read one choice: the index of the chosen option, 0 on error.

fortran
subroutine run_single(self, choice, error)

Arguments

NameTypeIntentAttributesDescription
selfclass(menu)inoutMenu.
choiceinteger(kind=I4P)outChosen index (0 on error).
errorinteger(kind=I4P)outoptionalError trapping flag.

Call graph

show ​

Write the numbered options, then the question on the line of the answer; with a suffix (yes/no), the question alone.

fortran
subroutine show(self, suffix)

Arguments

NameTypeIntentAttributesDescription
selfclass(menu)inMenu.
suffixcharacter(len=*)inoptionalSuffix of the question, the options not shown.

Call graph

yes_no ​

Ask the question as a yes/no one, without the options: y, yes, n, no in any case; an empty answer is the default.

The prompt ends with (Y/n), (y/N) or (y/n) (no default); retries and the end of the input as run.

fortran
subroutine yes_no(self, answer, default, error)

Arguments

NameTypeIntentAttributesDescription
selfclass(menu)inoutMenu.
answerlogicaloutAnswer (.false. on error).
defaultcharacter(len=*)inoptionalDefault answer: 'y' or 'n' (any case); none if absent.
errorinteger(kind=I4P)outoptionalError trapping flag.

Call graph

evaluate ​

The indexes of an answer (no blanks around): the defaults for an empty one; none on error, with its message.

Checked in order: several fields without multiple selection (too many), each field a number shown (invalid, also an empty field), an index repeated (duplicate).

Attributes: pure

fortran
subroutine evaluate(self, answer, choices, error, message)

Arguments

NameTypeIntentAttributesDescription
selfclass(menu)inMenu.
answercharacter(len=*)inAnswer.
choicesinteger(kind=I4P)outallocatableChosen indexes.
errorinteger(kind=I4P)outError code.
messagecharacter(len=:)outallocatableError message.

Call graph

finalize ​

Free dynamic memory when finalizing.

Attributes: elemental

fortran
subroutine finalize(self)

Arguments

NameTypeIntentAttributesDescription
selftype(menu)inoutMenu.

evaluate_yes_no ​

The index of a yes/no answer (no blanks around): 1 for y/yes, 2 for n/no (any case), the default if empty.

Attributes: pure

fortran
subroutine evaluate_yes_no(answer, default, choices, error, message)

Arguments

NameTypeIntentAttributesDescription
answercharacter(len=*)inAnswer.
defaultcharacter(len=1)inDefault: 'Y', 'N' or ' ' (none).
choicesinteger(kind=I4P)outallocatable[1] for yes, [2] for no; none on error.
errorinteger(kind=I4P)outError code.
messagecharacter(len=:)outallocatableError message.

Call graph

split_fields ​

Split an answer into fields: a blank separator splits at runs of blanks; any other exactly, each field trimmed (so 1,,3 has an empty field). Not tokenize, whose trailing-token behaviour belongs to the parser.

Attributes: pure

fortran
subroutine split_fields(answer, separator, fields)

Arguments

NameTypeIntentAttributesDescription
answercharacter(len=*)inAnswer (no blanks around, not empty).
separatorcharacter(len=*)inSeparator.
fieldstype(flap_string)outallocatableFields.

Call graph

Functions ​

default_index ​

Index of the (first) default option, 0 if none.

Attributes: pure

Returns: integer(kind=I4P)

fortran
function default_index(self) result(i)

Arguments

NameTypeIntentAttributesDescription
selfclass(menu)inMenu.

Call graph

raise ​

Write an error message on the error unit and return its code.

Returns: integer(kind=I4P)

fortran
function raise(self, code, message) result(error)

Arguments

NameTypeIntentAttributesDescription
selfclass(menu)inMenu.
codeinteger(kind=I4P)inError code.
messagecharacter(len=*)inError message.

Call graph

option_index ​

The option number written in a field: digits only, in 1..n; 0 otherwise.

Attributes: pure

Returns: integer(kind=I4P)

fortran
function option_index(field, n) result(choice)

Arguments

NameTypeIntentAttributesDescription
fieldcharacter(len=*)inField (no blanks around).
ninteger(kind=I4P)inNumber of options.

Call graph