-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer
More file actions
executable file
·78 lines (69 loc) · 1.71 KB
/
timer
File metadata and controls
executable file
·78 lines (69 loc) · 1.71 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
#!/usr/bin/zsh
SOUNDFILE="/usr/share/sounds/purple/receive.wav"
# print seconds in human-readable way
function prettyPrint() {
# for readability
local seconds=$1
# normalize seconds
local minutes=$(($seconds / 60))
seconds=$(($seconds % 60))
# normalize minutes
local hours=$(($minutes / 60))
minutes=$(($minutes % 60))
# pad with zeroes
echo $hours:${(l:2::0:)${minutes}}:${(l:2::0:)${seconds}}
}
function beep() {
aplay $SOUNDFILE 2&>1 > /dev/null
}
function usage() {
echo "Usage: ${0##*/} [-s] <hours>h<minutes>m<seconds>s [<description>]"
}
SOUND=false
while getopts ":s" opt; do
case $opt in
s)
SOUND=true
;;
\?)
echo "Invalid option -$OPTARG" >&2
usage
exit 1
;;
esac
done
# fix parameter
shift $((OPTIND-1))
# catch missing parameter
if [[ -z $1 ]]; then
usage
exit 1
fi
# initialize stuff
seconds=0
valid=false
if [[ -n $2 ]] timer=" ${(qq)2}"
# parse timestamp
[[ "$1" =~ "([0-9]+)h" ]] && seconds=$(($match[1] * 3600)) && valid=true
[[ "$1" =~ "([0-9]+)m" ]] && seconds=$(($seconds + $match[1] * 60)) && valid=true
[[ "$1" =~ "([0-9]+)s" ]] && seconds=$(($seconds + $match[1])) && valid=true
# at least one match was successful
if $valid; then
echo "Starting timer: $(prettyPrint $seconds)."
# offset
seconds=$(($seconds + 1))
# sleep for given time while printing progress
while (( seconds-- > 1 )) {
print -Pn "\r\e[0K$(prettyPrint $seconds) remaining"
sleep 1
}
# see initialization
print -P "\r\e[0KTimer${timer} finished on $(date +'%d.%m.%Y at %H:%M:%S')."
# notify
$SOUND && beep &
notify-send --icon=clock "Timer finished" "$2"
else
echo "Invalid timestamp, exiting."
exit 1
fi
exit 0