-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathask
89 lines (78 loc) · 1.66 KB
/
ask
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
if shellm-ndef; then
shellm-define
## \brief Provide useful function to get stdin content.
shellm source ui/auto.sh
shellm source message.sh
shellm_ask_confirm=
shellm_ask_timeout=-1
set_ask_timeout() {
shellm_ask_timeout=${1:-${shellm_ask_timeout}}
}
unset_ask_timeout() {
shellm_ask_timeout=-1
}
ask_cli() {
local a timeout
[ ${shellm_ask_timeout:--1} -ne -1 ] &&
timeout="-t ${shellm_ask_timeout}"
# shellcheck disable=SC2086
if read ${timeout} -p "$* " a; then
case $a in
[Yy]|[Yy][Ee][Ss]) return 0 ;;
[Nn]|[Nn][Oo]) return 1 ;;
[Aa]|[Yy][Aa]|[Yy][Aa][Ll][Ll]) return 2 ;;
[Qq]|[Nn][Aa]|[Nn][Aa][Ll][Ll]) return 3 ;;
*) return 1 ;;
esac
else
echo
return 1
fi
}
ask_gui() {
local timeout yad
[ ${shellm_ask_timeout:--1} -ne -1 ] &&
timeout="--timeout=${shellm_ask_timeout}"
yad="/usr/bin/yad ${timeout} \
--question \
--text '$*'"
[ -n "${shellm_ask_confirm}" ] &&
yad="$yad --button 'Yes:0' \
--button 'No:1' \
--button 'Yes to all:2' \
--button 'No to all:3'"
eval "$yad"
[ $? -eq 70 ] && return 1
}
activate_ask_extra() {
shellm_ask_confirm=help
}
deactivate_ask_extra() {
shellm_ask_confirm=
}
ask() {
case ${shellm_ask_confirm} in
"") auto_ui "ask_cli '$*'" "ask_gui '$*'"
case $? in
0|2) return 0 ;;
1|3) return 1 ;;
esac
;;
help)
err "*** y:yes / n:no / a:yes-to-all / q:no-to-all"
shellm_ask_confirm=ask
ask "$@"
;;
ask)
auto_ui "ask_cli '$*'" "ask_gui '$*'"
case $? in
0|1) return $? ;;
2) shellm_ask_confirm=yes-to-all; return 0 ;;
3) shellm_ask_confirm=no-to-all; return 1 ;;
esac
;;
yes-to-all) return 0 ;;
no-to-all) return 1 ;;
esac
}
fi # __IO_ASK_SH