-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpomux.sh
executable file
·196 lines (164 loc) · 3.98 KB
/
pomux.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
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
#!/bin/sh
#
# Pomodoro + Tmux = Pomux
#
# Carlo Caione <[email protected]>
#
time_pomodoro_min=25
time_short_break_min=5
time_long_break_min=15
color_pomodoro="#[fg=mycolor,bg=mycolor]#[fg=default]%s#[fg=mycolor,bg=mycolor]"
color_short_break="#[fg=mycolor,bg=mycolor]#[fg=red,bold]%s#[fg=mycolor,bg=mycolor]"
color_long_break="#[fg=mycolor,bg=mycolor]#[fg=blue,bold]%s#[fg=mycolor,bg=mycolor]"
msg_start_pomodoro="Pomodoro started"
msg_restart_pomodoro="Pomodoro restarted"
msg_start_short_break="Start short break"
msg_stop_short_break="Stop short break"
msg_start_long_break="Start long break"
msg_stop_long_break="Stop long break"
file_tmux_pipe=$HOME/.pomux-tmux
file_lock=$HOME/.pomux-lock
notify_exe="notify-send"
num_short_breaks=4
pb_length=20
pb_enable=1
if [ "$#" -ne 0 ]; then
OPTIND=1
while getopts "khr" opt; do
case "$opt" in
k)
if [ -f "$file_lock" ]; then
kill $(cat $file_lock)
exit 0
else
printf "pomux not running"
exit 255
fi
;;
r)
if [ -f "$file_lock" ]; then
kill -USR2 $(cat $file_lock)
exit 0
fi
;;
h)
printf "usage: $0 [-k] kill pomux | [-r] start/restart pomux\n"
exit 0
;;
esac
done
fi
if [ -f "$file_lock" ]; then
printf "pomux already running"
exit 0
fi
trap "{ rm -f $file_tmux_pipe $file_lock; exit 255; }" EXIT INT TERM
echo $$ > $file_lock
s_to_m()
{
printf "%02d:%02d" $(($1 / 60)) $(($1 % 60))
}
progress_bar()
{
local time_left=$1
local time_tot=$2
local pb_len=$3
local n_cur=$(($pb_len - ($time_left * $pb_len / $time_tot)))
local cnt=1
printf "%2d%% [" "$(($time_left * 100 / $time_tot))"
while [ "$cnt" -le "$pb_len" ]; do
if [ "$cnt" -le "$n_cur" ]; then
printf "."
else
printf "="
fi
cnt=$((cnt + 1))
done
printf "]"
}
print_status()
{
local step=$1
local color=$2
local time_left=$3
local time_tot=$4
local pb_len=$5
local pb_enable=$6
local str=""
[ "$pb_enable" -ne 0 ] && str=$(progress_bar $time_left $time_tot $pb_len)
str="$str $(s_to_m $time_left)"
str="$str ($step)"
printf $color "$str"
}
enable_reset()
{
trap "reset=1" USR2
if [ "$reset" -eq 1 ]; then
step="P"
cur_step_sec=$time_pomodoro_sec
cur_step_sec_tot=$time_pomodoro_sec
cur_step_color=$color_pomodoro
n_short_breaks=0
reset=0
[ ! -z "$notify_exe" -a ! -z "$msg_restart_pomodoro" ] && $notify_exe "$msg_restart_pomodoro"
fi
}
wait_for_usr2()
{
local catch=0
trap "catch=1" USR2
while [ "$catch" -eq 0 ]; do
sleep 1
done
trap - USR2
enable_reset
}
time_pomodoro_sec=$((time_pomodoro_min * 60))
time_short_break_sec=$((time_short_break_min * 60))
time_long_break_sec=$((time_long_break_min * 60))
step="P"
cur_step_sec=$time_pomodoro_sec
cur_step_sec_tot=$time_pomodoro_sec
cur_step_color=$color_pomodoro
n_short_breaks=0
reset=0
while true
do
enable_reset
cur_step_sec=$((cur_step_sec - 1))
print_status $step $cur_step_color $cur_step_sec $cur_step_sec_tot $pb_length $pb_enable > $file_tmux_pipe
sleep 1
if [ "$cur_step_sec" -eq 0 ]; then
case "$step" in
P)
if [ "$n_short_breaks" -eq "$num_short_breaks" ]; then
[ ! -z "$notify_exe" -a ! -z "$msg_start_long_break" ] && $notify_exe "$msg_start_long_break"
cur_step_sec_tot=$time_long_break_sec
cur_step_color=$color_long_break
step="LB"
n_short_breaks=0
else
[ ! -z "$notify_exe" -a ! -z "$msg_start_short_break" ] && $notify_exe "$msg_start_short_break"
cur_step_sec_tot=$time_short_break_sec
cur_step_color=$color_short_break
step="SB"
fi
;;
SB | LB)
if [ "$step" = "SB" ]; then
n_short_breaks=$((n_short_breaks + 1))
[ ! -z "$notify_exe" -a ! -z "$msg_stop_short_break" ] && $notify_exe "$msg_stop_short_break"
else
[ ! -z "$notify_exe" -a ! -z "$msg_stop_long_break" ] && $notify_exe "$msg_stop_long_break"
fi
cur_step_sec_tot=$time_pomodoro_sec
cur_step_color=$color_pomodoro
step="P"
wait_for_usr2
[ ! -z "$notify_exe" -a ! -z "$msg_start_pomodoro" ] && $notify_exe "$msg_start_pomodoro"
;;
esac
cur_step_sec=$cur_step_sec_tot
fi
done
exit 0