forked from coolcode/tomato-clock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tomato.py
executable file
·105 lines (82 loc) · 3.26 KB
/
tomato.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Pomodoro 番茄工作法 https://en.wikipedia.org/wiki/Pomodoro_Technique
# ====== 🍅 Tomato Clock =======
# ./tomato.py # start a 25 minutes tomato clock + 5 minutes break
# ./tomato.py -t # start a 25 minutes tomato clock
# ./tomato.py -t <n> # start a <n> minutes tomato clock
# ./tomato.py -b # take a 5 minutes break
# ./tomato.py -b <n> # take a <n> minutes break
# ./tomato.py -h # help
import sys
import time
import subprocess
WORK_MINUTES = 25
BREAK_MINUTES = 5
def main():
try:
if len(sys.argv) <= 1:
print(f'🍅 tomato {WORK_MINUTES} minutes. Ctrl+C to exit')
tomato(WORK_MINUTES, 'It is time to take a break')
print(f'🛀 break {BREAK_MINUTES} minutes. Ctrl+C to exit')
tomato(BREAK_MINUTES, 'It is time to work')
elif sys.argv[1] == '-t':
minutes = int(sys.argv[2]) if len(sys.argv) > 2 else WORK_MINUTES
print(f'🍅 tomato {minutes} minutes. Ctrl+C to exit')
tomato(minutes, 'It is time to take a break')
elif sys.argv[1] == '-b':
minutes = int(sys.argv[2]) if len(sys.argv) > 2 else BREAK_MINUTES
print(f'🛀 break {minutes} minutes. Ctrl+C to exit')
tomato(minutes, 'It is time to work')
elif sys.argv[1] == '-h':
help()
else:
help()
except KeyboardInterrupt:
print('\n👋 goodbye')
except Exception as ex:
print(ex)
exit(1)
def tomato(minutes, notify_msg):
start_time = time.perf_counter()
while True:
diff_seconds = int(round(time.perf_counter() - start_time))
left_seconds = minutes * 60 - diff_seconds
if left_seconds <= 0:
print('')
break
countdown = '{}:{} ⏰'.format(int(left_seconds / 60), int(left_seconds % 60))
duration = min(minutes, 25)
progressbar(diff_seconds, minutes * 60, duration, countdown)
time.sleep(1)
notify_me(notify_msg)
def progressbar(curr, total, duration=10, extra=''):
frac = curr / total
filled = round(frac * duration)
print('\r', '🍅' * filled + '--' * (duration - filled), '[{:.0%}]'.format(frac), extra, end='')
def notify_me(msg):
'''
terminal-notifier -> https://github.com/julienXX/terminal-notifier#download
terminal-notifier -message <msg>
say -v Ting-Ting <msg>
#Cantonese:
say -v Sin-ji <msg>
'''
print(msg)
try:
subprocess.run(['terminal-notifier', '-title', '🍅', '-message', msg])
subprocess.run(['say', '-v', 'Daniel', msg])
except:
pass
def help():
appname = sys.argv[0]
appname = appname if appname.endswith('.py') else 'tomato' # tomato is pypi package
print('====== 🍅 Tomato Clock =======')
print(f'{appname} # start a {WORK_MINUTES} minutes tomato clock + {BREAK_MINUTES} minutes break')
print(f'{appname} -t # start a {WORK_MINUTES} minutes tomato clock')
print(f'{appname} -t <n> # start a <n> minutes tomato clock')
print(f'{appname} -b # take a {BREAK_MINUTES} minutes break')
print(f'{appname} -b <n> # take a <n> minutes break')
print(f'{appname} -h # help')
if __name__ == "__main__":
main()