easydialog: allow form fields input to grow up to 100 chars (Closes: #562249)
[matthijs/upstream/backupninja.git] / lib / easydialog.in
1 #!@BASH@
2 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
4
5 # copyright 2002 lmoore@tump.com under the terms of the GNU LGPL.
6 # additions 2005 collective@riseup.net
7
8 # whiptail has trouble being called in the foo=$(whiptail ...) fashion for
9 # some reason.  this is very annoying.  this means that we need to use
10 # temporary files to store the answers from the input and list based boxes
11 # and then read the answers into a REPLY variable.  that just really
12 # stinks, oh well, that's what you get when you have a weak link
13 # implementation...
14 #
15 # inputBox and passwordBox could be refactored to use a common function
16
17 test -z "$WIDTH" && WIDTH=0
18 test -z "$HEIGHT" && HEIGHT=0
19 BACKTITLE=""
20 DIALOG=dialog
21 HELP=
22
23 setApplicationTitle() {
24    BACKTITLE=$*
25 }
26
27 setHelp() {
28    HELP="$@"
29 }
30
31 setDimension() {
32    WIDTH=$1
33    HEIGHT=$2
34 }
35
36 booleanBox() {
37    $DIALOG --backtitle "$BACKTITLE" --title "$1" \
38       `[ "$3" == no ] && echo '--defaultno'` --yesno "$2" $HEIGHT $WIDTH
39 }
40
41 msgBox() {
42    $DIALOG --backtitle "$BACKTITLE" --title "$1" \
43       --msgbox "$2" $HEIGHT $WIDTH
44 }
45
46 gaugeBox() {
47    $DIALOG --backtitle "$BACKTITLE" --title "$1" \
48       --gauge "$2" $HEIGHT $WIDTH 0
49 }
50
51 inputBox() {
52    local temp=$(@MKTEMP@ -t backupninja.XXXXXX) || exit 1
53    trap "rm -f $temp" 0
54    REPLY=
55    $DIALOG --backtitle "$BACKTITLE" --title "$1" \
56       --inputbox "$2" $HEIGHT $WIDTH "$3" 2> $temp
57    local status=$?
58    [ $status = 0 ] && REPLY=$(cat $temp)
59    rm -f $temp
60    return $status
61 }
62
63 # Xdialog and {dialog,whiptail} use different mechanism to "qoute" the
64 # values from a checklist.  {dialog,whiptail} uses standard double quoting
65 # while Xdialog uses a "/" as the separator.  the slash is arguably better,
66 # but the double quoting is more standard.  anyway, this function can be
67 # overridden to allow a derived implementation to change it's quoting
68 # mechanism to the standard double-quoting one.  it receives two
69 # arguements, the file that has the data and the box type.
70 _listReplyHook() {
71    cat $1
72 }
73
74 # this is the base implementation of all the list based boxes, it works
75 # out nicely that way.  the real function just passes it's arguments to
76 # this function with an extra argument specifying the actual box that
77 # needs to be rendered.
78 _genericListBox() {
79    local box=$1
80    shift 1
81    local title=$1
82    local text=$2
83    shift 2
84    local temp=$(@MKTEMP@ -t backupninja.XXXXXX) || exit 1
85    trap "rm -f $temp" 0
86    REPLY=
87    $DIALOG $HELP $_DEFAULT --backtitle "$BACKTITLE" --title "$title" \
88       $box "$text" $HEIGHT $WIDTH 10 \
89       "$@" 2> $temp
90    local status=$?
91    [ $status = 0 ] && REPLY=$(_listReplyHook $temp $box)
92    rm -f $temp
93    _DEFAULT=
94    return $status
95 }
96
97 setDefault() {
98    _DEFAULT="--default-item $1"
99 }
100
101 menuBox() {
102    _genericListBox --menu "$@"
103 }
104
105 ## a menu box with additional help info displayed
106 ## at the bottom of the window when an item is selected
107 menuBoxHelp() {
108    HELP="--item-help"
109    _genericListBox --menu "$@"
110    status=$?
111    HELP=
112    return $status
113 }
114
115 ## a menu box with an addition button 'help'
116 menuBoxHelpFile() {
117    HELP="--help-button"
118    _genericListBox --menu "$@"
119    status=$?
120    HELP=
121    return $status
122 }
123
124 checkBox() {
125    _genericListBox --checklist "$@"
126 }
127
128 radioBox() {
129    _genericListBox --radiolist "$@"
130 }
131
132 textBox() {
133    $DIALOG --backtitle "$BACKTITLE" --title "$1" --textbox "$2" $HEIGHT $WIDTH
134 }
135
136 passwordBox() {
137    local temp=$(@MKTEMP@ -t backupninja.XXXXXX) || exit 1
138    trap "rm -f $temp" 0
139    REPLY=
140    $DIALOG --backtitle "$BACKTITLE" --title "$1" \
141       --passwordbox "$2" $HEIGHT $WIDTH 2> $temp
142    local status=$?
143    [ $status = 0 ] && REPLY=$(cat $temp)
144    rm -f $temp
145    return $status
146 }
147
148
149 #########################################################
150 ## begin-item-display style lists
151 ##
152 ## these lists are built by calling fuctions multiple times.
153 ## this can make it easier to build your list in a loop
154 ##
155
156 listBegin() {
157    _menu_title=$1
158    _menu_msg=$2
159    _menu_items=0
160    _menu_text=
161    _menu_labels=
162    _menu_status=
163 }
164
165 listItem() {
166    _menu_labels[$_menu_items]=$1
167    _menu_text[$_menu_items]=$2
168    _menu_status[$_menu_items]=$3 # available only for checklist
169    let "_menu_items += 1"
170 }
171
172
173 ##
174 ## takes one of:
175 ## menu, checklist, radiolist
176 ##
177 listDisplay() {
178    boxtype=$1
179    local temp=$(@MKTEMP@ -t backupninja.XXXXXX) || exit 1
180    trap "rm -f $temp" 0
181
182    local label
183    local text
184    local status
185    (
186       echo -ne " $HELP $_DEFAULT "
187       echo -ne " --backtitle '$BACKTITLE' "
188       echo -ne " --title '$_menu_title' "
189       echo -ne " --$boxtype '$_menu_msg' "
190       echo -ne " $HEIGHT $WIDTH 10 "
191       for ((i=0; i < $_menu_items ; i++)); do
192          label=${_menu_labels[$i]}
193          text=${_menu_text[$i]}
194          status=${_menu_status[$i]}
195          echo -ne " $label '$text' $status "
196       done
197    ) | xargs $DIALOG 2> $temp
198
199    local status=$?
200    REPLY=""
201    [ $status = 0 ] && REPLY=`cat $temp`
202    rm -f $temp
203    _DEFAULT=
204    return $status
205 }
206
207 ####################################################
208 ## FORM
209
210 _form_gap=2
211 formBegin() {
212    _form_title=$1
213    _form_items=0
214    _form_labels=
215    _form_text=
216 }
217
218 formItem() {
219    _form_labels[$_form_items]=$1
220    _form_text[$_form_items]=$2
221    let "_form_items += 1"
222 }
223
224 formDisplay() {
225    local temp=$(@MKTEMP@ -t backupninja.XXXXXX) || exit 1
226
227    max_length=0
228    for ((i=0; i < ${#_form_labels[@]} ; i++)); do
229       label=${_form_labels[$i]}
230       length=`expr length $label`
231       if [ $length -gt $max_length ]; then
232          max_length=$length
233       fi
234    done
235    let "max_length += 2"
236
237    local xpos=1
238    (
239       echo -n -e "--form '$_form_title' 0 0 20"
240       for ((i=0; i < $_form_items ; i++)); do
241          label=${_form_labels[$i]}
242          text=${_form_text[$i]}
243          echo -n -e " $label $xpos 1 '$text' $xpos $max_length 30 100"
244          let "xpos += _form_gap"
245       done
246    ) | xargs $DIALOG 2> $temp
247    local status=$?
248
249    ##
250    ## the exit status is meaningless, it is always 0.
251    ## i can't figure out how to get the exit status of dialog
252    ## if we do "dialog `arg code`" or "dialog $args", then the quotes
253    ## get messed up and dialog won't run.
254    ## if we do "(arg code) | xargs dialog", then the exit status is
255    ## swallowed by xargs. xargs should return different exit status
256    ## depending on the exit status of the command run, but i have
257    ## never been able to get that to work.
258    ##
259
260    REPLY=
261    if [ $status = 0 ]; then
262       IFS=$''
263       REPLY=`cat $temp`
264       IFS=$' \t\n'
265    fi
266    rm -f $temp
267    return $status
268 }