-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·132 lines (118 loc) · 4.75 KB
/
main.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
129
130
131
132
#!/usr/bin/env python3
"""
- Python3 script file to execute and work as reminder, target OS for macOS and Linux.
- The program will use `terminal-notifier` for macOS and `notify-send` for Linux.
- The program will take options,
- `--title <string>`
- `--body <string>`
- `--open <URL>`
- `--command <string>`
- `--timer <string>`
- `--background`
- `--show-progress`
- An example command would be like this:
```bash
> reminder --title "Start building" --body "github auth feat" --open "https://github.com/" --command 'echo hello' --timer 1h10m15s --background
> reminder --title "Break time" --body "10-minute break" --open "https://youtube.com/" --command 'echo yeah' --timer 10m --show-progress
```
"""
import argparse
import platform
import subprocess
import time
import re
import os
import sys
def parse_timer(timer_str):
"""Parse the timer string (e.g., '1h10m15s') and convert to seconds."""
pattern = r'(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?'
match = re.fullmatch(pattern, timer_str)
if not match:
raise ValueError(f"Invalid timer format: {timer_str}")
hours, minutes, seconds = (int(v) if v else 0 for v in match.groups())
return hours * 3600 + minutes * 60 + seconds
def send_notification(title, body, open_url, os_name):
"""Send a notification using the appropriate tool for the OS."""
if os_name == "Darwin": # macOS
cmd = [
"terminal-notifier",
"-sound", "default",
"-title", title,
"-message", body
]
if open_url:
cmd.extend(["-open", open_url])
try:
subprocess.run(cmd, check=True)
except FileNotFoundError as e:
print(f"Notification tool not found: {e}. Please ensure terminal-notifier is installed.")
sys.exit(1)
elif os_name == "Linux":
cmd = ["notify-send", title]
if body:
cmd.append(body)
try:
subprocess.run(cmd, check=True)
except FileNotFoundError as e:
print(f"Notification tool not found: {e}. Please ensure notify-send is installed.")
sys.exit(1)
if open_url:
try:
subprocess.run(["xdg-open", open_url], check=True)
except FileNotFoundError as e:
print(f"Notification tool or xdg-open not found: {e}. Please ensure xdg-utils is installed.")
sys.exit(1)
else:
raise OSError(f"Unsupported OS: {os_name}")
def execute_command(command):
"""Execute the specified command."""
if command:
try:
subprocess.run(command, shell=True, check=True)
except subprocess.CalledProcessError as e:
print(f"Failed to execute command: {e}")
def run_reminder(title, body, open_url, command, timer, show_progress):
msgs = [f"Well done!", f"You're welcome!"]
os_name = platform.system()
try:
if timer:
wait_time = parse_timer(timer)
else:
wait_time = 15 * 60
except ValueError as e:
print(e)
sys.exit(1)
if show_progress:
try:
from tqdm import tqdm
except ImportError:
print("The tqdm library is required for progress bar functionality. Install it using 'pip install tqdm'.")
print("\tYou can run the \"ywfm\" without '--show-progress' option.")
sys.exit(1)
print(f"Starting timer for {timer}...")
for _ in tqdm(range(wait_time), desc="Progress", ncols=80, unit="s"):
time.sleep(1)
print(msgs[0] if wait_time % 2 else msgs[1])
else:
time.sleep(wait_time)
send_notification(title, body or "", open_url, os_name)
if command:
execute_command(command)
def main():
parser = argparse.ArgumentParser(description="CLI Reminder tool with notifications.")
parser.add_argument("--title", required=True, help="Title for the reminder notification.")
parser.add_argument("--body", help="Body for the reminder notification.")
parser.add_argument("--open", help="URL to open with the notification.")
parser.add_argument("--command", help="Command to exeute after the timer.")
parser.add_argument("--timer", help="Timer duration (e.g., '1h10m15s').")
parser.add_argument("--background", action="store_true", help="Run the reminder in the background.")
parser.add_argument("--show-progress", action="store_true", help="Show a progress bar for the countdown.")
args = parser.parse_args()
if args.background:
pid = os.fork()
if pid > 0:
print(f"Reminder running in background with PID {pid}.")
sys.exit(0)
run_reminder(args.title, args.body, args.open, args.command, args.timer, args.show_progress)
if __name__ == "__main__":
main()