choose is a shell script which allows an interactive choice on stderr among lines from stdin (or arguments) and returns the result on stdout.
multiple lines menu one line
---> choose --->
args / stdin TTY input / stderr stdout
It is nearly the same goal as the shell builtin select with fancy I/O more.
A choice can be preselected as first argument. In cursor mode, it falls back to the first line if there is no default choice.
When the key ENTER is pressed, the selected choice is validated.
-
In cursor mode (the default), the selection can be moved with the keys UP and DOWN.
-
In numerated mode, a number matching a list item can be entered.
There are 2 styles for the selected line. It can be shown with an arrow or in a reversed font.
When the choice is done, the menu can disappeared (quiet option), be replaced by the chosen line (default), or be kept as is (verbose option).
If you don't want it tampers your TTY with alternative settings and special characters, use -nav options (numerated mode with arrow and keep verbose). It disables all "advanced" features.
The environment variables CHOOSE_INDENT and CHOOSE_ARROW allow to customize line prefix.
# answer=$(choose -av '' yes no)
=> yes
no
# echo $answer
yes
There is a directory with more useful examples.
-
In cursor mode, long lines with non printable characters are smaller than the terminal width.
-
In numerated mode with default verbose, clearing may be wrong if there are long lines with non printable characters.
It should need no special tool to run. Indeed all requirements are standard and commonly available.
choose runs with /bin/sh which can be any POSIX shell (ash, bash, dash, ksh, zsh...).
It uses many basic tools from coreutils (basename, cat, head, cut, tr, fold, od, dd, seq, wc, stty), ncurses (tput) and sed.
It also needs procfs (especially /proc/?/fd/0 to get terminal input).
It is designed for GNU/Linux but could run on other Unix.
The author disclaims copyrights, so choose is in the public domain.