-
Notifications
You must be signed in to change notification settings - Fork 115
/
tomato.py
executable file
·128 lines (100 loc) · 3.92 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/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
seconds_slot = int(left_seconds % 60)
seconds_str = str(seconds_slot) if seconds_slot >= 10 else '0{}'.format(seconds_slot)
countdown = '{}:{} ⏰'.format(int(left_seconds / 60), seconds_str)
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):
'''
# macos desktop notification
terminal-notifier -> https://github.com/julienXX/terminal-notifier#download
terminal-notifier -message <msg>
# ubuntu desktop notification
notify-send
# voice notification
say -v <lang> <msg>
lang options:
- Daniel: British English
- Ting-Ting: Mandarin
- Sin-ji: Cantonese
'''
print(msg)
try:
if sys.platform == 'darwin':
# macos desktop notification
subprocess.run(['terminal-notifier', '-title', '🍅', '-message', msg])
subprocess.run(['say', '-v', 'Daniel', msg])
elif sys.platform.startswith('linux'):
# ubuntu desktop notification
subprocess.Popen(["notify-send", '🍅', msg])
else:
# windows?
# TODO: windows notification
pass
except:
# skip the notification error
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()