Sunday, September 6, 2009

[Level 2] Shell Interactive With Zenity Episode 5 -- list

If you want to give a list for user to choose, you can use the option "--list". The command synopsis as below:
SYNOPSIS
     zenity  --list [--title=title] [--window-icon=path] [--width=width]    [--height=height] [--timeout=seconds] [--column=text] [--checklist]       [--radiolist] [--separator=character] [--multiple] [--editable] [--print-column=number] [--hide-column=number]

Just like "--calendar", if you press "ESC" key or click button "Cancel", the return code will be "1". If you select one option and click "OK", zenity will response the option and return the code will be "0". And also the same effect about the options "--title", "--timeout"...etc, for saving your time, please refer to the previous post "[Level 2] Shell Interactive With Zenity Episode 1 -- calendar", too. About "--multiple" and "--separator", please refer to tht previous post "[Level 2] Shell Interactive With Zenity Episode 4 -- file-selection".

As the experience before, maybe you cannot wait and use the command, "zenity --list", to show the first screen. Unfortunately, you will get an error response like below:
# zenity --list
No column titles specified for List dialog.
#

That means you have to also define the column. So the first command for list will be:
# zenity --list --column=c1

After above screen show out, you may wonder about "How can we put something in the list?". Do not break the above command. At this comment, the zenity is waiting your input to construct the list. For example, after you key in the "zenity" command, and you put some entry, then the screen will add the entries that you key in. If you put an empty line, the zenity will also create a empty entry.
# zenity --list --column=c1
o1
o2
o3
o4
o5





#

Of course, you cannot ask user to key in the options for you! So how could we pass the "list" to the zenity? According to the above option, you might know the zenity get the option from the stardard input. Therefore, we can use "|" to pass the list. Ex.
# ls /etc | zenity --list --column=filename/foldername

If you want to use another style to show the list, you can use the option "--checklist" or "--radiolist". But with these two options, you should create two column, one for "check box"/"radio box", another is for the content.
# ls /etc | zenity --list --checklist --column=check --column=filename/foldername

# ls /etc | zenity --list --radiolist --column=check --column=filename/foldername

If we have multi columns, how does zenity deal this data? Everytime when zenity get a line, it put it into a column. Ex.
(In this case, <NULL> means empty line)
# zenity --list --column=c1 --column=c2 --column=c3 <<EOF
~> c1-1
~> c2-1
~> c3-1
~> c1-2
~> c2-2
~> c3-2
~> c1-3
~> c2-3
~> <NULL>
~> c1-4
~> <NULL>
~> c3-4
~> EOF

#


If you want to hide some column, you can use the option "--hide-column" and if you want the zenity to response the specific column, you can use the option "--print-column" (The first column is the default column that zenity response). Ex.
# zenity --list --column=c1 --column=c2 --column=c3 --hide-column=2 --print-column=3 <<EOF
~> c1-1
~> c2-1
~> c3-1
~> c1-2
~> c2-2
~> c3-2
~> c1-3
~> c2-3
~> <NULL>
~> c1-4
~> <NULL>
~> c3-4
~> EOF
c1-2

#

If you want to let user to modify the list content, you can use the option "--editable"
# ls /etc | zenity --list --column=filename/foldername --editable

Wish this helps.

regards,
Stanley Huang