18
18
# along with UCK. If not, see <http://www.gnu.org/licenses/>. #
19
19
###################################################################################
21
function find_dialog()
23
if [ ! -z "$DISPLAY" ] ; then
24
DIALOG=`which kdialog`
26
if [ ! -z "$DIALOG" ]; then
29
DIALOG=`which Xdialog`
31
if [ ! -z "$DIALOG" ]; then
36
if [ -z "$DIALOG" ]; then
39
if [ ! -z "$DIALOG" ]; then
45
if [ -z "$DIALOG" ]; then
48
if [ ! -z "$DIALOG" ]; then
53
if [ -z $DIALOG ]; then
54
failure "You need kdialog, Xdialog, dialog or zenity application to run this script, please install it using 'apt-get install packagename' where packagename is 'kdebase-bin' for kdialog, 'xdialog' for dialog, 'dialog' for dialog. If you are using text-mode, you need to install dialog."
58
function dialog_menu()
65
if [ "$DIALOG_TYPE" = "zenity" ]; then
71
$DIALOG --list --text "$DESCRIPTION" --column "" "${PARAMS[@]}" --width=500 --height=400
73
if [ "$DIALOG_TYPE" = "kdialog" ] ; then
78
PARAMS[$i]="$v" #yes, 2 times as kdialog requires key and value
81
$DIALOG --menu "$DESCRIPTION" "${PARAMS[@]}"
90
$DIALOG --stdout --menu "$DESCRIPTION" 20 30 10 "${PARAMS[@]}"
95
function dialog_multi_choice()
100
if [ "$DIALOG_TYPE" = "zenity" ]; then
102
PARAMS="$PARAMS $i $i"
104
$DIALOG --separator $'\n' --list --checklist --multiple --text "$DESCRIPTION" --column "" --column "" $PARAMS --width=500 --height=400
106
if [ "$DIALOG_TYPE" = "kdialog" ] ; then
108
PARAMS="$PARAMS $i $i 0"
110
$DIALOG --separate-output --checklist "$DESCRIPTION" $PARAMS
113
PARAMS="$PARAMS $i Language 0"
115
$DIALOG --stdout --separate-output --checklist "$DESCRIPTION" 20 30 10 $PARAMS
123
function dialog_line_input()
128
if [ "$DIALOG_TYPE" = "zenity" ] ; then
129
$DIALOG --entry --text "$DESCRIPTION" --entry-text "$INITIAL_VALUE"
131
if [ "$DIALOG_TYPE" = "kdialog" ] ; then
132
$DIALOG --inputbox "$DESCRIPTION" "$INITIAL_VALUE"
134
$DIALOG --stdout --inputbox "$DESCRIPTION" 20 30 "$INITIAL_VALUE"
142
function dialog_choose_file()
146
if [ "$DIALOG_TYPE" = "zenity" ] ; then
147
$DIALOG --title "$TITLE" --file-selection "`pwd`/"
149
if [ "$DIALOG_TYPE" = "kdialog" ] ; then
150
$DIALOG --title "$TITLE" --getopenfilename "`pwd`/"
152
$DIALOG --stdout --title "$TITLE" --fselect "`pwd`/" 10 70
157
function dialog_msgbox()
162
if [ "$DIALOG_TYPE" = "zenity" ]; then
163
echo -n "$TEXT" | $DIALOG --title "$TITLE" --text-info --width=500 --height=400
165
$DIALOG --title "$TITLE" --msgbox "$TEXT" 20 80
169
function dialog_question()
174
if [ "$DIALOG_TYPE" = "zenity" ]; then
175
$DIALOG --title "$TITLE" --question --text "$TEXT"
177
$DIALOG --title "$TITLE" --yesno "$TEXT" 20 80
b'\\ No newline at end of file'
22
# This shell library module provides user interface funtionality.
24
# It looks for and uses the best possible UI for shell procedures
25
# and falls back to simple builtin functionality if none is found.
27
# This module has been written so it works in many shells, e.g.:
28
# bash, dash, ash, busybox, zsh, ksh
29
# have been tested. Minimum requirement is a shell that can handle
30
# functions and your typical selection of standard commands.
32
# For similar work in other areas see:
33
# http://sourceforge.net/projects/ui-dialog/
36
# find_dialog() -- determine dialog system to use
37
# dialog_menu(descr, c1, c2, ...) -- solicit a single choice from the user
38
# dialog_multi_choice(descr, c1, c2, ...) -- solicit multiple choices
39
# dialog_line_input(descr, init) -- solicit a single line of input
40
# dialog_choose_file(descr) -- select a file
41
# dialog_msgbox(title, message) -- display a message and wait for input
42
# dialog_question(title, question) -- display a yes/no question
44
# Some notes on cross-shell portability:
45
# - "which" is a zsh builtin that does not work the way we need it. So
46
# we use /usr/bin/which instead.
47
# - Using command substitution within a test expression does not work
48
# quite right in some shells. Using intermediate varaibles instead.
49
# - Expressions are evaluated by expr and arrays are handled via eval.
51
# _dialog_type is set when the module is loaded and defines the type
52
# of user interface. Possible values (in order of preference) are:
53
# kdialog -- use kdialog
54
# zenity -- use zenity
55
# dialog -- use dialog
56
# builtin -- use shell-only builtins
59
# find_dialog -- find the best type of dialog that is usable
61
if [ -n "$DISPLAY" ]; then
62
# Check availability of X windows based dialogs
63
kdialog=`/usr/bin/which kdialog`
64
zenity=`/usr/bin/which zenity`
65
if [ -n "$kdialog" ]; then
67
elif [ -n "$zenity" ]; then
72
if [ -z "$DISPLAY" -o $_dialog_type = builtin ]; then
73
# None of the X windows based dialogs found/usable.
74
dialog=`/usr/bin/which dialog`
75
if [ -n "$dialog" ]; then
81
# dialog_menu -- solicit a single choice from the user
82
# Parameters: descr choice [choice [...]]
83
# Returns: selected choice on stdout
84
# Status: 0 for success, 1 for failure
90
# Create key/value pairs for argument list
94
args="$args $i '$1'"; shift
98
choice=`kdialog --menu "$descr" "$@"`
100
# Convert key returned by dialog to value
101
if expr "$choice" : '[1-9][0-9]*$' >/dev/null; then
102
eval echo \$`expr $choice \* 2`
109
zenity --list --width=500 --height=400 \
110
--column "$descr" "$@"
115
# Create key/value pairs for argument list
117
while [ -n "$1" ]; do
119
args="$args $i '$1'"; shift
123
choice=`dialog --stdout --menu "$descr" 20 30 10 "$@"`
125
# Convert key returned by dialog to value
126
if expr "$choice" : '[1-9][0-9]*$' >/dev/null; then
127
eval echo \$`expr $choice \* 2`
137
while [ $i -lt $# ]; do
139
eval echo "$i\) \$$i" >&2
141
echo -n "Your choice (1..$#)> " >&2
144
# Convert key returned by dialog to value
145
if expr "$choice" : '[1-9][0-9]*$' >/dev/null; then
152
*) echo "$0: internal error: fall through case" >&2; exit 100
157
# dialog_multi_choice -- solicit multiple choices from the user
158
# Parameters: descr choice [choice [...]]
159
# Returns: selected choices on stdout
160
# Status: 0 for success, 1 for failure
161
dialog_multi_choice() {
164
case $_dialog_type in
168
args="$args '$i' '$i' 0"
171
kdialog --separate-output --checklist "$descr" "$@"
177
args="$args '$i' '$i'"
180
zenity --separator $'\n' --list --checklist --multiple --width=500 --height=400 \
181
--text "$descr" --column "" --column "" "$@"
187
args="$args '$i' '' 0"
190
dialog --stdout --separate-output --checklist "$descr" \
199
while [ $i -lt $# ]; do
201
eval echo "$i\) \$$i" >&2
203
echo "Currently selected: $selection" >&2
204
echo -n "Your choice (1..$#, 0 or epmty line if done, -1 to abort) > " >&2
213
if expr "$choice" : '[1-9][0-9]*$' >/dev/null; then
214
: this is what we need
218
[ "$choice" -gt $# ] && continue
220
# Delete if already in selection
223
for chosen in $selection
225
if [ $chosen -eq $choice ]; then
228
nselection="$nselection $chosen"
231
selection="$nselection"
233
# Append if not found
234
if [ $found -eq 0 ]; then
235
selection="$selection $choice"
239
# Create list with choices selected on stdout
240
if [ -n "$selection" ]; then
241
for choice in $selection; do
250
*) echo "$0: internal error: fall through case" >&2; exit 100
255
# dialog_line_input -- negotiate a single line of input from the user
256
# Arguments: descr initial_value
257
# Returns: a single line on stdout
258
# Status: 0 for success, 1 for failure
259
dialog_line_input() {
263
case $_dialog_type in
265
kdialog --inputbox "$descr" "$initial"
269
zenity --entry --text "$descr" --entry-text "$initial"
273
dialog --stdout --inputbox "$descr" 20 30 "$initial"
279
echo -n "($initial)> " >&2
282
"") echo "$initial";;
287
*) echo "$0: internal error: fall through case" >&2; exit 100
294
# dialog_choose_file -- solicit a single file name from the user
296
# Returns: selected file name on stdout
297
# Status: 0 for success, 1 for failure
298
dialog_choose_file() {
301
case $_dialog_type in
303
kdialog --title "$descr" --getopenfilename "`pwd`/"
307
zenity --title "$descr" --file-selection "`pwd`/"
311
dialog --stdout --title "$descr" --fselect "`pwd`/" 10 70
314
builtin) # run in subshell to leave cwd alone for caller
319
echo "Current working directory: `pwd`" >&2
321
echo -n "Please enter file name, empty to abort > " >&2
323
if [ -z "$answ" ]; then
326
if [ -f "$answ" ]; then
330
if [ -d "$answ" ]; then
334
echo "That is not the name of a file. Try again..." >&2
338
*) echo "$0: internal error: fall through case" >&2; exit 100
345
# dialog_msgbox -- Show message and wait for user to acknowledge
346
# Parameters: descr text
353
case $_dialog_type in
355
kdialog --title "$descr" --msgbox "$text"
360
zenity --title "$descr" --text-info --width=500 --height=400
364
dialog --title "$descr" --msgbox "$text" 20 80
371
echo -n "Hit <RETURN> to continue..." >&2
375
*) echo "$0: internal error: fall through case" >&2; exit 100
380
# dialog_question -- solicit a yes/no answer from the user
381
# Parameters: descr question
383
# Status: 0 for success / yes, 1 for failure / no
388
case $_dialog_type in
390
kdialog --title "$descr" --yesno "$text"
394
zenity --title "$descr" --question --text "$text"
398
dialog --title "$descr" --yesno "$text" 20 80
404
echo -n "$text (y/N)> " >&2
412
*) echo "$0: internal error: fall through case" >&2; exit 100