Merge pull request #384 from rampage644/pomodoro_refactor
Pomodoro module timezone fix
This commit is contained in:
commit
9c414154fb
@ -1,12 +1,13 @@
|
|||||||
import os
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import locale
|
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from i3pystatus import IntervalModule
|
from i3pystatus import IntervalModule
|
||||||
|
|
||||||
|
|
||||||
|
STOPPED = 0
|
||||||
|
RUNNING = 1
|
||||||
|
BREAK = 2
|
||||||
|
|
||||||
|
|
||||||
class Pomodoro(IntervalModule):
|
class Pomodoro(IntervalModule):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -45,59 +46,57 @@ class Pomodoro(IntervalModule):
|
|||||||
|
|
||||||
def init(self):
|
def init(self):
|
||||||
# state could be either running/break or stopped
|
# state could be either running/break or stopped
|
||||||
self.state = 'stopped'
|
self.state = STOPPED
|
||||||
self.breaks = 0
|
self.current_pomodoro = 0
|
||||||
|
self.total_pomodoro = self.short_break_count + 1 # and 1 long break
|
||||||
self.time = None
|
self.time = None
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
if self.time and datetime.now() >= self.time:
|
if self.time and datetime.utcnow() >= self.time:
|
||||||
if self.state == 'running':
|
if self.state == RUNNING:
|
||||||
self.state = 'break'
|
self.state = BREAK
|
||||||
if self.breaks == self.short_break_count:
|
if self.current_pomodoro == self.short_break_count:
|
||||||
self.time = datetime.now() + \
|
self.time = datetime.utcnow() + \
|
||||||
timedelta(seconds=self.long_break_duration)
|
timedelta(seconds=self.long_break_duration)
|
||||||
self.breaks = 0
|
|
||||||
else:
|
else:
|
||||||
self.time = datetime.now() + \
|
self.time = datetime.utcnow() + \
|
||||||
timedelta(seconds=self.break_duration)
|
timedelta(seconds=self.break_duration)
|
||||||
self.breaks += 1
|
|
||||||
text = 'Go for a break!'
|
text = 'Go for a break!'
|
||||||
else:
|
else:
|
||||||
self.state = 'running'
|
self.state = RUNNING
|
||||||
self.time = datetime.now() + \
|
self.time = datetime.utcnow() + \
|
||||||
timedelta(seconds=self.pomodoro_duration)
|
timedelta(seconds=self.pomodoro_duration)
|
||||||
text = 'Back to work!'
|
text = 'Back to work!'
|
||||||
|
self.current_pomodoro = (self.current_pomodoro + 1) % self.total_pomodoro
|
||||||
self._alarm(text)
|
self._alarm(text)
|
||||||
|
|
||||||
if self.state == 'running' or self.state == 'break':
|
if self.state == RUNNING or self.state == BREAK:
|
||||||
min, sec = divmod((self.time - datetime.now()).total_seconds(), 60)
|
min, sec = divmod((self.time - datetime.utcnow()).total_seconds(), 60)
|
||||||
text = '{:02}:{:02}'.format(int(min), int(sec))
|
text = '{:02}:{:02}'.format(int(min), int(sec))
|
||||||
color = self.color_running if self.state == 'running' else self.color_break
|
|
||||||
else:
|
|
||||||
self.output = {
|
|
||||||
'full_text': 'Stopped',
|
|
||||||
'color': self.color_stopped
|
|
||||||
}
|
|
||||||
return
|
|
||||||
|
|
||||||
sdict = {
|
sdict = {
|
||||||
'time': text,
|
'time': text,
|
||||||
'current_pomodoro': self.breaks,
|
'current_pomodoro': self.current_pomodoro + 1,
|
||||||
'total_pomodoro': self.short_break_count + 1,
|
'total_pomodoro': self.total_pomodoro
|
||||||
}
|
}
|
||||||
self.data = sdict
|
|
||||||
|
color = self.color_running if self.state == RUNNING else self.color_break
|
||||||
|
text = self.format.format(**sdict)
|
||||||
|
else:
|
||||||
|
text = 'Start pomodoro',
|
||||||
|
color = self.color_stopped
|
||||||
|
|
||||||
self.output = {
|
self.output = {
|
||||||
'full_text': self.format.format(**sdict),
|
'full_text': text,
|
||||||
'color': color
|
'color': color
|
||||||
}
|
}
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self.state = 'running'
|
self.state = RUNNING
|
||||||
self.time = datetime.now() + timedelta(seconds=self.pomodoro_duration)
|
self.time = datetime.utcnow() + timedelta(seconds=self.pomodoro_duration)
|
||||||
self.breaks = 0
|
self.current_pomodoro = 0
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.state = 'stopped'
|
self.state = STOPPED
|
||||||
self.time = None
|
self.time = None
|
||||||
|
|
||||||
def _alarm(self, text):
|
def _alarm(self, text):
|
||||||
@ -106,4 +105,5 @@ class Pomodoro(IntervalModule):
|
|||||||
text])
|
text])
|
||||||
subprocess.Popen(['aplay',
|
subprocess.Popen(['aplay',
|
||||||
self.sound,
|
self.sound,
|
||||||
'-q'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
'-q'],
|
||||||
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
|
Loading…
Reference in New Issue
Block a user