File: easydialog.sh

package info (click to toggle)
lessdisks 0.5.3cvs.20040906-16
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,480 kB
  • ctags: 271
  • sloc: sh: 3,058; makefile: 92
file content (113 lines) | stat: -rwxr-xr-x 3,043 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash

# copyright 2002 lmoore@tump.com under the terms of the GNU LGPL.

# setApplicationTitle text
#   set the application title.  this may be ignored by the implementations
#
# setDimension width height
#   set the width and height of the box.  this may be ignored by the
#   implementations
#
# booleanBox title text
#   issue a yes/no box.  presents two buttons "yes" and "no".  
#   returns 0 on yes, 1 on no, 255 on cancel/error.
#
# msgBox title text
#   show a message in a box
#
# inputBox title text [initial-input]
#   show a text message and text input field.  the optional initial-input
#   may be ignored by implementations.  returns 0 and puts the input in the 
#   variable REPLY input on success and non-zero on failure.
# 
# menuBox title text {tag item}+
#   show a text message and a menu.  returns 0 and puts the tag of the 
#   selected item in the variable REPLY success. returns non-zero on failure.
#
# checkBox title text {tag item status}+
#   show a text message and a list of checkable items.  multiple items can be 
#   checked.  status is either "on" or "off".  returns 0 and puts the tags
#   of all selected items in the variable REPLY. returns non-zero on failure.
#   
# radioBox title text {tag item status}+
#   this is the same as checkBox but only a single item selected at a time.
#
# textBox title file
#   show a file in a text box
#
# passwordBox title text
#   same as inputBox, but the input text won't be echoed.  some 
#   implementations may not support non-echoing input.

# the following is private implementation, users don't need to look past
# here

console_dialogs="dialog whiptail"
x_dialogs="Xdialog"

test -z "$WIDTH" && WIDTH=0
test -z "$HEIGHT" && HEIGHT=0
BACKTITLE=""

setApplicationTitle() {
    BACKTITLE=$*
}

setDimension() {
    WIDTH=$1
    HEIGHT=$2
}

findProg() {
    # this is probably overkill
    which $1 > /dev/null 2> /dev/null
}

testXserver() {
  # attempts to test if user can write to the X server
  if [ -n "$(which xdpyinfo)" ]; then
    # requires xbase-clients
    xdpyinfo &> /dev/null
    return $?
  else
    # assume the best
    return 0
  fi
}

getImplementation() {
    if [ -n "$DISPLAY" ]; then
	# they running X, let's try to use an X based dialog
	testXserver
        if [ "$?" = "0" ]; then
        	for p in $x_dialogs ; do
		  findProg $p && { echo $p; return 0; }
       		done
        fi
    fi

    # no X or no X based implementation
    for p in $console_dialogs ; do
	findProg $p && { echo $p; return 0; }
    done

    # nothing matched, fallback on the command line
    echo "cmdline"
    return 0
}

# if the user specifies a preference, we'll attempt to honor it
if [ -z "$DIALOG" ]; then
    # no preference, we'll do what we can
    DIALOG=$(getImplementation)
fi

case "$DIALOG" in
    Xdialog|xdialog) source easydialog_Xdialog.sh;;
    dialog|whiptail) source easydialog_dialog.sh;;
    cmdline) source easydialog_cmdline.sh;;
    *)  echo "$DIALOG is not a valid easydialog implementation."
        exit 1
        ;;
esac