-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpomodoro_timer.1s.py
executable file
·82 lines (63 loc) · 2.17 KB
/
pomodoro_timer.1s.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
#!/usr/bin/env python3
# <swiftbar.hideRunInTerminal>true</swiftbar.hideRunInTerminal>
# <swiftbar.hideDisablePlugin>true</swiftbar.hideDisablePlugin>
# <swiftbar.hideSwiftBar>true</swiftbar.hideSwiftBar>
import datetime
import os
import subprocess
import plugin
PLUGIN_ICON = "🍅"
MONOSPACED_FONT = "SFMono-Regular"
WORK_INTERVAL = datetime.timedelta(minutes=25)
OSASCRIPT_PATH = "/usr/bin/osascript"
NOTIFICATION_ON = True
NOTIFICATION_TEXT = "Well done! Feel free to take a break"
NOTIFICATION_SCRIPT = f'display notification "{NOTIFICATION_TEXT}" with title "{PLUGIN_ICON} Pomodoro Timer"'
TMP_FILE = os.path.join(os.environ["TMPDIR"], "swiftbar.pomodoro.deadline")
def main() -> None:
# has the script been run because of a user's click?
if os.getenv("CLICK") is not None:
if Timer.is_ticking():
Timer.stop()
else:
Timer.start()
return
time_prefix = ""
if Timer.is_ticking():
time = Timer.time_left()
if time.total_seconds() > 0:
minutes = time.seconds // 60
seconds = time.seconds % 60
time_prefix = f"{minutes:02}:{seconds:02} "
else:
Timer.stop()
if NOTIFICATION_ON:
display_notification()
plugin.print_menu_action(
f"{time_prefix}{PLUGIN_ICON}",
["CLICK=1", __file__], # run the same script on click
refresh=True,
font=MONOSPACED_FONT,
)
class Timer:
@classmethod
def start(cls) -> None:
with open(TMP_FILE, "w") as f:
deadline = datetime.datetime.now() + WORK_INTERVAL
f.write(deadline.isoformat())
@classmethod
def stop(cls) -> None:
os.remove(TMP_FILE)
@classmethod
def is_ticking(cls) -> bool:
return os.path.exists(TMP_FILE)
@classmethod
def time_left(cls) -> datetime.timedelta:
with open(TMP_FILE) as f:
text = f.read()
deadline = datetime.datetime.fromisoformat(text)
return deadline - datetime.datetime.now()
def display_notification() -> None:
subprocess.run([OSASCRIPT_PATH, "-e", NOTIFICATION_SCRIPT], check=True)
if __name__ == "__main__":
main()