fix: multiselect nameref collision with options/defaults arrays
This commit is contained in:
parent
7b63dbca46
commit
6090507530
1 changed files with 13 additions and 8 deletions
21
functions
21
functions
|
|
@ -1068,12 +1068,17 @@ multiselect(){
|
|||
get_cursor_row() { IFS=';' read -sdR -p $'\E[6n' ROW COL; echo ${ROW#*[}; }
|
||||
|
||||
local return_value=$1
|
||||
local -n options=$2
|
||||
local -n defaults=$3
|
||||
# Nameref-aliased to the caller's arrays. Named _ms_* (not "options"/
|
||||
# "defaults") because a nameref that happens to share its target
|
||||
# variable's exact name triggers a "circular name reference" from bash
|
||||
# and silently unreliable behavior - a caller passing arrays literally
|
||||
# named $options/$defaults would otherwise break this.
|
||||
local -n _ms_options=$2
|
||||
local -n _ms_defaults=$3
|
||||
|
||||
local selected=()
|
||||
for ((i=0; i<${#options[@]}; i++)); do
|
||||
if [[ ${defaults[i]} = "true" ]]; then
|
||||
for ((i=0; i<${#_ms_options[@]}; i++)); do
|
||||
if [[ ${_ms_defaults[i]} = "true" ]]; then
|
||||
selected+=("true")
|
||||
else
|
||||
selected+=("false")
|
||||
|
|
@ -1083,7 +1088,7 @@ multiselect(){
|
|||
|
||||
# determine current screen position for overwriting the options
|
||||
local lastrow=`get_cursor_row`
|
||||
local startrow=$(($lastrow - ${#options[@]}))
|
||||
local startrow=$(($lastrow - ${#_ms_options[@]}))
|
||||
|
||||
# ensure cursor and input echoing back on upon a ctrl+c during read -s
|
||||
trap "cursor_blink_on; stty echo; printf '\n'; exit" 2
|
||||
|
|
@ -1115,7 +1120,7 @@ multiselect(){
|
|||
print_options() {
|
||||
# print options by overwriting the last lines
|
||||
local idx=0
|
||||
for option in "${options[@]}"; do
|
||||
for option in "${_ms_options[@]}"; do
|
||||
#local prefix="[ ]"
|
||||
local prefix="${gry}${deselected_opt}${dfl}"
|
||||
if [[ ${selected[idx]} == true ]]; then
|
||||
|
|
@ -1142,9 +1147,9 @@ multiselect(){
|
|||
space) toggle_option $active;;
|
||||
enter) print_options -1; break;;
|
||||
up) ((active--));
|
||||
if [ $active -lt 0 ]; then active=$((${#options[@]} - 1)); fi;;
|
||||
if [ $active -lt 0 ]; then active=$((${#_ms_options[@]} - 1)); fi;;
|
||||
down) ((active++));
|
||||
if [ $active -ge ${#options[@]} ]; then active=0; fi;;
|
||||
if [ $active -ge ${#_ms_options[@]} ]; then active=0; fi;;
|
||||
esac
|
||||
done
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue