The Linux Rain Linux General/Gaming News, Reviews and Tutorials

Tips on getting (and suggesting, and editing) user input

By Bob Mesibov, published 01/08/2014 in Tutorials

In a previous Linux Rain article I explained how to use the YAD dialog to do multiple-item data entry in a GUI. Here I show one way to allow users to check and edit what they've entered in YAD. I also explain how to offer default, editable inputs in terminal-based data entry, and how to color the entry-prompt differently from the user's input.

Providing an editable default reply for 'read'

I'll start with the terminal. For many a long year (OK, since 1989), the BASH shell has had a built-in command called read which can accept user input and store it in a variable. The -p option for read allows you to write an entry prompt for that input:

Since BASH version 4, you can include a default reply after the entry-prompt. Just precede the default reply with the -e and -i options:

Notice the cursor at the end of the default reply? The user can backspace from the cursor position to edit or delete the default reply, and the edit is what gets stored in the variable:

Adding some color

It would be nice to contrast the color of the entry-prompt in read with the user's input. That isn't possible with the read command alone, but we can do a workaround. Instead of using the read -p option to create the entry-prompt, we can feed in the prompt using the echo command. To keep the prompt on the same line, we need to add the -n option to echo to suppress a trailing newline :

And by adding the -e option to echo, we can get it to do some text coloring:

Putting the two tricks together, we get a contrasting prompt and a default (or user-entered) reply:

(I made the entry-prompt bold as well as blue by using the code '\e[1;36m' instead of non-bold '\e[0;36m'.)

User-checking the input in YAD

A great feature of the YAD dialog for entering text, choosing from a list or displaying information is the -editable option. Suppose the user has entered data in a form dialog like the one below, which defaults to the pipe character (|) as separator for output items. The example dialog is contained in a BASH script called 'demo':

Whoops. Looks like Mr Bloggs made a mistake in his date of birth entry. No problem, we can help him by offering a confirmation dialog in which his previous entries are editable.

First we'll send the output of the YAD form dialog to a temp file, then extract each of the data entries from that file with the cut command, using the pipe character as delimiter (-d'|'):

Next, we'll echo those entries back into a YAD 'text-info' dialog with the -editable option, storing the edited (or not) result in a variable:

Editing is done by Mr Bloggs:

If Fred is happy with the edited result and clicks 'OK', YAD faithfully returns the edited data with the entry-prompts and the tabs and newline characters (from the text-info dialog input string) included in the output. However, because we stored the YAD output in a variable, we can replace the tab and newline characters with a single space simply by echoing the variable (echo $final):

and we can remove the entry-prompts and replace spaces with pipes using sed commands:

The finished script is shown below; it includes a 'get me out of here!' cancel for the text-info dialog:

About the Author

Bob Mesibov is Tasmanian, retired and a keen Linux tinkerer.

Tags: user-input yad linux tutorial scripting shell bash
blog comments powered by Disqus