summaryrefslogtreecommitdiffstats
path: root/examples/pomodoro.py
blob: df15be4dd2cbf55b5b5277c6d6b78e10683e1b8f (plain)
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
"""
Pomodoro countdown on i3bar originally written by @Fandekasp (Adrien Lemaire)

Requires:
    - libnotify
    - python-gobject
    - python-keybinder
"""
from gi.repository import Notify
from gi.repository import GObject as gobject

#FIXME: keybinder gobject collides with Notify but its not blocking
import keybinder
import gtk

from threading import Thread
from time import time
from time import sleep

KEY_PAUSE = '<Mod1>B' #Alt + B(reak)
KEY_START = '<Mod1>P' #Alt + P(omodoro)
KEY_STOP  = '<Mod1>R' #Alt + R(eset)
MAX_BREAKS = 4
POSITION = 0
TIMER_POMODORO = 25 * 60
TIMER_BREAK = 5 * 60
TIMER_LONG_BREAK = 15 * 60

pomo_thread = None
gobject.threads_init()

class Pomodoro(Thread):
    """
    Handles Pomodoro and Break countdowns
    """
    def __init__(self):
        """
        Well, default values
        """
        Thread.__init__(self)
        self.kill = False
        self.callback('stop')

    @property
    def response(self):
        """
        Return the response full_text string
        """
        return '%s (%d)' % (self.prefix, self.timer)

    def callback(self, status):
        """
        Called on a keybinder event
        """
        self.status = status
        if status == 'stop':
            self.prefix = 'Pomodoro'
            self.status = 'stop'
            self.timer = TIMER_POMODORO
            self.breaks = 1

        elif status == 'start':
            self.prefix = 'Pomodoro'
            self.timer = TIMER_POMODORO

        elif status == 'pause':
            self.prefix = 'Break #%d' % self.breaks
            if self.breaks > MAX_BREAKS:
                self.timer = TIMER_LONG_BREAK
                self.breaks = 1
            else:
                self.breaks += 1
                self.timer = TIMER_BREAK

    def stop(self):
        """
        Exit nicely
        """
        self.kill = True

    def notify(self):
        """
        Print a visual message
        """
        try:
            Notify.init('')
            notification = Notify.Notification.new (
                '<b>Attention:</b>',
                '{} is finished'.format(self.response),
                'dialog-information',
                )
            notification.show()
        except Exception as err:
            pass

    def run(self):
        """
        Infinite loop setting the keybinder and waiting for key events
        """
        bindings = {
            'pause': KEY_PAUSE,
            'start': KEY_START,
            'stop' : KEY_STOP,
            }
        for status, keystr in bindings.items():
            keybinder.bind(keystr, self.callback, status)

        while not self.kill:
            gtk.main_iteration(block=False)
            if self.status != 'stop' and self.timer > 0:
                self.timer -= 1
                if self.timer == 0:
                    self.notify()
            sleep(1)

        for status, keystr in bindings.items():
            try:
                keybinder.unbind(keystr)
            except Exception as e:
                pass

class Py3status:
    """
    Main py3status class which spawns a thread for Pomodoro keybinder
    """
    def kill(self):
        """
        Exit nicely
        """
        pomo_thread.stop()

    def pomodoro(self, json, i3status_config):
        """
        Pomodoro response handling
        """
        response = {'full_text' : '', 'name' : 'pomodoro'}

        global pomo_thread

        if not pomo_thread:
            pomo_thread = Pomodoro()
            pomo_thread.start()
        else:
            response['full_text'] = pomo_thread.response

        if pomo_thread.status == 'start':
            response['color'] = i3status_config['color_good']
        elif pomo_thread.status == 'pause':
            response['color'] = i3status_config['color_degraded']
        else:
            response['color'] = i3status_config['color_bad']

        response['cached_until'] = time()
        return (POSITION, response)