-
Notifications
You must be signed in to change notification settings - Fork 5
/
i3-pomodoro
executable file
·202 lines (164 loc) · 5.4 KB
/
i3-pomodoro
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
#!/usr/bin/env python3
# Source: https://github.com/rkashapov/i3blocks-pomodoro
#
# MIT License
#
# Copyright (c) 2019 Rustam Kashapov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
Simple Pomodoro app inspired by spt project.
The app designed to be used with i3blocks.
"""
import errno
import os
import socket
from itertools import cycle
from subprocess import call
from time import sleep
from threading import Thread
def notify(text):
call(["notify-send", "Pomodoro", text])
def daemonize():
if os.fork() > 0:
exit()
os.setsid()
if os.fork() > 0:
exit()
class AlredyStartedError(Exception):
pass
class Cycle:
def __init__(self, items):
self._items = items
self._iter = None
self.reset()
def reset(self):
self._iter = cycle(self._items)
def __iter__(self):
return self
def __next__(self):
return next(self._iter)
class Pomodoro:
def __init__(self, cycle):
self._cycle = cycle
self._state = "Paused"
self._remaining = 0
self._do_reset = False
self._running = False
@property
def state(self):
state = self._state
if not self._running:
state = "Paused"
minutes, seconds = divmod(self._remaining, 60)
return "{0} {1:02d}:{2:02d}".format(state, minutes, seconds)
def toggle(self):
self._running = not self._running
def reset(self):
self._do_reset = True
self._cycle.reset()
def start(self):
for timeout, state, message in self._cycle:
if self._do_reset:
self._do_reset = False
if self._running:
self._state = state
notify(message)
self._remaining = timeout * 60
while self._remaining:
if self._do_reset:
break
sleep(1)
if self._running:
self._remaining -= 1
self._state = state
class App:
ADDRESS = '/tmp/pomodoro.sock'
PAUSE = b"pause"
RESET = b"reset"
DISPLAY = b"display"
def __init__(self, pomodoro):
self._thread = Thread(target=self._handle_connections)
self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self._pomodoro = pomodoro
def run(self):
try:
self._sock.bind(self.ADDRESS)
except socket.error as e:
if e.errno != errno.EADDRINUSE:
raise e
try:
self._sock.connect(self.ADDRESS)
except socket.error as err:
if err.errno != errno.ECONNREFUSED:
raise e
os.unlink(self.ADDRESS)
self._sock.bind(self.ADDRESS)
else:
raise AlredyStartedError()
daemonize()
Thread(target=self._pomodoro.start).start()
self._handle_connections()
def display(self):
self._send(self.DISPLAY)
def toggle(self):
self._send(self.PAUSE)
def reset(self):
self._send(self.RESET)
def _send(self, command):
self._sock.sendall(command)
print(self._sock.recv(32).decode("utf-8"))
def _handle_connections(self):
self._sock.listen()
while True:
conn, _ = self._sock.accept()
command = conn.recv(16)
if command == self.PAUSE:
self._pomodoro.toggle()
elif command == self.RESET:
self._pomodoro.reset()
conn.sendall(self._pomodoro.state.encode("utf-8"))
conn.close()
app = App(
Pomodoro(
Cycle(
[
(25, "Working", "Time to start working!"),
(5, "Resting", "Time to start resting!"),
(25, "Working", "Time to start working!"),
(5, "Resting", "Time to start resting!"),
(25, "Working", "Time to start working!"),
(5, "Resting", "Time to start resting!"),
(25, "Working", "Time to start working!"),
(15, "Long Break", "Time to take some nap!"),
]
)
)
)
if __name__ == "__main__":
try:
app.run()
except AlredyStartedError:
button = os.getenv("BLOCK_BUTTON", "").lower()
if button == "1":
app.toggle()
elif button == "3":
app.reset()
else:
app.display()