-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtermux-dialog
executable file
·281 lines (242 loc) · 7.8 KB
/
termux-dialog
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/data/data/com.termux/files/usr/bin/bash
set -e -u
DEFAULT_WIDGET="text"
SCRIPTNAME=termux-dialog
show_usage() {
echo "Usage: $SCRIPTNAME widget [options]"
echo "Get user input w/ different widgets! Default: $DEFAULT_WIDGET"
echo " -h, help Show this help"
echo " -l, list List all widgets and their options"
echo " -t, title Set title of input dialog (optional)"
exit 0
}
declare -a widgets=("confirm" "checkbox" "counter" "date" "radio" "sheet" "spinner" "speech" "text" "time")
# Descriptions for various options that multiple widgets can use
OPT_HINT_DESC="[-i hint] text hint (optional)"
OPT_MULTI_LINE_DESC="[-m] multiple lines instead of single (optional)"
OPT_PASS_DESC="[-p] enter input as password (optional)"
OPT_NUMERIC_DESC="[-n] enter input as numbers (optional)"
OPT_TITLE_DESC="[-t title] set title of dialog (optional)"
OPT_RANGE_DESC="[-r min,max,start] comma delim of (3) numbers to use (optional)"
OPT_VALUES_DESC="[-v \",,,\"] comma delim values to use (required)"
OPT_DATEFORMAT_DESC="[-d \"dd-MM-yyyy k:m:s\"] SimpleDateFormat Pattern of date/time widget output (optional)"
# Widget hints
ARG_I=""
OPT_I=""
# Text widget multiline
ARG_M=""
OPT_M=""
# Text widget numeric
ARG_N=""
OPT_N=""
# Counter range
ARG_R=""
OPT_R=""
# Text widget input as password
ARG_P=""
OPT_P=""
# Dialog title (supported by all widgets)
ARG_T=""
OPT_T=""
# Values for list-type widgets
OPT_V=""
ARG_V=""
# Date output format (supported by date and time widgets)
ARG_D=""
OPT_D=""
# Widget type
ARG_W=""
WIDGET=""
# Flags for detecting invalid option combinations
HINT_FLAG=1
MULTI_LINE_FLAG=2
PASS_FLAG=4
RANGE_FLAG=8
VALUES_FLAG=16
NUM_FLAG=32
FLAGS=0
# Show usage help for specific widget
widget_usage () {
echo -n -e "$1 - "
case "$1" in
"confirm")
echo "Show confirmation dialog"
echo " $OPT_HINT_DESC"
echo " $OPT_TITLE_DESC"
;;
"checkbox")
echo "Select multiple values using checkboxes"
echo " $OPT_VALUES_DESC"
echo " $OPT_TITLE_DESC"
;;
"counter")
echo "Pick a number in specified range"
echo " $OPT_RANGE_DESC"
echo " $OPT_TITLE_DESC"
;;
"date")
echo "Pick a date"
echo " $OPT_TITLE_DESC"
echo " $OPT_DATEFORMAT_DESC"
;;
"radio")
echo "Pick a single value from radio buttons"
echo " $OPT_VALUES_DESC"
echo " $OPT_TITLE_DESC"
;;
"sheet")
echo "Pick a value from sliding bottom sheet"
echo " $OPT_VALUES_DESC"
echo " $OPT_TITLE_DESC"
;;
"speech")
echo "Obtain speech using device microphone"
echo " $OPT_HINT_DESC"
echo " $OPT_TITLE_DESC"
;;
"spinner")
echo "Pick a single value from a dropdown spinner"
echo " $OPT_VALUES_DESC"
echo " $OPT_TITLE_DESC"
;;
"text")
echo "Input text (default if no widget specified)"
echo " $OPT_HINT_DESC"
echo " $OPT_MULTI_LINE_DESC*"
echo " $OPT_NUMERIC_DESC*"
echo " $OPT_PASS_DESC"
echo " $OPT_TITLE_DESC"
echo " * cannot use [-m] with [-n]"
;;
"time")
echo "Pick a time value"
echo " $OPT_TITLE_DESC"
echo " $OPT_DATEFORMAT_DESC"
;;
*)
echo "\tUnknown usage for '$1'"
;;
esac
}
# List all widgets
list_widgets() {
echo "Supported widgets:"
echo
for w in "${widgets[@]}"; do
widget_usage $w
echo
done
}
# Checks to see if widgets array contains specified widget
has_widget () {
for w in "${widgets[@]}"; do
[ "$w" == "$1" ] && return 0
done
return 1
}
set_flag () {
FLAGS=$((FLAGS | $1));
}
# Convenience method to get all supported options, regardless of specified widget
# NOTE: Option combination validation doesn't occur here
parse_options() {
while getopts :hlmnpr:i:t:d:v: option
do
case "$option" in
h) show_usage ;;
l) list_widgets; exit 0;;
i) ARG_I="--es input_hint"; OPT_I="$OPTARG"; set_flag $HINT_FLAG; ;;
m) ARG_M="--ez multiple_lines"; OPT_M="true"; set_flag $MULTI_LINE_FLAG; ;;
p) ARG_P="--ez password"; OPT_P="true"; set_flag $PASS_FLAG; ;;
n) ARG_N="--ez numeric"; OPT_N="true"; set_flag $NUM_FLAG; ;;
t) ARG_T="--es input_title"; OPT_T="$OPTARG" ;;
d) ARG_D="--es date_format"; OPT_D="$OPTARG";;
v) ARG_V="--es input_values"; OPT_V="$OPTARG"; set_flag $VALUES_FLAG; ;;
r) ARG_R="--eia input_range"; OPT_R=$OPTARG; set_flag $RANGE_FLAG; ;;
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
esac
done
}
options_error () {
echo -e "ERROR: Invalid option(s) detected for '$1'\n"
echo "Usage '$1'"
widget_usage $1
exit 1
}
if [ $# -eq 0 ]; then
WIDGET=$DEFAULT_WIDGET
# First argument wasn't a widget
elif ! has_widget $1; then
# we didn't receive a widget as an argument, check to see if we
# at least options (even if they're illegal)
if ! [[ $1 =~ -[a-z] ]]; then
echo -e "ERROR: Illegal argument $1\n"
show_usage
fi
WIDGET="$DEFAULT_WIDGET"
else
WIDGET="$1"
shift
fi
parse_options "$@"
shift $((OPTIND - 1))
# Ensure proper option combinations for the specific type of widget
case "$WIDGET" in
# Text (default) if no widget specified
"text")
if [ $((FLAGS & (RANGE_FLAG | VALUES_FLAG))) -ne 0 ]; then
options_error $WIDGET
fi
if [ $((FLAGS & MULTI_LINE_FLAG)) -ne 0 ] && [ $((FLAGS & NUM_FLAG)) -ne 0 ]; then
options_error $WIDGET
fi
;;
"confirm")
if [ $((FLAGS & (MULTI_LINE_FLAG | PASS_FLAG | RANGE_FLAG | VALUES_FLAG | NUM_FLAG))) -ne 0 ]; then
options_error $WIDGET
fi
;;
"counter")
if [ $((FLAGS & (MULTI_LINE_FLAG | PASS_FLAG | VALUES_FLAG | NUM_FLAG))) -ne 0 ]; then
options_error $WIDGET
fi
;;
"speech")
if [ $((FLAGS & (MULTI_LINE_FLAG | PASS_FLAG | RANGE_FLAG | VALUES_FLAG | NUM_FLAG))) -ne 0 ]; then
options_error $WIDGET
fi
;;
"date" | "time")
if [ $((FLAGS & (HINT_FLAG | MULTI_LINE_FLAG | PASS_FLAG | RANGE_FLAG | VALUES_FLAG | NUM_FLAG))) -ne 0 ]; then
options_error $WIDGET
fi
;;
"checkbox" | "radio" | "sheet" | "spinner")
if [ "$ARG_V" == "" ]; then
echo "ERROR: '$WIDGET' must be called with $OPT_VALUES_DESC"
exit 1
fi
if [ $((FLAGS & (HINT_FLAG | MULTI_LINE_FLAG | PASS_FLAG | RANGE_FLAG | NUM_FLAG))) -ne 0 ]; then
options_error $WIDGET
fi
;;
*)
echo "$SCRIPTNAME: unsupported widget '$WIDGET'"; show_usage ;;
esac
# All valid args should be consumed by this point
if [ $# -gt 0 ]; then
echo "ERROR: Too many arguments!"
show_usage
fi
ARG_W="--es input_method"
# Set options, ensuring whitespace isn't lost
if [ -n "$ARG_W" ]; then set -- "$@" $ARG_W "$WIDGET"; fi
if [ -n "$ARG_I" ]; then set -- "$@" $ARG_I "$OPT_I"; fi
if [ -n "$ARG_T" ]; then set -- "$@" $ARG_T "$OPT_T"; fi
if [ -n "$ARG_R" ]; then set -- "$@" $ARG_R "$OPT_R"; fi
if [ -n "$ARG_V" ]; then set -- "$@" $ARG_V "$OPT_V"; fi
if [ -n "$ARG_M" ]; then set -- "$@" $ARG_M "$OPT_M"; fi
if [ -n "$ARG_P" ]; then set -- "$@" $ARG_P "$OPT_P"; fi
if [ -n "$ARG_N" ]; then set -- "$@" $ARG_N "$OPT_N"; fi
if [ -n "$ARG_D" ]; then set -- "$@" $ARG_D "$OPT_D"; fi
/data/data/com.termux/files/usr/libexec/termux-api Dialog "$@"