-
Notifications
You must be signed in to change notification settings - Fork 0
/
pomodoro.py
executable file
·85 lines (76 loc) · 2.58 KB
/
pomodoro.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
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
from __future__ import print_function, division, absolute_import
from datetime import timedelta as td
import itertools
import select
import sys
import termios
import time
import tty
from dwanderson import Color
basic_pomo = (((25, 'Work'), (5, 'Break')) * 3 + (25, 'Work') + (15, 'BREAK'))
## refactor...
def pomo_input(sec=1):
old_tty = termios.tcgetattr(sys.stdin)
paused = False
response = None
while True:
try:
tty.setcbreak(sys.stdin.fileno())
if select.select([sys.stdin], [], [], sec) == ([sys.stdin], [], []):
letter = sys.stdin.read(1).lower()
if letter == 'q':
print('\r{:<30}'.format(Color.colorize("Quittin", "GRAY")),
end='\n')
response = False
elif letter == 'n':
print('\r{:<15}'.format(Color.colorize("NEXT", "GRAY")),
end='')
time.sleep(1)
response = True
elif letter in ('p', 'u') and paused:
response = 'unpause'
elif letter == 'p' and not paused:
paused = True
print("\r{}".format(
Color.colorize("Press 'p' or 'u' to unpause!", "GRAY")),
end='')
if response is not None or not paused:
break
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_tty)
return response
def countdown(t=td(minutes=0), caption=''):
if caption:
caption += ' - '
if caption.startswith('Work'):
clr = "GREEN"
else:
clr = "RED"
try:
while t > td(seconds=0):
out = "\r{}{:<15}".format(Color.colorize(caption + str(t), clr),'')
print(out, end='')
t -= td(seconds=1)
response = pomo_input()
if response in (True, False):
return response
return True
except KeyboardInterrupt as ki:
return False
finally:
sys.stdout.write('\b' * (7 + len(caption)))
sys.stdout.flush()
def pomodoro_cycle(pattern=basic_pomo):
print("KEYS: 'q'-Quit; 'p'-Pause; 'p'/'u'-Unpause; 'n'-Next")
pattern_cycle = itertools.cycle(pattern)
running = True
while running:
mins, caption = next(pattern_cycle)
running = countdown(td(minutes=mins), caption)
return
##############################################################################
def main():
pomodoro_cycle()
if __name__ == '__main__':
main()