banner

For a list of BASHing data 2 blog posts see the index page.    RSS


Searching a pick-list with YAD

I've been celebrating the YAD dialog for 10 years now, but I'm still learning new ways to use it. This post is about a very handy YAD feature for selecting an entry from a pick-list.

Here's a sample list ("names"):

Andre Patrick
Archie Pittman
Derick Stark
Dusty Romero
Erma Rojas
Fletcher Preston
Garry Carr
Gino Chase
Gonzalo Goodwin
Imogene Bates
Kip Vargas
Kurt Randall
Lynette Sampson
Madeleine Clayton
Santiago Landry

Names generated with the RIG random identity generator, and selected with AWK:
 
rig -c 15 | awk -v RS="" -v FS="\n" '{print $1}' | sort

To make "names" into a pick-list for YAD, I use paste to put all the names on a single line and separate them with "!", which is the default item separator for YAD. The line is then stored in a variable, "list":

list=$(paste -s -d"!" names)

list1

One way to use YAD to select a name from the list is with the YAD form dialog with "list" in a combo-box:

yad --form --separator="" --field="Name":CB "$list"

This command builds a dialog with the first name on the list in the entry window, and with a click-me arrowhead on the right that allows you to select from the full list:

list2
list3

If I select "Fletcher Preston" from the list, that will be the name returned by YAD on the command line when I click "OK":

list4
list5

Without the --separator="" option, YAD would return the selected name with a pipe as trailing separator: "Fletcher Preston|".

The "CB" option on field in the YAD command tells YAD that the field's contents are to be presented as a combo-box. If I change that option to "CE", no name appears in the entry window, but if I start typing an entry, YAD offers choices from "list" beginning with that initial string, and case doesn't matter:

yad --form --separator="" --field="Name":CE "$list"

list6

Now for some magic. If I add a --complete="any" option to the command, I can search the picklist for any string. How about "on"?

yad --form --separator="" --complete="any" --field="Name":CE "$list"

list7

This feature has been an important effort-saver for me in a couple of my YAD-based scripts, where my pick-list has hundreds of entries. The --complete option takes one of 3 values: any to match any of the typed words, all to match all of the typed words and regex to treat the typed text as a regular expression. In the case of my "names" pick-list and a search for "on", any of these 3 values will return those 4 names. A "regex" search might look like this

list8

but to be honest I haven't needed to search a YAD pick-list that way yet!


Previous posts about YAD and its uses:


Last update: 2024-08-09
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License