-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer
More file actions
executable file
·294 lines (244 loc) · 5.93 KB
/
timer
File metadata and controls
executable file
·294 lines (244 loc) · 5.93 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env bash
# ╺┳╸╻┏┳┓┏━╸┏━┓
# ┃ ┃┃┃┃┣╸ ┣┳┛
# ╹ ╹╹ ╹┗━╸╹┗╸
# set timers
set -eou pipefail
PROG="${0##*/}"
DURATION="${1:-}"
TIMER_NAME="${2:-}"
ICON=""
ICON=""
DEPS=(notify-send dmenu mpv)
# files
SOUND="$HOME"/bin/assets/alarm-beepbeep.mp3
PIDFILE="/tmp/$PROG.pid"
TIMEFILE="/tmp/$PROG.time"
MPVPID="/tmp/$PROG.mpv"
# TODO:
# - [ ] add support for multiple timers (unique names)
function _notifyme {
declare -a notify_args=(-r 888 --icon="alarm-timer")
prog=$(echo "$PROG" | tr '[:lower:]' '[:upper:]')
notify_args+=("$prog")
notify-send "${notify_args[@]}" "$*"
}
function _logerr {
local msg="$1"
printf "%s: %s\n" "$PROG" "$msg" >&2
_notifyme "$msg"
exit 1
}
function _logme {
printf "%s: %s\n" "$PROG" "$1"
}
function _has {
local c
local verbose=false
if [[ $1 == '-v' ]]; then
verbose=true
shift
fi
for c in "$@"; do
c="${c%% *}"
if ! command -v "$c" &>/dev/null; then
[[ "$verbose" == true ]] && _logerr "$c not found"
return 1
fi
done
}
function _checkdeps {
_has -v "${DEPS[@]}"
if [[ ! -e "$SOUND" ]]; then
_logerr "timer file not found: $SOUND"
fi
}
function pre_exit {
local files=("$TIMEFILE" "$PIDFILE")
for f in "${files[@]}"; do
[[ -e "$f" ]] && rm -f "$f"
done
_logme "cleaning up..."
_notifyme "timer finished"
exit 0
}
function usage {
cat <<-EOF
Usage: $PROG [[+|-<duration>]]
Sets timers
Options:
status Show current status
cancel, stop Remove current timer
EOF
}
function _get_random_str {
echo "$RANDOM" | base64 | head -c5 | tr '[:upper:]' '[:lower:]'
}
function _interpret_time {
local input last_char time
if [[ ${#1} -eq 1 ]]; then
input="${1}m"
else
input="$1"
fi
last_char="${input: -1}"
time="${input:0:-1}"
if [[ "$last_char" == "m" ]]; then
time=$((time * 60))
elif [[ "$last_char" == "s" ]]; then
time=$((time))
elif [[ "$last_char" == "h" ]]; then
time=$((time * 60 * 60))
else
_logerr "invalid input: $input"
fi
echo "$time"
}
function _cancel_timer {
if [[ -e "$MPVPID" ]]; then
kill -9 "$(cat "$MPVPID")" &>/dev/null
rm "$MPVPID"
fi
if [[ ! -e "$PIDFILE" ]]; then
_logme "no timer found"
return
fi
_logme "timer '$TIMER_NAME' cancelled"
kill -9 "$(cat "$PIDFILE")" &>/dev/null
pre_exit
}
function _save_pid {
echo "$1" | tee "$PIDFILE" >/dev/null
}
function _save_timestamp {
echo "$1" | tee "$TIMEFILE" >/dev/null
}
function _get_timestamp {
cat "$TIMEFILE"
}
function _update_timestamp {
echo "${1:-0}" | tee "$TIMEFILE" >/dev/null
}
function _update_time {
local time new_time current
if [[ ! -e "$TIMEFILE" ]]; then
err_msg="no timer found"
_notifyme "$err_msg"
_logerr "$err_msg"
fi
time=$(_interpret_time "${1:-0}")
current=$(_get_timestamp)
new_time=$((current + time))
_update_timestamp "$new_time"
}
function _show_status {
local current time result
local time_diff_hours time_diff_minutes time_diff_seconds
if [[ ! -f "$TIMEFILE" ]]; then
return 1
fi
current=$(date +%s)
time=$(_get_timestamp)
time_diff=$((time - current))
if [[ "$time_diff" -lt 0 ]]; then
return 1
fi
time_diff=${time_diff#-}
if [[ $time_diff -ge 3600 ]]; then
time_diff_hours=$((time_diff / 3600))
time_diff=$((time_diff % 3600))
time_diff_minutes=$((time_diff / 60))
time_diff_seconds=$((time_diff % 60))
result="${time_diff_hours}h ${time_diff_minutes}m ${time_diff_seconds}s"
elif [[ $time_diff -ge 60 ]]; then
time_diff_minutes=$((time_diff / 60))
time_diff_seconds=$((time_diff % 60))
result="${time_diff_minutes}m ${time_diff_seconds}s"
else
result="${time_diff}s"
fi
echo "$result"
}
function _sound_the_alarm {
_notifyme "timer <b>'$TIMER_NAME'</b> is up!!!"
mpv "$SOUND" --mute=no --loop-file=10 --no-resume-playback >/dev/null 2>&1 &
mpv_pid=$!
echo "$mpv_pid" >"$MPVPID"
}
function _validate_input {
local input="$1"
if [[ $input =~ [0-9] ]]; then
return 0
else
return 1
fi
}
function _set_timer {
trap pre_exit EXIT SIGTERM SIGINT
local duration="$1"
local name="$2"
local current current_time
current=$(date +%s)
duration=$(_interpret_time "$duration")
current_time=$((current + duration))
_save_timestamp "$current_time"
_notifyme "setting $(_show_status) timer for '$name'"
while true; do
current=$(date +%s)
time=$(_get_timestamp)
time_diff=$((current - time))
time_diff=${time_diff#-}
if [[ $time_diff -le 0 ]]; then
break
fi
sleep 1s
done
_sound_the_alarm
}
function _get_input {
local data
local prompt="$1"
data=$(: | dmenu -i -p "$ICON $prompt")
echo "$data"
}
function _get_duration {
local icon=""
DURATION=$(: | dmenu -i -p "$icon Duration:")
echo "$DURATION"
}
function parse_and_exit {
case "$1" in
status)
_show_status
exit 0
;;
-h | --help | help)
usage
exit 1
;;
cancel | stop | kill)
_cancel_timer
;;
+* | -*)
_update_time "$@"
exit 0
;;
*) ;;
esac
}
function main {
local opt="${1:-}"
parse_and_exit "$opt"
_checkdeps
if [[ -z "$DURATION" ]]; then
DURATION=$(_get_input "Duration>")
fi
[[ -z "$DURATION" ]] && exit 1
if ! _validate_input "$DURATION"; then
_logerr "invalid duration: $DURATION"
fi
[[ -z "$TIMER_NAME" ]] && TIMER_NAME="${DURATION}-$(_get_random_str)"
_save_pid "$$" "$TIMER_NAME"
_set_timer "$DURATION" "$TIMER_NAME"
}
main "$@"