forked from brumar/pomodoro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pomo_runner.py
executable file
·77 lines (65 loc) · 2.76 KB
/
pomo_runner.py
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
#! /usr/bin/python3
import argparse
from subprocess import call
from threading import Thread
from constants import *
from util import timedelta_str, Stage, PomodoroState
MAX_LINE_LEN = 50
ONE_TIME_ONLY = True
_kill = False
pomo_state = None
# TODO: use dunst
def notify_user(title, message):
if ENABLE_DESKTOP_NOTIFS:
try:
call(['notify-send', title, message, '-t', '1000'])
except FileNotFoundError:
print("Skipping desktop notification because `notify-send` wasn't recognized.")
def update_progress_line(td):
print(timedelta_str(td).ljust(MAX_LINE_LEN), end='\r')
def run_stage(stage, progress_callback=None):
global pomo_state
t = Thread(target=pomo_state.run, args=(stage, 0.2, progress_callback))
t.start()
try:
t.join()
except KeyboardInterrupt as interrupt:
pomo_state.kill()
t.join()
raise interrupt
parser = argparse.ArgumentParser(description='A Pomodoro CLI.')
parser.add_argument('--interactive', dest='interactive', action='store_true')
parser.add_argument('--cycle', dest='cycle', action='store_false')
parser.add_argument('--minutes', dest='minutes', type=int, default=ACTIVE_STAGE_MINUTES)
args = parser.parse_args()
if args.interactive:
print("Running in interactive mode")
try:
pomo_state = PomodoroState(ACTIVE_STAGE_MINUTES, REST_STAGE_MINUTES, STATE_FILE)
while True:
input("Press <enter> to begin a pomodoro.")
run_stage(Stage.ACTIVE, progress_callback=update_progress_line)
pomo_state.prep_for_rest()
notify_user("Pomodoro #%d completed" % pomo_state.pomos_completed, "Time for the rest stage")
print("Time for the rest stage.")
run_stage(Stage.REST, progress_callback=update_progress_line)
pomo_state.prep_for_active()
notify_user("Rest stage #%d completed" % pomo_state.pomos_completed,
"Go to the CLI to start pomodoro #%d" % (pomo_state.pomos_completed + 1))
print("Rest stage finished - You've completed %d pomodoro(s)." % pomo_state.pomos_completed)
except KeyboardInterrupt:
pomo_state.kill()
print("Finished - You completed a total of %d pomodoro(s)." % pomo_state.pomos_completed)
else:
# Non-interactive, should be able to just run in the background.
print("Running in non-interactive mode")
pomo_state = PomodoroState(args.minutes, REST_STAGE_MINUTES, STATE_FILE)
while True:
run_stage(Stage.ACTIVE)
notify_user("Pomodoro completed", "Time for the rest stage")
if not cycle:
break
pomo_state.prep_for_rest()
run_stage(Stage.REST)
pomo_state.prep_for_active()
notify_user("Rest stage completed", "Time to get back to work")