From d2347d8cfbb7cba52cf70551e15f4db0e9ca53fd Mon Sep 17 00:00:00 2001 From: Ville Rantanen Date: Mon, 19 Oct 2020 08:08:54 +0300 Subject: [PATCH] add timeout --- shell/select-option.sh | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/shell/select-option.sh b/shell/select-option.sh index 0c25f5f..9a0b5ad 100755 --- a/shell/select-option.sh +++ b/shell/select-option.sh @@ -50,7 +50,8 @@ function select_option { print_multiselected() { printf "$ESC[32;1m*$ESC[0m"; } fi 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 local idx=$selected while true; do @@ -60,6 +61,7 @@ function select_option { fi if [[ $idx -eq $selected ]]; then break; fi done + if [[ $timeoutec = 142 ]]; then echo esc; 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[C ]]; then echo left; return; fi @@ -207,7 +209,7 @@ _help() { echo "Select Option [Pure BASH] 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 -m Multiple choice selection. Note that only 7 options can be encoded reliably in return value. Use File output for more options. @@ -216,6 +218,7 @@ _help() { --title Shows a title above selection --center Center box on screen --middle Clear screen and position in the middle + --timeout # Set timeout in seconds -o File Save the choice(s) in a file. -_ Print spaces instead of underscores, workaround for shell arguments containing spaces. @@ -276,6 +279,7 @@ SELECT_CENTER=0 SELECT_MIDDLE=0 SELECT_TOFILE="" SELECT_TITLE="" +SELECT_TIMEOUT="" for (( i=1; i<=$#; i++ )); do value=${!i} j=$(( i + 1 )) @@ -285,6 +289,7 @@ for (( i=1; i<=$#; i++ )); do [[ "$value" = "--center" ]] && { SELECT_CENTER=1; continue; } [[ "$value" = "--middle" ]] && { SELECT_MIDDLE=1; continue; } [[ "$value" = "--title" ]] && { SELECT_TITLE="${!j}"; ((i++)); continue; } + [[ "$value" = "--timeout" ]] && { SELECT_TIMEOUT="-t ${!j}"; ((i++)); continue; } [[ "${value}" = "-"* ]] && { [[ "$value" =~ -.*h ]] && { _help; } [[ "$value" =~ -.*m ]] && { SELECT_MULTI=1; }