bashzenity

Bash - Zenity list to display fields from two arrays


I wanted to make a zenity list that takes the fields for one column from one list and the fields for the other column from another list

menu=("option1" "option2" "option3")
desc=("description1" "description2" "description3")
ans=`zenity --list --column=Menu "${menu[@]}" --column=Description "${desc[@]}" --height 170`

That didn't work because it first displays all values from the first list and then from the other:

Menu Description
option1 option2
option3 description1
description2 description3

So I figured I probably need to merge them in alternating order but I don't know how.


Solution

  • From man zenity :

      --column=STRING
             Set the column header
    

    So the --column option will only set the header, and not parse your data. You will need to do some pre-processing before giving your data to zenity :

    #!/usr/bin/env bash
    menu=("option1" "option2" "option3")
    desc=("description1" "description2" "description3")
    
    # this for loop will create a new array ("option1" "description1" "option2" ...)
    # there will be issues if the 2 arrays don't have the same length    
    for (( i=0; i<${#menu[*]}; ++i)); do
        data+=( "${menu[$i]}" "${desc[$i]}" )
    done
    
    ans=$(zenity --list --column=Menu --column=Description --height 170 "${data[@]}")