2020-04-25 16:15:46 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Display and run a Pomodoro timer.
|
|
|
|
Left click to start timer, left click again to pause.
|
|
|
|
Right click will cancel the timer.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* pomodoro.work: The work duration of timer in minutes (defaults to 25)
|
|
|
|
* pomodoro.break: The break duration of timer in minutes (defaults to 5)
|
2020-04-25 16:16:20 +02:00
|
|
|
* pomodoro.format: Timer display format with '%m' and '%s' for minutes and seconds (defaults to '%m:%s')
|
|
|
|
Examples: '%m min %s sec', '%mm', '', 'timer'
|
2020-04-25 16:15:46 +02:00
|
|
|
* pomodoro.notify: Notification command to run when timer ends/starts (defaults to nothing)
|
2020-04-25 16:16:20 +02:00
|
|
|
Example: 'notify-send 'Time up!''. If you want to chain multiple commands,
|
2020-04-25 16:15:46 +02:00
|
|
|
please use an external wrapper script and invoke that. The module itself does
|
|
|
|
not support command chaining (see https://github.com/tobi-wan-kenobi/bumblebee-status/issues/532
|
|
|
|
for a detailled explanation)
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import datetime
|
|
|
|
from math import ceil
|
|
|
|
|
2020-04-25 16:19:08 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
2020-04-25 16:15:46 +02:00
|
|
|
|
2020-04-25 16:19:08 +02:00
|
|
|
import util.cli
|
2020-04-25 16:15:46 +02:00
|
|
|
|
2020-04-25 16:19:08 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
def __init__(self, config):
|
|
|
|
super().__init__(config, core.widget.Widget(self.text))
|
2020-04-25 16:15:46 +02:00
|
|
|
|
|
|
|
# Parameters
|
2020-04-25 16:19:08 +02:00
|
|
|
self.__work_period = int(self.parameter('work', 25))
|
|
|
|
self.__break_period = int(self.parameter('break', 5))
|
|
|
|
self.__time_format = self.parameter('format', '%m:%s')
|
|
|
|
self.__notify_cmd = self.parameter('notify', '')
|
2020-04-25 16:15:46 +02:00
|
|
|
|
|
|
|
# TODO: Handle time formats more gracefully. This is kludge.
|
|
|
|
self.display_seconds_p = False
|
|
|
|
self.display_minutes_p = False
|
2020-04-25 16:19:08 +02:00
|
|
|
if '%s' in self.__time_format:
|
2020-04-25 16:15:46 +02:00
|
|
|
self.display_seconds_p = True
|
2020-04-25 16:19:08 +02:00
|
|
|
if '%m' in self.__time_format:
|
2020-04-25 16:15:46 +02:00
|
|
|
self.display_minutes_p = True
|
|
|
|
|
2020-04-25 16:19:08 +02:00
|
|
|
self.remaining_time = datetime.timedelta(minutes=self.__work_period)
|
2020-04-25 16:15:46 +02:00
|
|
|
|
|
|
|
self.time = None
|
2020-04-25 16:16:20 +02:00
|
|
|
self.pomodoro = { 'state':'OFF', 'type': ''}
|
2020-04-25 16:19:08 +02:00
|
|
|
self.__text = self.remaining_time_str() + self.pomodoro['type']
|
2020-04-25 16:15:46 +02:00
|
|
|
|
2020-04-25 16:19:08 +02:00
|
|
|
core.input.register(self, button=core.input.LEFT_MOUSE,
|
2020-04-25 16:15:46 +02:00
|
|
|
cmd=self.timer_play_pause)
|
2020-04-25 16:19:08 +02:00
|
|
|
core.input.register(self, button=core.input.RIGHT_MOUSE,
|
2020-04-25 16:15:46 +02:00
|
|
|
cmd=self.timer_reset)
|
|
|
|
|
|
|
|
def remaining_time_str(self):
|
|
|
|
if self.display_seconds_p and self.display_minutes_p:
|
|
|
|
minutes, seconds = divmod(self.remaining_time.seconds, 60)
|
|
|
|
if not self.display_seconds_p:
|
|
|
|
minutes = ceil(self.remaining_time.seconds / 60)
|
|
|
|
seconds = 0
|
|
|
|
if not self.display_minutes_p:
|
|
|
|
minutes = 0
|
|
|
|
seconds = self.remaining_time.seconds
|
|
|
|
|
2020-04-25 16:16:20 +02:00
|
|
|
minutes = '{:2d}'.format(minutes)
|
|
|
|
seconds = '{:02d}'.format(seconds)
|
2020-04-25 16:19:08 +02:00
|
|
|
return self.__time_format.replace('%m',minutes).replace('%s',seconds)+' '
|
2020-04-25 16:15:46 +02:00
|
|
|
|
|
|
|
def text(self, widget):
|
2020-04-25 16:19:08 +02:00
|
|
|
return '{}'.format(self.__text)
|
2020-04-25 16:15:46 +02:00
|
|
|
|
2020-04-25 16:19:08 +02:00
|
|
|
def update(self):
|
2020-04-25 16:16:20 +02:00
|
|
|
if self.pomodoro['state'] == 'ON':
|
2020-04-25 16:15:46 +02:00
|
|
|
timediff = (datetime.datetime.now() - self.time)
|
|
|
|
if timediff.seconds >= 0:
|
|
|
|
self.remaining_time -= timediff
|
|
|
|
self.time = datetime.datetime.now()
|
|
|
|
|
|
|
|
if self.remaining_time.total_seconds() <= 0:
|
|
|
|
self.notify()
|
2020-04-25 16:16:20 +02:00
|
|
|
if self.pomodoro['type'] == 'Work':
|
|
|
|
self.pomodoro['type'] = 'Break'
|
2020-04-25 16:19:08 +02:00
|
|
|
self.remaining_time = datetime.timedelta(minutes=self.__break_period)
|
2020-04-25 16:16:20 +02:00
|
|
|
elif self.pomodoro['type'] == 'Break':
|
|
|
|
self.pomodoro['type'] = 'Work'
|
2020-04-25 16:19:08 +02:00
|
|
|
self.remaining_time = datetime.timedelta(minutes=self.__work_period)
|
2020-04-25 16:15:46 +02:00
|
|
|
|
2020-04-25 16:19:08 +02:00
|
|
|
self.__text = self.remaining_time_str() + self.pomodoro['type']
|
2020-04-25 16:15:46 +02:00
|
|
|
|
|
|
|
def notify(self):
|
2020-04-25 16:19:08 +02:00
|
|
|
if self.__notify_cmd:
|
|
|
|
util.cli.execute(self.__notify_cmd)
|
2020-04-25 16:15:46 +02:00
|
|
|
|
|
|
|
def timer_play_pause(self, widget):
|
2020-04-25 16:16:20 +02:00
|
|
|
if self.pomodoro['state'] == 'OFF':
|
|
|
|
self.pomodoro = {'state': 'ON', 'type': 'Work'}
|
2020-04-25 16:19:08 +02:00
|
|
|
self.remaining_time = datetime.timedelta(minutes=self.__work_period)
|
2020-04-25 16:15:46 +02:00
|
|
|
self.time = datetime.datetime.now()
|
2020-04-25 16:16:20 +02:00
|
|
|
elif self.pomodoro['state'] == 'ON':
|
|
|
|
self.pomodoro['state'] = 'PAUSED'
|
2020-04-25 16:15:46 +02:00
|
|
|
self.remaining_time -= (datetime.datetime.now() - self.time)
|
|
|
|
self.time = datetime.datetime.now()
|
2020-04-25 16:16:20 +02:00
|
|
|
elif self.pomodoro['state'] == 'PAUSED':
|
|
|
|
self.pomodoro['state'] = 'ON'
|
2020-04-25 16:15:46 +02:00
|
|
|
self.time = datetime.datetime.now()
|
|
|
|
|
|
|
|
def timer_reset(self, widget):
|
2020-04-25 16:16:20 +02:00
|
|
|
if self.pomodoro['state'] == 'ON' or self.pomodoro['state'] == 'PAUSED':
|
|
|
|
self.pomodoro = {'state':'OFF', 'type': '' }
|
2020-04-25 16:19:08 +02:00
|
|
|
self.remaining_time = datetime.timedelta(minutes=self.__work_period)
|
2020-04-25 16:15:46 +02:00
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
state = [];
|
2020-04-25 16:16:20 +02:00
|
|
|
state.append(self.pomodoro['state'].lower())
|
|
|
|
if self.pomodoro['state'] == 'ON' or self.pomodoro['state'] == 'OFF':
|
|
|
|
state.append(self.pomodoro['type'].lower())
|
2020-04-25 16:15:46 +02:00
|
|
|
|
|
|
|
return state
|
2020-04-25 16:19:08 +02:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|