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
|
import time
import re
import subprocess
import sys
import argparse
def parse_duration(duration_str):
"""
Parse a duration string like '1h5m8s' or 'HH:MM:SS' into total seconds.
"""
# Check for HH:MM:SS format
if re.match(r"^\d{1,2}:\d{2}:\d{2}$", duration_str):
parts = duration_str.split(":")
hours = int(parts[0])
minutes = int(parts[1])
seconds = int(parts[2])
return hours * 3600 + minutes * 60 + seconds
# Match all occurrences of numbers followed by valid time units (h, m, s)
matches = re.findall(r"(\d+)([hms])", duration_str)
if not matches:
raise ValueError("Invalid duration format. Use a combination of 'h', 'm', and 's' (e.g., '1h5m8s') or 'HH:MM:SS'.")
total_seconds = 0
for value, unit in matches:
value = int(value)
if unit == 'h':
total_seconds += value * 3600
elif unit == 'm':
total_seconds += value * 60
elif unit == 's':
total_seconds += value
else:
raise ValueError("Invalid duration unit. Use 'h', 'm', or 's'.")
return total_seconds
def start_timer(timer_name, duration_str):
"""
Run a timer with the given name and duration.
"""
try:
total_duration = parse_duration(duration_str)
except ValueError as e:
print(f"Error: {e}")
return
interval = 0.5 # Update interval in seconds (500ms)
notification_id = None # Store the ID of the current notification
start_time = time.time()
while True:
elapsed_time = time.time() - start_time
if elapsed_time > total_duration:
break
percentage = int((elapsed_time / total_duration) * 100)
message = f"{timer_name}: {percentage}% complete"
# Send or update the notification
if notification_id is None:
# First notification, store its ID
result = subprocess.run(
["dunstify", "-p", "-h", f"int:value:{percentage}", message],
stdout=subprocess.PIPE,
text=True
)
notification_id = result.stdout.strip() # Capture the notification ID
else:
# Update the existing notification
subprocess.run(
["dunstify", "-r", notification_id, "-h", f"int:value:{percentage}", message]
)
time.sleep(interval)
# Final notification when the timer is complete
subprocess.run(
["dunstify", "-r", notification_id, f"{timer_name} complete!", "-h", "int:value:100"]
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run a timer with notifications.")
parser.add_argument("-t", "--title", required=False, help="Title of the timer", default="Timer")
parser.add_argument("-d", "--duration", required=True, help="Duration of the timer (e.g., '1h5m8s' or 'HH:MM:SS')")
args = parser.parse_args()
start_timer(args.title, args.duration)
|