-
Notifications
You must be signed in to change notification settings - Fork 0
/
waybar-timer.sh
executable file
·103 lines (92 loc) · 2.68 KB
/
waybar-timer.sh
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/bash
### AUTHOR: Johann Birnick (github: jbirnick)
### PROJECT REPO: https://github.com/jbirnick/waybar-timer
## FUNCTIONS
now () { date --utc +%s; }
killTimer () { rm -rf /tmp/waybar-timer ; }
timerSet () { [ -e /tmp/waybar-timer/ ] ; }
timerPaused () { [ -f /tmp/waybar-timer/paused ] ; }
timerExpiry () { cat /tmp/waybar-timer/expiry ; }
timerAction () { cat /tmp/waybar-timer/action ; }
secondsLeftWhenPaused () { cat /tmp/waybar-timer/paused ; }
minutesLeftWhenPaused () { echo $(( ( $(secondsLeftWhenPaused) + 59 ) / 60 )) ; }
secondsLeft () { echo $(( $(timerExpiry) - $(now) )) ; }
minutesLeft () { echo $(( ( $(secondsLeft) + 59 ) / 60 )) ; }
printExpiryTime () { notify-send -u low -r 12345 "Timer expires at $( date -d "$(secondsLeft) sec" +%H:%M)" ;}
printPaused () { notify-send -u low -r 12345 "Timer paused" ; }
removePrinting () { notify-send -C 12345 ; }
updateTail () {
# check whether timer is expired
if timerSet
then
if { timerPaused && [ $(minutesLeftWhenPaused) -le 0 ] ; } || { ! timerPaused && [ $(minutesLeft) -le 0 ] ; }
then
eval $(timerAction)
killTimer
removePrinting
fi
fi
# update output
if timerSet
then
if timerPaused
then
echo "{\"text\": \"$(minutesLeftWhenPaused)\", \"alt\": \"paused\", \"tooltip\": \"Timer paused\", \"class\": \"timer\" }"
else
echo "{\"text\": \"$(minutesLeft)\", \"alt\": \"running\", \"tooltip\": \"Timer expires at $( date -d "$(secondsLeft) sec" +%H:%M)\", \"class\": \"timer\" }"
fi
else
echo "{\"text\": \"0\", \"alt\": \"standby\", \"tooltip\": \"No timer set\", \"class\": \"timer\" }"
fi
}
## MAIN CODE
case $1 in
updateandprint)
updateTail
;;
new)
killTimer
mkdir /tmp/waybar-timer
echo "$(( $(now) + 60*${2} ))" > /tmp/waybar-timer/expiry
echo "${3}" > /tmp/waybar-timer/action
printExpiryTime
;;
increase)
if timerSet
then
if timerPaused
then
echo "$(( $(secondsLeftWhenPaused) + ${2} ))" > /tmp/waybar-timer/paused
else
echo "$(( $(timerExpiry) + ${2} ))" > /tmp/waybar-timer/expiry
printExpiryTime
fi
else
exit 1
fi
;;
cancel)
killTimer
removePrinting
;;
togglepause)
if timerSet
then
if timerPaused
then
echo "$(( $(now) + $(secondsLeftWhenPaused) ))" > /tmp/waybar-timer/expiry
rm -f /tmp/waybar-timer/paused
printExpiryTime
else
secondsLeft > /tmp/waybar-timer/paused
rm -f /tmp/waybar-timer/expiry
printPaused
fi
else
exit 1
fi
;;
*)
echo "Please read the manual at https://github.com/jbirnick/waybar-timer ."
;;
esac