Dialog (shell): Difference between revisions

From wikinotes
No edit summary
Line 50: Line 50:
= components =
= components =
<blockquote>
<blockquote>
== Messagebox ==
== MessageBox ==
<blockquote>
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
dialog --title 'Message' --msgbox 'Hello, world!' 5 20
dialog --title 'Message' --msgbox 'Hello, world!' 5 20
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- MessageBox -->


== YesNo Prompt ==
== Yes/No Prompt ==
<blockquote>
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
dialog --title "Message"  --yesno "Are you having\ fun?" 6 25
dialog --title "Message"  --yesno "Are you having\ fun?" 6 25
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Yes/No Prompt -->


== InputBox ==
== Input Box ==
<blockquote>
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
## dialog --inputbox text  width height
# dialog --inputbox text  width height
dialog --inputbox "Enter your name:" 8 40 2>answer
dialog --inputbox "Enter your name:" 8 40 2>answer
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Input Box -->


== Menu ==
== Menu ==
<blockquote>
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
## dialog --menu <text> <height> <width>
## dialog --menu <text> <height> <width>
Line 87: Line 94:
result=$(eval "$dialog --menu Choose 10 10 3 $choices --stdout")
result=$(eval "$dialog --menu Choose 10 10 3 $choices --stdout")
</source>
</source>
</blockquote><!-- Menu -->
</blockquote><!-- components -->
</blockquote><!-- components -->

Revision as of 18:56, 13 August 2021

The dialog program provides shell-access to the curses library.

Documentation

man dialog https://man.archlinux.org/man/dialog.1

Tutorials

https://www.linuxjournal.com/article/2807

return values

Dialog saves it's result to stderr by default. You can change this using the --stdout parameter.

VAR=$(dialog --menu 'Choose' 10 10 10 \
        0 apples \
        1 oranges \
        2 kiwis)

See stack overflow for details of saving dialog results to a variable.

This is ackward. Bash does not support return codes other than 0 or 1. Dialog, by default outputs it's result on stderr (so that it can be redirected into a file then read). To skip the ackward in between step, you can do this:

## __NOTE__: the backslashes are mandatory here
result=$(                                            \
  dialog --title "Create Directory"                  \
         --inputbox "Enter the directory name:" 8 40 \
  3>&1 1>&2 2>&3 3>&-                                \
)
echo $result

components

MessageBox

dialog --title 'Message' --msgbox 'Hello, world!' 5 20

Yes/No Prompt

dialog --title "Message"  --yesno "Are you having\ fun?" 6 25

Input Box

# dialog --inputbox text   width height
dialog --inputbox "Enter your name:" 8 40 2>answer

Menu

## dialog --menu <text> <height> <width>
dialog --menu "Choose one:" \
  10 30 3  `#height width menu-height` \
  0 red \
  1 green\
  2 blue

Here's a real example

colourschemes=(soda hybrid diokai)
choices=""

for i in ${!colourschemes[@]} ; do
    choices+=" $i ${colourschemes[$i]}"
done

result=$(eval "$dialog --menu Choose 10 10 3 $choices --stdout")