-
Notifications
You must be signed in to change notification settings - Fork 8
/
pomodoro.py
229 lines (188 loc) · 7.42 KB
/
pomodoro.py
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
import sublime
import sublime_plugin
import threading
import functools
import time
ST_VERSION = 3000 if sublime.version() == '' else int(sublime.version())
try:
from SubNotify.sub_notify import SubNotifyIsReadyCommand as Notify
except Exception:
class Notify(object):
"""Notify fallback."""
@classmethod
def is_ready(cls):
"""Return false to effectively disable SubNotify."""
return False
timeRecorder_thread = None
def drawProgressbar(totalSize, currPos, charStartBar, charEndBar, charBackground, charPos):
s = charStartBar
for c in range(1, currPos - 1):
s = s + charBackground
s = s + charPos
for c in range(currPos, totalSize):
s = s + charBackground
s = s + charEndBar
return s
def updateWorkingTimeStatus(kwargs):
leftMins = kwargs.get('leftMins')
totMins = kwargs.get('runningMins')
current_pomodoro = kwargs.get('current_pomodoro')
total_pomodoros = kwargs.get('total_pomodoros')
sublime.status_message(
'Working time remaining: ' + str(leftMins) + 'mins | pomodoro: ' +
str(current_pomodoro + 1) + '/' + str(total_pomodoros) + ' ' +
drawProgressbar(totMins, totMins - leftMins + 1, '[', ']', '-', 'O')
)
def updateRestingTimeStatus(kwargs):
leftMins = kwargs.get('leftMins')
totMins = kwargs.get('runningMins')
current_pomodoro = kwargs.get('current_pomodoro')
total_pomodoros = kwargs.get('total_pomodoros')
if current_pomodoro == 0:
current_pomodoro = total_pomodoros
sublime.status_message(
'Resting time remaining: ' + str(leftMins) + 'mins ' + '| break: ' +
str(current_pomodoro) + '/' + str(total_pomodoros) + ' ' +
drawProgressbar(totMins, totMins - leftMins + 1, '[', ']', '-', 'O')
)
def stopRecording():
sublime.status_message('')
def pauseRecording():
sublime.status_message('Pomodoro Paused ||')
time.sleep(1)
sublime.status_message('')
time.sleep(1)
class TimeRecorder(threading.Thread):
def __init__(self, view, workingMins, restingMins, longBreakWorkingCount, longBreakMins):
super(TimeRecorder, self).__init__()
self.view = view
self.workingMins = workingMins
self.restingMins = restingMins
self.longBreakWorkingCount = longBreakWorkingCount
self.longBreakMins = longBreakMins
self.stopFlag = threading.Event()
self.workingSessionCount = 0
self.is_paused = False
def recording(self, runningMins, displayCallback):
leftMins = runningMins
while leftMins > 1:
for i in range(1, 60):
while self.is_paused:
pauseRecording()
if self.stopped():
stopRecording()
break
kwargs = {
'runningMins': runningMins,
'leftMins': leftMins,
'current_pomodoro': self.workingSessionCount,
'total_pomodoros': self.longBreakWorkingCount
}
sublime.set_timeout(functools.partial(displayCallback, kwargs), 10)
time.sleep(1)
leftMins = leftMins - 1
if leftMins == 1:
for i in range(1, 12):
while self.is_paused:
pauseRecording()
if self.stopped():
stopRecording()
break
kwargs = {
'runningMins': runningMins,
'leftMins': leftMins,
'current_pomodoro': self.workingSessionCount,
'total_pomodoros': self.longBreakWorkingCount
}
sublime.set_timeout(functools.partial(displayCallback, kwargs), 10)
time.sleep(5)
leftMins = leftMins - 1
def longBreak(self, workingSessionCount):
return workingSessionCount >= self.longBreakWorkingCount
def run(self):
while 1:
if self.stopped():
stopRecording()
time.sleep(2)
continue
self.recording(self.workingMins, updateWorkingTimeStatus)
if self.stopped():
stopRecording()
time.sleep(2)
continue
if Notify.is_ready():
sublime.run_command("sub_notify", {"title": "", "msg": "Hey, you are working too hard, take a rest."})
rest = True
else:
rest = sublime.ok_cancel_dialog('Hey, you are working too hard, take a rest.', 'OK')
# increase work session count
self.workingSessionCount += 1
if rest:
restingMins = self.restingMins
if self.longBreak(self.workingSessionCount):
restingMins = self.longBreakMins
self.workingSessionCount = 0
self.recording(restingMins, updateRestingTimeStatus)
if self.stopped():
stopRecording()
time.sleep(2)
continue
if Notify.is_ready():
sublime.run_command("sub_notify", {"title": "", "msg": "Come on, let's continue."})
work = True
else:
work = sublime.ok_cancel_dialog("Come on, let's continue.", 'OK')
if not work:
self.stop()
time.sleep(2)
def stop(self):
self.stopFlag.set()
def stopped(self):
return self.stopFlag.isSet()
def resume(self):
self.stopFlag.clear()
def pause(self):
self.is_paused = not self.is_paused
class PomodoroCommand(sublime_plugin.TextCommand):
def run(self, edit, **kwargs):
global timeRecorder_thread
autoStart, workingMins, restingMins, longBreakWorkingCount, longBreakMins = load_settings()
if timeRecorder_thread is None:
timeRecorder_thread = TimeRecorder(
self.view, workingMins, restingMins, longBreakWorkingCount, longBreakMins
)
timeRecorder_thread.start()
elif timeRecorder_thread.stopped():
timeRecorder_thread.resume()
else:
timeRecorder_thread.stop()
class PomodoroPauseCommand(sublime_plugin.TextCommand):
def run(self, edit, **kwargs):
if timeRecorder_thread:
timeRecorder_thread.pause()
def load_settings():
s = sublime.load_settings("Pomodoro.sublime-settings")
autoStart = s.get("autoStart", False)
workingMins = s.get("workingMins", 25)
restingMins = s.get("restingMins", 5)
longBreakWorkingCount = s.get("longBreakWorkingCount", 4)
longBreakMins = s.get("longBreakMins", 15)
return autoStart, workingMins, restingMins, longBreakWorkingCount, longBreakMins
def plugin_loaded():
autoStart, workingMins, restingMins, longBreakWorkingCount, longBreakMins = load_settings()
if autoStart:
sublime.active_window().run_command(
'pomodoro',
)
if ST_VERSION < 3000:
autoStart, workingMins, restingMins, longBreakWorkingCount, longBreakMins = load_settings()
if autoStart:
sublime.active_window().run_command(
'pomodoro',
{
'workingMins': workingMins,
"restingMins": restingMins,
"longBreakWorkingCount": longBreakWorkingCount,
"longBreakMins": longBreakMins
}
)