add timeout

This commit is contained in:
Ville Rantanen
2020-10-19 08:08:54 +03:00
parent dfd5111bea
commit d2347d8cfb

View File

@@ -50,7 +50,8 @@ function select_option {
print_multiselected() { printf "$ESC[32;1m*$ESC[0m"; } print_multiselected() { printf "$ESC[32;1m*$ESC[0m"; }
fi fi
get_cursor_row() { IFS=';' read -sdR -p $'\E[6n' ROW COL; echo ${ROW#*[}; } get_cursor_row() { IFS=';' read -sdR -p $'\E[6n' ROW COL; echo ${ROW#*[}; }
key_input() { read -s -n1 key1 2>/dev/null >&2 key_input() { read -s -n1 $SELECT_TIMEOUT key1 2>/dev/null >&2
timeoutec=$?
read -s -n2 -t 0.1 key2 2>/dev/null >&2 read -s -n2 -t 0.1 key2 2>/dev/null >&2
local idx=$selected local idx=$selected
while true; do while true; do
@@ -60,6 +61,7 @@ function select_option {
fi fi
if [[ $idx -eq $selected ]]; then break; fi if [[ $idx -eq $selected ]]; then break; fi
done done
if [[ $timeoutec = 142 ]]; then echo esc; return; fi
if [[ $key1$key2 = $ESC[A ]]; then echo up; return; fi if [[ $key1$key2 = $ESC[A ]]; then echo up; return; fi
if [[ $key1$key2 = $ESC[B ]]; then echo down; return; fi if [[ $key1$key2 = $ESC[B ]]; then echo down; return; fi
if [[ $key1$key2 = $ESC[C ]]; then echo left; return; fi if [[ $key1$key2 = $ESC[C ]]; then echo left; return; fi
@@ -207,7 +209,7 @@ _help() {
echo "Select Option [Pure BASH] echo "Select Option [Pure BASH]
Adapted from: https://unix.stackexchange.com/questions/146570/arrow-key-enter-menu Adapted from: https://unix.stackexchange.com/questions/146570/arrow-key-enter-menu
Usage: $SELF [-nms_] [--nc] [-o File] [--title Title] list of options Usage: $SELF [-nms_] [--nc] [-o File] [--title Title] [--timeout #] list of options
-n Show item number -n Show item number
-m Multiple choice selection. Note that only 7 options can be encoded -m Multiple choice selection. Note that only 7 options can be encoded
reliably in return value. Use File output for more options. reliably in return value. Use File output for more options.
@@ -216,6 +218,7 @@ _help() {
--title Shows a title above selection --title Shows a title above selection
--center Center box on screen --center Center box on screen
--middle Clear screen and position in the middle --middle Clear screen and position in the middle
--timeout # Set timeout in seconds
-o File Save the choice(s) in a file. -o File Save the choice(s) in a file.
-_ Print spaces instead of underscores, workaround for -_ Print spaces instead of underscores, workaround for
shell arguments containing spaces. shell arguments containing spaces.
@@ -276,6 +279,7 @@ SELECT_CENTER=0
SELECT_MIDDLE=0 SELECT_MIDDLE=0
SELECT_TOFILE="" SELECT_TOFILE=""
SELECT_TITLE="" SELECT_TITLE=""
SELECT_TIMEOUT=""
for (( i=1; i<=$#; i++ )); do for (( i=1; i<=$#; i++ )); do
value=${!i} value=${!i}
j=$(( i + 1 )) j=$(( i + 1 ))
@@ -285,6 +289,7 @@ for (( i=1; i<=$#; i++ )); do
[[ "$value" = "--center" ]] && { SELECT_CENTER=1; continue; } [[ "$value" = "--center" ]] && { SELECT_CENTER=1; continue; }
[[ "$value" = "--middle" ]] && { SELECT_MIDDLE=1; continue; } [[ "$value" = "--middle" ]] && { SELECT_MIDDLE=1; continue; }
[[ "$value" = "--title" ]] && { SELECT_TITLE="${!j}"; ((i++)); continue; } [[ "$value" = "--title" ]] && { SELECT_TITLE="${!j}"; ((i++)); continue; }
[[ "$value" = "--timeout" ]] && { SELECT_TIMEOUT="-t ${!j}"; ((i++)); continue; }
[[ "${value}" = "-"* ]] && { [[ "${value}" = "-"* ]] && {
[[ "$value" =~ -.*h ]] && { _help; } [[ "$value" =~ -.*h ]] && { _help; }
[[ "$value" =~ -.*m ]] && { SELECT_MULTI=1; } [[ "$value" =~ -.*m ]] && { SELECT_MULTI=1; }