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
|
# -*- coding: utf-8 -*-
"""
Pomodoro countdown on i3bar originally written by @Fandekasp (Adrien Lemaire)
"""
from subprocess import call
from syslog import syslog, LOG_INFO
from time import time
try:
from pygame import mixer
mixer.init()
except ImportError:
mixer = None
# PROGRESS_BAR_ITEMS = u"▁▃▄▅▆▇█"
PROGRESS_BAR_ITEMS = u"▏▎▍▌▋▊▉"
class Py3status:
"""
Configuration parameters:
- display_bar: display time in bars when True, otherwise in seconds
- max_breaks: maximum number of breaks
- num_progress_bars: number of progress bars
- sound_break_end: break end sound (file path)
- sound_pomodoro_end: pomodoro end sound (file path)
- sound_pomodoro_start: pomodoro start sound (file path)
- timer_break: normal break time (seconds) (requires pygame)
- timer_long_break: long break time (seconds) (requires pygame)
- timer_pomodoro: pomodoro time (seconds) (requires pygame)
"""
# available configuration parameters
display_bar = False
max_breaks = 4
num_progress_bars = 5
sound_break_end = None
sound_pomodoro_end = None
sound_pomodoro_start = None
timer_break = 5 * 60
timer_long_break = 15 * 60
timer_pomodoro = 25 * 60
def __init__(self):
self.__setup('stop')
self.alert = False
self.run = False
def on_click(self, i3s_output_list, i3s_config, event):
"""
Handles click events:
- left click starts an inactive counter and pauses a running
Pomodoro
- middle click resets everything
- right click starts (and ends, if needed) a break
"""
if event['button'] == 1:
if self.status == 'stop':
self.status = 'start'
self.__play_sound(self.sound_pomodoro_start)
self.run = True
elif self.status == 'break':
self.run = True
elif self.status == 'start':
if self.run:
self.status = 'pause'
self.run = False
else:
self.run = True
elif self.status == 'pause':
self.status = 'start'
self.run = True
elif event['button'] == 2:
self.__setup('stop')
self.run = False
elif event['button'] == 3:
if self.status == 'break':
self.__setup('start')
else:
self.__setup('break')
self.run = False
@property
def response(self):
"""
Return the response full_text string
"""
if self.display_bar and self.status in ('start', 'pause'):
bar = u''
items_cnt = len(PROGRESS_BAR_ITEMS)
bar = u''
bar_val = float(self.timer) / self.time_window * \
self.num_progress_bars
while bar_val > 0:
selector = int(bar_val * items_cnt)
selector = min(selector, items_cnt - 1)
bar += PROGRESS_BAR_ITEMS[selector]
bar_val -= 1
bar = bar.ljust(self.num_progress_bars).encode('utf_8')
else:
bar = self.timer
if self.run:
text = '{} [{}]'.format(self.prefix, bar)
else:
text = '{} ({})'.format(self.prefix, bar)
return dict(full_text=text)
def __setup(self, status):
"""
Setup a step
"""
self.status = status
if status == 'stop':
self.prefix = 'Pomodoro'
self.status = 'stop'
self.timer = self.timer_pomodoro
self.time_window = self.timer
self.breaks = 1
elif status == 'start':
self.prefix = 'Pomodoro'
self.timer = self.timer_pomodoro
self.time_window = self.timer
elif status == 'break':
self.prefix = 'Break #%d' % self.breaks
if self.breaks > self.max_breaks:
self.timer = self.timer_long_break
self.time_window = self.timer
self.breaks = 1
else:
self.breaks += 1
self.timer = self.timer_break
self.time_window = self.timer
def __decrement(self):
"""
Countdown handler
"""
self.timer -= 1
if self.timer < 0:
self.alert = True
self.run = False
self.__i3_nagbar()
if self.status == 'start':
self.__setup('break')
self.status = 'break'
self.__play_sound(self.sound_pomodoro_end)
elif self.status == 'break':
self.__setup('start')
self.status = 'start'
self.__play_sound(self.sound_break_end)
def __i3_nagbar(self, level='warning'):
"""
Make use of i3-nagbar to display warnings to the user.
"""
msg = '{} time is up !'.format(self.prefix)
try:
call(
['i3-nagbar', '-m', msg, '-t', level],
stdout=open('/dev/null', 'w'),
stderr=open('/dev/null', 'w')
)
except:
pass
def pomodoro(self, i3s_output_list, i3s_config):
"""
Pomodoro response handling and countdown
"""
if self.run:
self.__decrement()
response = self.response
if self.alert:
response['urgent'] = True
self.alert = False
if self.status == 'start':
response['color'] = i3s_config['color_good']
elif self.status == 'break':
response['color'] = i3s_config['color_degraded']
else:
response['color'] = i3s_config['color_bad']
response['cached_until'] = time()
return response
def __play_sound(self, sound_fname):
"""Play sound if required
"""
if not sound_fname:
return
if not mixer:
syslog(LOG_INFO, "pomodoro module: the pygame library is required"
" to play sounds")
return
try:
mixer.music.load(sound_fname)
except Exception as e:
return
mixer.music.play()
if __name__ == "__main__":
"""
Test this module by calling it directly.
"""
from time import sleep
x = Py3status()
config = {
'color_good': '#00FF00',
'color_bad': '#FF0000',
}
while True:
print(x.pomodoro([], config))
sleep(1)
|