more options for selection

This commit is contained in:
2019-01-08 11:48:11 +02:00
parent 17603445ca
commit 2e5838f30d

View File

@@ -5,7 +5,7 @@ function select_option {
local nformat local nformat
local optcount=$# local optcount=$#
printf -v nformat "%%%dd. " ${#optcount} printf -v nformat "%%%dd. " ${#optcount}
local max_opt=0 local max_opt=$(( ${#SELECT_TITLE} - 4 ))
local multiselected=() local multiselected=()
local choices=() local choices=()
local shortcuts=() local shortcuts=()
@@ -25,11 +25,20 @@ function select_option {
fi fi
((idx++)) ((idx++))
done 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 # little helpers for terminal print control and key input
ESC=$( printf "\033" ) ESC=$( printf "\033" )
cursor_blink_on() { printf "$ESC[?25h"; } cursor_blink_on() { printf "$ESC[?25h"; }
cursor_blink_off() { printf "$ESC[?25l"; } cursor_blink_off() { printf "$ESC[?25l"; }
clear_screen() { printf "$ESC[2J"; }
cursor_to() { printf "$ESC[$1;${2:-1}H"; } cursor_to() { printf "$ESC[$1;${2:-1}H"; }
if [[ $SELECT_MONOCHROME -eq 1 ]]; then 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"; } 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() { reset_display() {
cursor_blink_on; stty echo; printf '\n'; 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) # initially print empty new lines (scroll down if at bottom of screen)
for opt; do printf "\n"; done for opt; do printf "\n"; done
# Extras for top and bottom # Extras for top and bottom
@@ -71,11 +91,17 @@ function select_option {
# determine current screen position for overwriting the options # determine current screen position for overwriting the options
local lastrow=`get_cursor_row` local lastrow=`get_cursor_row`
local startrow=$(( $lastrow - $# - 2 )) local startrow=$(( $lastrow - $# - 2 ))
#~ if [ -n "$SELECT_TITLE" ]; then
#~ ((startrow--))
#~ ((startrow--))
#~ fi
local toprow="+~~--" local toprow="+~~--"
local bottomrow="--~~+" local bottomrow="--~~+"
local lborderchars="|:'" local lborderchars="|:'"
local rborderchars="|:." local rborderchars="|:."
# ensure cursor and input echoing back on upon a ctrl+c during read -s # 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 trap "cursor_blink_on; stty echo; printf '\n'; exit 1" 2
cursor_blink_off cursor_blink_off
@@ -100,7 +126,7 @@ function select_option {
cursor_to $startrow cursor_to $startrow
if [ $selected -lt 0 ]; then selected=$(($# - 1)); fi if [ $selected -lt 0 ]; then selected=$(($# - 1)); fi
if [ $selected -ge $# ]; then selected=0; 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 for opt; do
opt="${choices[$idx]}" opt="${choices[$idx]}"
@@ -119,6 +145,7 @@ function select_option {
fi fi
printf -v padopt "%-${max_opt}s" "$opt" printf -v padopt "%-${max_opt}s" "$opt"
cursor_to $(($startrow + $idx + 1)) cursor_to $(($startrow + $idx + 1))
printf "%s" "${pad_center}"
if [ $idx -eq $selected ]; then if [ $idx -eq $selected ]; then
print_selected "$lborder" "$nidx$shortcut" "$padopt" "$rborder" print_selected "$lborder" "$nidx$shortcut" "$padopt" "$rborder"
else else
@@ -131,7 +158,7 @@ function select_option {
((idx++)) ((idx++))
done done
cursor_to $(($startrow + $idx + 1)) 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 # user key control
local user_input=`key_input` local user_input=`key_input`
@@ -180,12 +207,15 @@ _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] list of options Usage: $SELF [-nms_] [--nc] [-o File] [--title Title] 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.
-s Show shortcuts -s Show shortcuts
--nc No colors --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. -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.
@@ -242,13 +272,19 @@ SELECT_SHORTCUTS=0
SELECT_NUMBERS=0 SELECT_NUMBERS=0
SELECT_UNDERSCORES=0 SELECT_UNDERSCORES=0
SELECT_MULTI=0 SELECT_MULTI=0
SELECT_CENTER=0
SELECT_MIDDLE=0
SELECT_TOFILE="" SELECT_TOFILE=""
SELECT_TITLE=""
for (( i=1; i<=$#; i++ )); do for (( i=1; i<=$#; i++ )); do
value=${!i} value=${!i}
j=$(( i + 1 )) j=$(( i + 1 ))
[[ "$value" = "--help" ]] && _help [[ "$value" = "--help" ]] && _help
[[ "$value" = "--nc" ]] && { SELECT_MONOCHROME=1; continue; } [[ "$value" = "--nc" ]] && { SELECT_MONOCHROME=1; continue; }
[[ "$value" = "--package" ]] && { _package_self; exit; } [[ "$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}" = "-"* ]] && {
[[ "$value" =~ -.*h ]] && { _help; } [[ "$value" =~ -.*h ]] && { _help; }
[[ "$value" =~ -.*m ]] && { SELECT_MULTI=1; } [[ "$value" =~ -.*m ]] && { SELECT_MULTI=1; }