summaryrefslogtreecommitdiffstats
path: root/examples/pomodoro.py
blob: 143adf5d83de25abd0202dc0f0c3b562f87ace0a (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
from datetime import timedelta
from gi.repository import Notify
import os.path
from time import time
#from time import sleep
#from watchdog.observers import Observer
#from watchdog.events import LoggingEventHandler


POMO       = 1500  # 60 * 25
SHORT_REST = 300   # 60 * 5
LONG_REST  = 1800  # 60 * 30
NB_REST    = 4
WATCHDOG_FILE = os.path.join(os.path.expanduser('~'), '.i3', 'watchdog.log')


class Py3status:
    """
    Pomodoro method
    """
    pomo = POMO
    rest = SHORT_REST
    nb_rest = NB_REST
    default_response = {
        'full_text': '',
        'name': 'pomodoro',
    }

    def pomodoro(self, json, i3status_config):
        """
        This function will update a timer in the i3status bar for 25 minutes,
        then print a red "Break time" message for 5 minutes.
        """
        status = self.get_status()

        if status == 'stop':
            self.pomo = POMO
            self.rest = SHORT_REST
            self.nb_rest = NB_REST
            return self.default_response
        elif status == 'pause':
            return self.default_response

        # Else run normally
        if not self.pomo:
            self.rest -= 1
            timer = self.rest
            self.full_text = 'Break time'
            color = 'color_bad'
        else:
            self.pomo -= 1
            timer = self.pomo
            self.full_text = 'Pomodoro'
            color = 'color_degraded'

        if not timer:
            # Pomodoro or Rest is finished, send notification
            self.send_notification()

        response = self.default_response.copy()
        response.update({
            'cached_until' : time(),
            'color': i3status_config[color],
            'full_text': '{}: {}'.format(
                self.full_text, str(timedelta(seconds=timer))[2:]),
        })

        return (0, response)

    def get_status(self):
        """
        Read the watchdog file and get the status value. Value is modifying via
        i3 key mapping.

        Value can be:
            * start
            * pause
            * stop (default)
        """
        with open(WATCHDOG_FILE, 'r') as f:
            status = f.read()

        return status

    def send_notification(self):
        """
        :Requirements: libnotify, python-gobject
        """

        Notify.init('Pomodoro')
        notification = Notify.Notification.new (
            '<b>Attention:</b>',
            '{} is finished'.format(self.full_text),
            'dialog-information',
        )
        notification.show()

        if not self.rest:
            # Rest is finished, reinitialize data
            self.nb_rest -= 1
            self.pomo = POMO
            self.rest = SHORT_REST

        if not self.nb_rest:
            # Number of small rest reached, go for long rest
            self.rest = LONG_REST
            self.nb_rest = NB_REST


#if __name__ == "__main__":
    #event_handler = LoggingEventHandler()
    #observer = Observer()
    #observer.schedule(event_handler, path=WATCHDOG_FILE, recursive=True)
    #observer.start()
    #try:
        #while True:
            #sleep(1)
    #except KeyboardInterrupt:
        #observer.stop()
    #observer.join()