Zenity:如何从 Key/Value 关联数组中获取列表选择?

Zenity: How To Get List Selection From Key/Value Associative Array?

如何从关联数组中获取 key/values 并将它们显示在 Zenity 列表对话框中?

示例:

#!/bin/bash

get_final_bookmarks_choice(){
    if [ ${#matched_bookmarks[@]} -eq 1 ]; then
        # open_bookmarks_command
        return
    fi
    # guard clause.  If no bm found.
    if [ -${#matched_bookmarks[@]} -eq 0 ]; then 
        msg='No bookmarks match found.'
        notify-send "$msg"; echo "$msg"
        return
    fi

    bm_url=$(zenity --entry --title="Multi matches found" \
    --text="Choose bookmark" \
    --column="Name" --column="URL" \

    # Key/value dictionary here.
    )
    # Return the key.
    echo "$key returned here."
}


declare -A matched_bookmarks
matched_bookmarks=(
    ['match 1']='http://match1.com'
    ['match 2']='http://match2.com'
    ['match 3']='http://match3.com'
)

bm_name="$(get_final_bookmarks_choice)"

bm_url="${matched_bookmarks[$bm_name]}"

echo "Bookmarks URL:$bm_url"

您必须使用 array,而不是 associative array,作为 zenity 命令的参数:

dialogarray=()
for idx in "${!matched_bookmarks[@]}";do
    dialogarray+=("$idx" "${matched_bookmarks[$idx]}")
done

zenity --list --title="Multi matches found" --text="Choose bookmark" \
     --column="Name" --column="URL" "${dialogarray[@]}"

这是我的脚本版本:

#!/bin/bash

get_final_bookmarks_choice(){
    local -n Assoc= Result=
    case ${#Assoc[@]}  in
        1 ) # open_bookmarks_command
            Result=${!Assoc[*]}
            return
            ;;
        0 ) # guard clause.  If no bm found.
            msg='No bookmarks match found.'
            notify-send "$msg"
            echo "$msg"
            exit 1
            ;;
        * )
            local -a zenDialog=()
            local idx
            for idx in "${!Assoc[@]}";do
                zenDialog+=("$idx" "${Assoc[$idx]}")
            done
            read -r Result < <(
                zenity --list --title="Multi matches found" \
                       --text="Choose bookmark" \
                       --column="Name" --column="URL" "${zenDialog[@]}" )
    esac
}


declare -A matched_bookmarks
matched_bookmarks=(
    ['match 1']='http://match1.com'
    ['match 2']='http://match2.com'
    ['match 3']='http://match3.com'
)

get_final_bookmarks_choice matched_bookmarks bm_name

bm_url="${matched_bookmarks[$bm_name]}"

echo "Bookmarks URL:$bm_url"

为了好玩,一个基于 SE 的版本:

#!/bin/bash
shopt -s extglob
get_final_bookmarks_choice(){ local -n Assoc= Result=
    case ${#Assoc[@]}  in
        1 ) # open_bookmarks_command
            Result=${!Assoc[*]}
            return  ;;
        0 ) # guard clause.  If no bm found.
            msg='No bookmarks match found.'
            notify-send "$msg"
            echo "$msg"
            return 1  ;;
        * )
            local -a zArgs=(); local idx
            for idx in "${!Assoc[@]}";do
                zArgs+=("$idx" "${Assoc[$idx]}")
                while IFS=$'\r\n' read -r idx;do
                    [ "$idx" ]&&[ -z "${idx//*title*}" ]&&break;done< <(
                    wget -qO - "${Assoc[$idx]}")
                idx=${idx//<*([^>])>}  idx=${idx//&amp;/&} 
                zArgs+=("$idx")
            done
            read -r Result < <( zenity --list --width 860 --height 200 \
                --title="Multi matches found" --text="Choose bookmark" \
                --column="Name" --column="URL" --column="Title" "${zArgs[@]}");;
    esac
}
declare -A matched_bookmarks
matched_bookmarks=( ['Stack Overflow']='https://whosebug.com/'
                    ['Code Golf']='https://codegolf.stackexchange.com/'
                    ['Unix & Linux']='https://unix.stackexchange.com/'   )
get_final_bookmarks_choice matched_bookmarks bm_name &&
    echo "Bookmarks URL:${matched_bookmarks[$bm_name]}"

应该产生如下内容: