-
Notifications
You must be signed in to change notification settings - Fork 0
/
input-dialog
executable file
·43 lines (38 loc) · 1.18 KB
/
input-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
#!/usr/bin/env bash
# prompts the user with a basic dialog
command -v tttlog >/dev/null 2>&1 && tttlog "$(basename "$0")" "$@"
usage() {
echo 'Prompts user with an OS dialog, prints what they typed to STDOUT'
exit "${1:-0}"
}
[[ "$1" == "-h" ]] && usage
[[ -z "$1" ]] && {
echo -e "Pass the prompt string as the first argument\n" 2>&1
usage 1
}
case "${ON_OS:=$(on_machine)}" in
linux*)
havecmd -v rofi || exit $?
# use rofi with no list on linux
rofi -dmenu -theme-str 'listview {enabled: false;}' -p "$1" || exit $?
;;
mac*)
# use applescript on mac
OSASCRIPT="$(printf 'set T to text returned of (display dialog "%s" buttons {"Cancel", "OK"} default button "OK" default answer "")' "$1")"
RESP="$(osascript -e "$OSASCRIPT" 2>/dev/null)" || exit $?
[[ -z "$RESP" ]] && exit 1
printf '%s' "$RESP"
;;
android_termux*)
havecmd -V 'See https://stedolan.github.io/jq/' jq || exit $?
havecmd -V 'install the termux API package: https://github.com/termux/termux-api' termux-dialog || exit $?
RESP="$(termux-dialog -t "$1")"
CODE="$(jq -r .code <<<"$RESP")"
[[ "$CODE" != -1 ]] && exit 1
jq -r .text <<<"$RESP"
;;
*)
echo 'Not able to prompt for input...' >&2
printf '%s\n' "$1"
;;
esac