@@ -489,12 +489,12 @@ $grey─────────────────────────
489489 # - G_WHIP_NOCANCEL=1 | Optional, hide the cancel button on inputbox, menu and checkbox dialogues
490490 # - G_WHIP_MENU_ARRAY | Required for G_WHIP_MENU to set available menu entries, 2 array indices per line: ('item' 'description')
491491 # - G_WHIP_CHECKLIST_ARRAY | Required for G_WHIP_CHECKLIST set available checklist options, 3 array indices per line: ('item' 'description' 'on'/'off')
492- # - G_WHIP_CHECKLIST_ENUM=1 | Optional, if set, the checklist will be displayed enumerated with numeric tags rather than the provided tags
492+ # - G_WHIP_CHECKLIST_ENUM=1 | Optional, if set, the checklist will be displayed enumerated with numeric tags rather than the provided tags
493493 # Output:
494494 # - G_WHIP_RETURNED_VALUE | Returned value from inputbox/menu/checklist based whiptail items
495495
496496 # G_WHIP_DESTROY | Clear vars after run of whiptail
497- G_WHIP_DESTROY (){ unset -v G_WHIP_DEFAULT_ITEM G_WHIP_SIZE_X_MAX G_WHIP_BUTTON_OK_TEXT G_WHIP_BUTTON_CANCEL_TEXT G_WHIP_NOCANCEL G_WHIP_MENU_ARRAY G_WHIP_CHECKLIST_ARRAY G_WHIP_INPUTBOX_REGEX G_WHIP_INPUTBOX_REGEX_TEXT; }
497+ G_WHIP_DESTROY (){ unset -v G_WHIP_DEFAULT_ITEM G_WHIP_SIZE_X_MAX G_WHIP_BUTTON_OK_TEXT G_WHIP_BUTTON_CANCEL_TEXT G_WHIP_NOCANCEL G_WHIP_MENU_ARRAY G_WHIP_CHECKLIST_ARRAY G_WHIP_INPUTBOX_REGEX G_WHIP_INPUTBOX_REGEX_TEXT G_WHIP_CHECKLIST_ENUM ; }
498498
499499 # Run once, to be failsafe in case any exported/environment variables are left from originating shell
500500 G_WHIP_DESTROY
@@ -835,7 +835,7 @@ $grey─────────────────────────
835835 # G_WHIP_CHECKLIST "message"
836836 # - Prompt user to select multiple options from G_WHIP_CHECKLIST_ARRAY and save choice to G_WHIP_RETURNED_VALUE
837837 # - Exit code: 0=selection done, else=user cancelled or noninteractive
838- # - G_WHIP_CHECKLIST_ENUM=1 | Optional, if set, the checklist will be displayed enumerated with numeric tags rather than the provided tags
838+ # - G_WHIP_CHECKLIST_ENUM=1: Optional, if set, the checklist will be displayed enumerated with numeric tags rather than the provided tags
839839 G_WHIP_CHECKLIST ()
840840 {
841841 local result=1
@@ -847,45 +847,33 @@ $grey─────────────────────────
847847 [[ $G_WHIP_NOCANCEL == 1 ]] && NOCANCEL=(' --nocancel' )
848848 G_WHIP_BUTTON_OK_TEXT=${G_WHIP_BUTTON_OK_TEXT:- Confirm}
849849
850- # Places to save original checklist and default
851- local orig_checklist=() original_default_item=" $G_WHIP_DEFAULT_ITEM "
852-
853- # ###
854- # If G_WHIP_CHECKLIST_ENUM=1, the checklist will be enumerated with numeric tags for whiptail,
855- # while preserving the original tags for mapping backwards after the checklist selection.
856- if [[ ${G_WHIP_CHECKLIST_ENUM:- 0} == 1 && ${# G_WHIP_CHECKLIST_ARRAY[@]} -gt 0 ]]
850+ # If G_WHIP_CHECKLIST_ENUM=1 is set, map and replace original tags and default with numeric ones
851+ if (( $G_WHIP_CHECKLIST_ENUM ))
857852 then
858- # save the original checklist
859- orig_checklist=(" ${G_WHIP_CHECKLIST_ARRAY[@]} " )
860-
861- # Build numeric-tagged checklist for whiptail while preserving original tags
862- local -a __tmp=() __idx=0 __map=()
863- for (( i= 0 ;i< ${# orig_checklist[@]} ;i+= 3 )) ; do
864- __map[$__idx ]=" ${orig_checklist[$i]} "
865- __tmp+=(" $__idx " " ${orig_checklist[$i+1]} " " ${orig_checklist[$i+2]} " )
866- [[ -n " $original_default_item " && " $original_default_item " == " ${orig_checklist[$i]} " ]] && G_WHIP_DEFAULT_ITEM=$__idx
867- (( __idx++ ))
853+ local map=() idx=0
854+ for (( i= 0 ;i< ${# G_WHIP_CHECKLIST_ARRAY[@]} ;i+= 3 ))
855+ do
856+ [[ $G_WHIP_DEFAULT_ITEM == " ${G_WHIP_CHECKLIST_ARRAY[$i]} " ]] && G_WHIP_DEFAULT_ITEM=$idx
857+ map[$idx ]=${G_WHIP_CHECKLIST_ARRAY[$i]}
858+ G_WHIP_CHECKLIST_ARRAY[$i ]=$idx
859+ (( idx++ ))
868860 done
869- G_WHIP_CHECKLIST_ARRAY=(" ${__tmp[@]} " )
870861 fi
871862
872863 G_WHIP_INIT 3
873864 # shellcheck disable=SC2086
874865 G_WHIP_RETURNED_VALUE=$( whiptail ${G_PROGRAM_NAME: +--title " $G_PROGRAM_NAME " } --backtitle " $WHIP_BACKTITLE | Use spacebar to toggle selection" --checklist " $WHIP_MESSAGE " --separate-output --ok-button " $G_WHIP_BUTTON_OK_TEXT " --cancel-button " $G_WHIP_BUTTON_CANCEL_TEXT " " ${NOCANCEL[@]} " --default-item " $G_WHIP_DEFAULT_ITEM " $WHIP_SCROLLTEXT " $WHIP_SIZE_Y " " $WHIP_SIZE_X " " $WHIP_SIZE_Z " -- " ${G_WHIP_CHECKLIST_ARRAY[@]} " 3>&1 1>&2 2>&3 -)
875866 result=$?
876867
877- # If enumeration was used, map numeric selections back to original tags
878- if [[ ${ G_WHIP_CHECKLIST_ENUM:- 0} == 1 && ${ # orig_checklist[@]} -gt 0 && -n " $G_WHIP_RETURNED_VALUE " ]]
868+ # If G_WHIP_CHECKLIST_ENUM=1 is set, replace numeric selections back to original tags
869+ if (( $ G_WHIP_CHECKLIST_ENUM ))
879870 then
880- local sel mapped=()
881- for sel in $G_WHIP_RETURNED_VALUE ; do
882- mapped+=(" ${__map[$sel]} " )
871+ local mapped=()
872+ for idx in $G_WHIP_RETURNED_VALUE
873+ do
874+ mapped+=(" ${map[$idx]} " )
883875 done
884- G_WHIP_RETURNED_VALUE=" ${mapped[*]} "
885- # restore originals
886- G_WHIP_CHECKLIST_ARRAY=(" ${orig_checklist[@]} " )
887- G_WHIP_DEFAULT_ITEM=" $original_default_item "
888- unset -v __map __tmp __idx sel mapped
876+ G_WHIP_RETURNED_VALUE=${mapped[*]}
889877 fi
890878
891879 G_WHIP_RETURNED_VALUE=$( echo -e " $G_WHIP_RETURNED_VALUE " | tr ' \n' ' ' )
0 commit comments