2018-06-20 06:07:58 +02:00
|
|
|
#!/data/data/com.termux/files/usr/bin/bash
|
2017-04-09 18:19:29 +02:00
|
|
|
set -e -u
|
|
|
|
|
2018-06-20 06:07:58 +02:00
|
|
|
DEFAULT_WIDGET="text"
|
|
|
|
|
2017-04-09 18:19:29 +02:00
|
|
|
SCRIPTNAME=termux-dialog
|
2018-06-20 06:07:58 +02:00
|
|
|
show_usage() {
|
|
|
|
echo "Usage: $SCRIPTNAME widget [options]"
|
2018-06-20 22:19:18 +02:00
|
|
|
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)"
|
2017-04-09 18:19:29 +02:00
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2018-06-21 03:00:38 +02:00
|
|
|
declare -a widgets=("confirm" "checkbox" "counter" "date" "radio" "sheet" "spinner" "speech" "text" "time")
|
2018-06-20 06:07:58 +02:00
|
|
|
|
|
|
|
# Descriptions for various options that multiple widgets can use
|
|
|
|
OPT_HINT_DESC="[-i hint] text hint (optional)"
|
2018-06-20 22:02:17 +02:00
|
|
|
OPT_MULTI_LINE_DESC="[-m] multiple lines instead of single (optional)"
|
|
|
|
OPT_PASS_DESC="[-p] enter input as password (optional)"
|
2018-06-21 02:35:07 +02:00
|
|
|
OPT_NUMERIC_DESC="[-n] enter input as numbers (optional)"
|
2018-06-20 22:02:17 +02:00
|
|
|
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)"
|
2018-07-30 02:43:50 +02:00
|
|
|
OPT_DATEFORMAT_DESC="[-d \"dd-MM-yyyy k:m:s\"] SimpleDateFormat Pattern for date widget output (optional)"
|
2017-04-09 18:19:29 +02:00
|
|
|
|
2018-06-20 06:07:58 +02:00
|
|
|
# Widget hints
|
2017-04-09 18:19:29 +02:00
|
|
|
ARG_I=""
|
|
|
|
OPT_I=""
|
2018-06-20 06:07:58 +02:00
|
|
|
|
|
|
|
# Text widget multiline
|
2017-04-09 18:19:29 +02:00
|
|
|
ARG_M=""
|
2018-06-21 02:35:07 +02:00
|
|
|
OPT_M=""
|
|
|
|
|
|
|
|
# Text widget numeric
|
|
|
|
ARG_N=""
|
|
|
|
OPT_N=""
|
2018-06-20 06:07:58 +02:00
|
|
|
|
|
|
|
# Counter range
|
|
|
|
ARG_R=""
|
|
|
|
OPT_R=""
|
|
|
|
|
|
|
|
# Text widget input as password
|
2017-04-09 18:19:29 +02:00
|
|
|
ARG_P=""
|
2018-06-21 02:35:07 +02:00
|
|
|
OPT_P=""
|
2018-06-20 06:07:58 +02:00
|
|
|
|
|
|
|
# Dialog title (supported by all widgets)
|
2017-04-09 18:19:29 +02:00
|
|
|
ARG_T=""
|
|
|
|
OPT_T=""
|
2018-06-20 06:07:58 +02:00
|
|
|
|
|
|
|
# Values for list-type widgets
|
2018-05-28 01:20:26 +02:00
|
|
|
OPT_V=""
|
|
|
|
ARG_V=""
|
2017-04-09 18:19:29 +02:00
|
|
|
|
2018-07-31 16:49:46 +02:00
|
|
|
# Date output format (supported by date widget)
|
2018-07-30 02:43:50 +02:00
|
|
|
ARG_D=""
|
|
|
|
OPT_D=""
|
|
|
|
|
2018-06-20 06:07:58 +02:00
|
|
|
# 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
|
2018-06-21 02:35:07 +02:00
|
|
|
NUM_FLAG=32
|
2018-06-20 06:07:58 +02:00
|
|
|
|
|
|
|
FLAGS=0
|
|
|
|
|
|
|
|
|
|
|
|
# Show usage help for specific widget
|
|
|
|
widget_usage () {
|
2018-06-20 21:51:26 +02:00
|
|
|
echo -n -e "$1 - "
|
2018-06-20 06:07:58 +02:00
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
"confirm")
|
2018-06-20 21:51:26 +02:00
|
|
|
echo "Show confirmation dialog"
|
|
|
|
echo " $OPT_HINT_DESC"
|
2018-06-21 02:54:25 +02:00
|
|
|
echo " $OPT_TITLE_DESC"
|
2018-06-20 06:07:58 +02:00
|
|
|
;;
|
|
|
|
"checkbox")
|
2018-06-20 21:51:26 +02:00
|
|
|
echo "Select multiple values using checkboxes"
|
|
|
|
echo " $OPT_VALUES_DESC"
|
|
|
|
echo " $OPT_TITLE_DESC"
|
2018-06-20 06:07:58 +02:00
|
|
|
;;
|
|
|
|
"counter")
|
2018-06-20 21:51:26 +02:00
|
|
|
echo "Pick a number in specified range"
|
|
|
|
echo " $OPT_RANGE_DESC"
|
2018-06-21 02:54:25 +02:00
|
|
|
echo " $OPT_TITLE_DESC"
|
2018-06-20 06:07:58 +02:00
|
|
|
;;
|
|
|
|
"date")
|
2018-06-20 21:51:26 +02:00
|
|
|
echo "Pick a date"
|
|
|
|
echo " $OPT_TITLE_DESC"
|
2018-07-30 02:43:50 +02:00
|
|
|
echo " $OPT_DATEFORMAT_DESC"
|
2018-06-20 06:07:58 +02:00
|
|
|
;;
|
|
|
|
"radio")
|
2018-06-20 21:51:26 +02:00
|
|
|
echo "Pick a single value from radio buttons"
|
|
|
|
echo " $OPT_VALUES_DESC"
|
|
|
|
echo " $OPT_TITLE_DESC"
|
2018-06-20 06:07:58 +02:00
|
|
|
;;
|
|
|
|
"sheet")
|
2018-06-20 22:02:17 +02:00
|
|
|
echo "Pick a value from sliding bottom sheet"
|
2018-06-20 21:51:26 +02:00
|
|
|
echo " $OPT_VALUES_DESC"
|
|
|
|
echo " $OPT_TITLE_DESC"
|
2018-06-20 06:07:58 +02:00
|
|
|
;;
|
2018-06-21 03:00:38 +02:00
|
|
|
"speech")
|
|
|
|
echo "Obtain speech using device microphone"
|
|
|
|
echo " $OPT_HINT_DESC"
|
|
|
|
echo " $OPT_TITLE_DESC"
|
|
|
|
;;
|
2018-06-20 06:07:58 +02:00
|
|
|
"spinner")
|
2018-06-20 21:51:26 +02:00
|
|
|
echo "Pick a single value from a dropdown spinner"
|
|
|
|
echo " $OPT_VALUES_DESC"
|
|
|
|
echo " $OPT_TITLE_DESC"
|
2018-06-20 06:07:58 +02:00
|
|
|
;;
|
|
|
|
"text")
|
2018-06-20 21:51:26 +02:00
|
|
|
echo "Input text (default if no widget specified)"
|
|
|
|
echo " $OPT_HINT_DESC"
|
2018-06-20 22:02:17 +02:00
|
|
|
echo " $OPT_MULTI_LINE_DESC*"
|
2018-06-21 02:35:07 +02:00
|
|
|
echo " $OPT_NUMERIC_DESC*"
|
|
|
|
echo " $OPT_PASS_DESC"
|
2018-06-21 02:54:25 +02:00
|
|
|
echo " $OPT_TITLE_DESC"
|
2018-06-21 02:35:07 +02:00
|
|
|
echo " * cannot use [-m] with [-n]"
|
2018-06-20 06:07:58 +02:00
|
|
|
;;
|
|
|
|
"time")
|
2018-06-20 21:51:26 +02:00
|
|
|
echo "Pick a time value"
|
2018-06-20 22:16:30 +02:00
|
|
|
echo " $OPT_TITLE_DESC"
|
2018-06-20 06:07:58 +02:00
|
|
|
;;
|
|
|
|
*)
|
2018-06-20 21:51:26 +02:00
|
|
|
echo "\tUnknown usage for '$1'"
|
2018-06-20 06:07:58 +02:00
|
|
|
;;
|
|
|
|
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() {
|
2018-07-30 02:43:50 +02:00
|
|
|
while getopts :hlmnpr:i:t:d:v: option
|
2018-06-20 06:07:58 +02:00
|
|
|
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; ;;
|
2018-06-21 02:35:07 +02:00
|
|
|
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; ;;
|
2018-06-20 06:07:58 +02:00
|
|
|
t) ARG_T="--es input_title"; OPT_T="$OPTARG" ;;
|
2018-07-30 02:43:50 +02:00
|
|
|
d) ARG_D="--es date_format"; OPT_D="$OPTARG";;
|
2018-06-20 06:07:58 +02:00
|
|
|
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
|
2018-06-20 19:23:12 +02:00
|
|
|
WIDGET="$DEFAULT_WIDGET"
|
2018-06-20 06:07:58 +02:00
|
|
|
else
|
2018-06-20 19:23:12 +02:00
|
|
|
WIDGET="$1"
|
2018-06-20 06:07:58 +02:00
|
|
|
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
|
|
|
|
|
2018-06-21 02:35:07 +02:00
|
|
|
if [ $((FLAGS & MULTI_LINE_FLAG)) -ne 0 ] && [ $((FLAGS & NUM_FLAG)) -ne 0 ]; then
|
2018-06-20 06:07:58 +02:00
|
|
|
options_error $WIDGET
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
|
|
|
"confirm")
|
2018-06-21 02:35:07 +02:00
|
|
|
if [ $((FLAGS & (MULTI_LINE_FLAG | PASS_FLAG | RANGE_FLAG | VALUES_FLAG | NUM_FLAG))) -ne 0 ]; then
|
2018-06-20 06:07:58 +02:00
|
|
|
options_error $WIDGET
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
|
|
|
"counter")
|
2018-06-21 02:35:07 +02:00
|
|
|
if [ $((FLAGS & (MULTI_LINE_FLAG | PASS_FLAG | VALUES_FLAG | NUM_FLAG))) -ne 0 ]; then
|
2018-06-20 06:07:58 +02:00
|
|
|
options_error $WIDGET
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
|
2018-06-21 03:00:38 +02:00
|
|
|
"speech")
|
|
|
|
if [ $((FLAGS & (MULTI_LINE_FLAG | PASS_FLAG | RANGE_FLAG | VALUES_FLAG | NUM_FLAG))) -ne 0 ]; then
|
|
|
|
options_error $WIDGET
|
2018-06-20 06:07:58 +02:00
|
|
|
fi
|
2018-06-21 03:00:38 +02:00
|
|
|
;;
|
2018-06-20 06:07:58 +02:00
|
|
|
|
2018-06-21 03:00:38 +02:00
|
|
|
"date" | "time")
|
2018-06-21 02:35:07 +02:00
|
|
|
if [ $((FLAGS & (HINT_FLAG | MULTI_LINE_FLAG | PASS_FLAG | RANGE_FLAG | VALUES_FLAG | NUM_FLAG))) -ne 0 ]; then
|
2018-06-20 06:07:58 +02:00
|
|
|
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
|
|
|
|
|
2018-06-21 02:35:07 +02:00
|
|
|
if [ $((FLAGS & (HINT_FLAG | MULTI_LINE_FLAG | PASS_FLAG | RANGE_FLAG | NUM_FLAG))) -ne 0 ]; then
|
2018-06-20 06:07:58 +02:00
|
|
|
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
|
|
|
|
|
2018-05-28 01:20:26 +02:00
|
|
|
ARG_W="--es input_method"
|
|
|
|
|
2018-06-20 06:07:58 +02:00
|
|
|
# Set options, ensuring whitespace isn't lost
|
|
|
|
if [ -n "$ARG_W" ]; then set -- "$@" $ARG_W "$WIDGET"; fi
|
2017-04-09 18:19:29 +02:00
|
|
|
if [ -n "$ARG_I" ]; then set -- "$@" $ARG_I "$OPT_I"; fi
|
|
|
|
if [ -n "$ARG_T" ]; then set -- "$@" $ARG_T "$OPT_T"; fi
|
2018-06-20 06:07:58 +02:00
|
|
|
if [ -n "$ARG_R" ]; then set -- "$@" $ARG_R "$OPT_R"; fi
|
2018-05-28 01:20:26 +02:00
|
|
|
if [ -n "$ARG_V" ]; then set -- "$@" $ARG_V "$OPT_V"; fi
|
2018-06-21 02:35:07 +02:00
|
|
|
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
|
2018-07-30 02:43:50 +02:00
|
|
|
if [ -n "$ARG_D" ]; then set -- "$@" $ARG_D "$OPT_D"; fi
|
2017-04-09 18:19:29 +02:00
|
|
|
|
2017-04-09 19:00:20 +02:00
|
|
|
/data/data/com.termux/files/usr/libexec/termux-api Dialog "$@"
|