forked from Neway6655/Sublime-Pomodoro
/
pomodoro.py
79 lines (62 loc) · 2.69 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
import sublime
import sublime_plugin
from threading import Thread
import functools
import time
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(totMins, leftMins):
sublime.status_message('Working time remaining: ' + str(leftMins) + 'mins ' + drawProgressbar(totMins, totMins - leftMins + 1, '[', ']', '-', 'O'))
def updateRestingTimeStatus(totMins, leftMins):
sublime.status_message('Resting time remaining: ' + str(leftMins) + 'mins ' + drawProgressbar(totMins, totMins - leftMins + 1, '[', ']', '-', 'O'))
class TimeRecorder(Thread):
def __init__(self, view, workingMins, restingMins):
self.view = view
self.workingMins = workingMins
self.restingMins = restingMins
Thread.__init__(self)
def recording(self, runningMins, displayCallback):
leftMins = runningMins
while leftMins > 1:
for i in range(1, 60):
sublime.set_timeout(functools.partial(displayCallback, runningMins, leftMins), 10)
time.sleep(1)
leftMins = leftMins - 1
if leftMins == 1:
for i in range(1, 12):
sublime.set_timeout(functools.partial(displayCallback, runningMins, leftMins), 10)
time.sleep(5)
leftMins = leftMins - 1
def run(self):
while 1:
self.recording(self.workingMins, updateWorkingTimeStatus)
rest = sublime.ok_cancel_dialog('Hey, you are working too hard, take a rest.', 'OK')
if rest:
self.recording(self.restingMins, updateRestingTimeStatus)
work = sublime.ok_cancel_dialog("Come on, let's continue.", 'OK')
if not work:
break
time.sleep(2)
self.stop()
def stop(self):
if self.isAlive():
self._Thread__stop()
class PomodoroCommand(sublime_plugin.TextCommand):
_timeRecorder_thread = None
def run(self, edit, workingMins, restingMins):
if (self._timeRecorder_thread is not None):
self._timeRecorder_thread.stop()
self._timeRecorder_thread = TimeRecorder(self.view, workingMins, restingMins)
self._timeRecorder_thread.start()
def is_enabled(self):
if self._timeRecorder_thread is None or not self._timeRecorder_thread.isAlive():
return True
else:
return False