From 6090507530dac1491183cec74c4ef81fabfe89d3 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Sat, 18 Jul 2026 22:31:30 -0700 Subject: [PATCH] fix: multiselect nameref collision with options/defaults arrays --- functions | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/functions b/functions index 94a9a51..ce76651 100644 --- a/functions +++ b/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