Skip to content

Latest commit

 

History

History
62 lines (49 loc) · 3.4 KB

interaction.md

File metadata and controls

62 lines (49 loc) · 3.4 KB

Documentation for the functions in interaction.sh. A general overview is given in the project documentation.

Function documentation

If the pipes are not documented, the default is:

  • stdin: piped input ignored
  • stdout: empty

Parameters enclosed in brackets [ ] are optional.

read_and_validate()

Combined read and regex check.

Important read flags:

  • -n <nb_chars> : read stops after nb_chars, which gives a "auto-return" UX
  • -s : makes read hide the user input
Param. $1validation regex (if matched it leads to return status 0)
[$2]flags passed to read, defaults to an empty string
Pipes stdinpiped input ignored; used via read
stdoutthe user input
Status 0the user entered a $1 match
1the user input doesn't match $1
2if $1 is empty

get_user_confirmation()

Yes/no type questions. Blocks waiting for user input, returns as soon as one character is entered.

Param.[$1]confirmation character, defaults to y
Pipes stdinpiped input ignored; used via read_and_validate()
stdouta newline control sequence to reset the cursor which stands just after the user input
Status 0if the user entered $1
1if the user entered something else

get_user_choice()

The function behaves as if it ignores input as long as it doesn't match the regex $1. It may hence be used as "option selector". As soon as the input matches $1 the function returns with status 0 and provides the selected option on stdout.

The way it works is that it uses read's -s flag to keep the entered input hidden; if the input doesn't match $1, read_and_validate() returns 1, the function loops and calls read_and_validate() again.

Example: the user is offered 3 choices numbered 1 to 3, $1 should be ^[1-3]$

Param.$1options regex
Pipes stdinpiped input ignored; used via read_and_validate()
stdoutthe selected option
Status0