-
Notifications
You must be signed in to change notification settings - Fork 2
/
alert
executable file
·103 lines (90 loc) · 2.69 KB
/
alert
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
#!/bin/sh
help() { cat <</help
Pop up an alert
Usage: ${0##*/} [OPTIONS] MESSAGE
-i, --icon=FILE Use this icon beside the text
-n, --no=TEXT Use this negative button text instead of "$cancel"
-t, --title=TEXT Use this window title text instead of "$title"
-T, --time=TIME Timeout after TIME (seconds or with units of s, m, or h)
-v, --verbose Also alert to standard output
-y, --yes=TEXT Use this positive button text instead of "$okay"
/help
version
}
version() {
echo "Part of misc-scripts: https://github/adamhotep/misc-scripts"
echo "alert 0.7.20240810.1 copyright 2005+ by Adam Katz, GPL v2+"
exit
}
title="${0##*/}" # the base name of this script
timeout=31536000 # "disable" timeout by setting it to one year
okay=Okay
cancel=Cancel
icon=
we_have() {
command -v "$@" >/dev/null 2>&1
}
die() {
echo "$*" >&2
exit 2
}
needs_arg() { # WARNING: this hard-codes the $OPT variable
if [ -z "$OPTARG" ]; then
die "No arg for --$OPT option"
fi
}
while getopts hi:n:t:T:vVy:-: OPT; do
if [ "$OPT" = - ]; then
OPT="${OPTARG%%=*}" OPTARG="${OPTARG#$OPT}" OPTARG="${OPTARG#=}"
fi
case "$OPT" in
( h | help ) help ;;
( i | icon ) needs_arg; icon="$OPTARG" ;;
( n | no | cancel ) needs_arg; cancel="$OPTARG" ;;
( t | title ) title="$OPTARG" ;;
( T | time* )
needs_arg
case "$OPTARG" in
( *[!0-9]*[0-9smhd] | *[!0-9smhd] ) die "Invalid time '$OPTARG'" ;;
( *[0-9] ) timeout="$OPTARG" ;;
( [0-9]*s ) timeout="${OPTARG%?}" ;;
( [0-9]*m ) timeout="$((${OPTARG%?}*60))" ;;
( [0-9]*h ) timeout="$((${OPTARG%?}*3600))" ;;
( [0-9]*d ) timeout="$((${OPTARG%?}*86400))" ;;
esac ;;
( v | verb* ) VERBOSE=1 ;;
( V | ver* ) version ;;
( y | yes | ok* ) needs_arg; okay="$OPTARG" ;;
esac
done
shift $((OPTIND-1))
TEXT="$*"
[ -z "$DISPLAY" ] && DISPLAY=:0
export DISPLAY
if we_have zenity; then
alert() {
zenity --question --timeout=$timeout --title="$title" \
--ok-label="$okay" --cancel-label="$cancel" --text="$TEXT" \
${icon:+"--icon=$icon"} \
>/dev/null 2>&1
}
elif we_have gxmessage || we_have gmessage; then
alert() {
gxmessage -display $DISPLAY -center -timeout $timeout -title "$title" \
-buttons "$okay:1,$cancel:0" -default "$okay" "$TEXT" \
>/dev/null 2>&1
}
if we_have gmessage && ! we_have gxmessage; then
gxmessage() { gmessage "$@"; }
fi
else
echo "This is just a simple wrapper for zenity or gxmessage or gmessage." >&2
echo "You don't have any of those installed, so you don't get a pop-up." >&2
echo "" >&2
echo "ALERT${1:+: }$*"
exit 127
fi
if [ -n "$VERBOSE" ]; then
echo "ALERT${1:+: }$*"
fi
alert "$@"