Skip to content

Instantly share code, notes, and snippets.

@nicoe
Created April 10, 2019 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicoe/3c36ddc4b7242a1cad6f3639419a6fef to your computer and use it in GitHub Desktop.
Save nicoe/3c36ddc4b7242a1cad6f3639419a6fef to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
from gi.repository import Gtk
from gi.repository import GLib
POMODORO_TIME = 25
BREAK_TIME = 5
LONG_BREAK_TIME = 15
LONG_BREAK_OCCURENCE = 4
STATUS_FILE = os.path.expanduser('~/.var/pomodoro')
class AnnoyingWindow(Gtk.Dialog):
def __init__(self):
super(AnnoyingWindow, self).__init__()
self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
self.set_has_resize_grip(False)
self.set_skip_pager_hint(True)
self.set_skip_taskbar_hint(True)
self.set_keep_above(True)
self.stick()
self.connect('delete-event', Gtk.main_quit)
class StartBreakWindow(AnnoyingWindow):
def __init__(self, start_break, skip_break):
super(StartBreakWindow, self).__init__()
content = self.get_content_area()
label = Gtk.Label(
'You can now start your {} minute break'.format(BREAK_TIME))
content.pack_start(label, expand=True, fill=True, padding=0)
action = self.get_action_area()
start_break_btn = Gtk.Button('Start Break')
start_break_btn.connect('clicked', start_break)
skip_break_btn = Gtk.Button('Skip Break')
skip_break_btn.connect('clicked', skip_break)
action.pack_start(start_break_btn, expand=True, fill=True, padding=0)
action.pack_start(skip_break_btn, expand=True, fill=True, padding=0)
class PomodoroWindow(AnnoyingWindow):
def __init__(self, start_pomodoro):
super(PomodoroWindow, self).__init__()
content = self.get_content_area()
label = Gtk.Label('Are you ready to start your {} minute pomodoro'
.format(POMODORO_TIME))
content.pack_start(label, expand=True, fill=True, padding=0)
action = self.get_action_area()
button = Gtk.Button(label='Start pomodoro')
button.connect('clicked', start_pomodoro)
action.pack_start(button, expand=True, fill=True, padding=0)
class Pomodoro:
def __init__(self):
self.state = None
self.breaks = 0
self.elapsed_time = 0
self.pomodoro_window = PomodoroWindow(self.start_pomodoro)
self.break_window = StartBreakWindow(self.start_break, self.skip_break)
def start_pomodoro(self, widget):
self.maximum_time = POMODORO_TIME
self.state = 'POMODORO'
self.update_status()
GLib.timeout_add_seconds(60, self.check_time)
self.pomodoro_window.hide()
def start_break(self, widget):
self.breaks += 1
if self.breaks % LONG_BREAK_OCCURENCE:
self.maximum_time = BREAK_TIME
else:
self.maximum_time = LONG_BREAK_TIME
self.state = 'BREAK'
self.update_status()
GLib.timeout_add_seconds(60, self.check_time)
self.break_window.hide()
def skip_break(self, widget):
self.state = None
self.break_window.hide()
self.pomodoro_window.show_all()
def update_status(self):
with open(STATUS_FILE, 'w') as status:
progression = (float(self.maximum_time - self.elapsed_time)
/ self.maximum_time)
status.write("{} {}".format(self.state, 100 * progression))
def check_time(self):
self.elapsed_time += 1
finished = self.elapsed_time >= self.maximum_time
self.update_status()
if finished:
self.elapsed_time = 0
if self.state == 'POMODORO':
self.break_window.show_all()
elif self.state == 'BREAK':
self.pomodoro_window.show_all()
return not finished
if __name__ == '__main__':
app = Pomodoro()
app.pomodoro_window.show_all()
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment