From 2e5838f30de60ceecb31f4657be83a1c9e92f9d6 Mon Sep 17 00:00:00 2001 From: Ville Rantanen Date: Tue, 8 Jan 2019 11:48:11 +0200 Subject: [PATCH] more options for selection --- shell/select-option.sh | 46 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/shell/select-option.sh b/shell/select-option.sh index 0501fc3..36f7384 100755 --- a/shell/select-option.sh +++ b/shell/select-option.sh @@ -5,7 +5,7 @@ function select_option { local nformat local optcount=$# printf -v nformat "%%%dd. " ${#optcount} - local max_opt=0 + local max_opt=$(( ${#SELECT_TITLE} - 4 )) local multiselected=() local choices=() local shortcuts=() @@ -25,11 +25,20 @@ function select_option { fi ((idx++)) done + if [[ $SELECT_CENTER -eq 1 ]]; then + local pad_center=$(( ( $( tput cols ) - $max_opt ) / 2 - 5 )) + if [[ $pad_center -gt 0 ]]; then + printf -v pad_center "%${pad_center}s" " " + else + pad_center="" + fi + fi # little helpers for terminal print control and key input - ESC=$( printf "\033") + ESC=$( printf "\033" ) cursor_blink_on() { printf "$ESC[?25h"; } cursor_blink_off() { printf "$ESC[?25l"; } + clear_screen() { printf "$ESC[2J"; } cursor_to() { printf "$ESC[$1;${2:-1}H"; } if [[ $SELECT_MONOCHROME -eq 1 ]]; then print_option() { printf " $ESC[30m%s $ESC[0m%s %s $ESC[30m%s$ESC[0m$ESC[K" "$1" "$2" "$3" "$4"; } @@ -63,6 +72,17 @@ function select_option { reset_display() { cursor_blink_on; stty echo; printf '\n'; } + if [[ $SELECT_MIDDLE -eq 1 ]]; then + clear_screen + cursor_to 1 + local pad_middle=$(( ( $( tput lines ) - $optcount ) / 2 - 2 )) + if [[ $pad_middle -gt 0 ]]; then + printf "\n%.0s" $( seq 1 $pad_middle ) + fi + fi + if [ -n "$SELECT_TITLE" ]; then + printf "${pad_center} $ESC[35;1m%s$ESC[0m$ESC[K \n" "$SELECT_TITLE" + fi # initially print empty new lines (scroll down if at bottom of screen) for opt; do printf "\n"; done # Extras for top and bottom @@ -71,11 +91,17 @@ function select_option { # determine current screen position for overwriting the options local lastrow=`get_cursor_row` local startrow=$(( $lastrow - $# - 2 )) + #~ if [ -n "$SELECT_TITLE" ]; then + #~ ((startrow--)) + #~ ((startrow--)) + #~ fi local toprow="+~~--" local bottomrow="--~~+" local lborderchars="|:'" local rborderchars="|:." + + # ensure cursor and input echoing back on upon a ctrl+c during read -s trap "cursor_blink_on; stty echo; printf '\n'; exit 1" 2 cursor_blink_off @@ -100,7 +126,7 @@ function select_option { cursor_to $startrow if [ $selected -lt 0 ]; then selected=$(($# - 1)); fi if [ $selected -ge $# ]; then selected=0; fi - printf " $ESC[30m%s$ESC[0m$ESC[K " "$toprow" + printf "${pad_center} $ESC[30m%s$ESC[0m$ESC[K " "$toprow" for opt; do opt="${choices[$idx]}" @@ -119,6 +145,7 @@ function select_option { fi printf -v padopt "%-${max_opt}s" "$opt" cursor_to $(($startrow + $idx + 1)) + printf "%s" "${pad_center}" if [ $idx -eq $selected ]; then print_selected "$lborder" "$nidx$shortcut" "$padopt" "$rborder" else @@ -131,7 +158,7 @@ function select_option { ((idx++)) done cursor_to $(($startrow + $idx + 1)) - printf "$ESC[K$ESC[30m%${bottomlength}s$ESC[0m " "$bottomrow" + printf "${pad_center}$ESC[K$ESC[30m%${bottomlength}s$ESC[0m " "$bottomrow" # user key control local user_input=`key_input` @@ -180,12 +207,15 @@ _help() { echo "Select Option [Pure BASH] Adapted from: https://unix.stackexchange.com/questions/146570/arrow-key-enter-menu - Usage: $SELF [-nms_] [--nc] [-o File] list of options + Usage: $SELF [-nms_] [--nc] [-o File] [--title Title] 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. -s Show shortcuts --nc No colors + --title Shows a title above selection + --center Center box on screen + --middle Clear screen and position in the middle -o File Save the choice(s) in a file. -_ Print spaces instead of underscores, workaround for shell arguments containing spaces. @@ -242,13 +272,19 @@ SELECT_SHORTCUTS=0 SELECT_NUMBERS=0 SELECT_UNDERSCORES=0 SELECT_MULTI=0 +SELECT_CENTER=0 +SELECT_MIDDLE=0 SELECT_TOFILE="" +SELECT_TITLE="" for (( i=1; i<=$#; i++ )); do value=${!i} j=$(( i + 1 )) [[ "$value" = "--help" ]] && _help [[ "$value" = "--nc" ]] && { SELECT_MONOCHROME=1; continue; } [[ "$value" = "--package" ]] && { _package_self; exit; } + [[ "$value" = "--center" ]] && { SELECT_CENTER=1; continue; } + [[ "$value" = "--middle" ]] && { SELECT_MIDDLE=1; continue; } + [[ "$value" = "--title" ]] && { SELECT_TITLE="${!j}"; ((i++)); continue; } [[ "${value}" = "-"* ]] && { [[ "$value" =~ -.*h ]] && { _help; } [[ "$value" =~ -.*m ]] && { SELECT_MULTI=1; }