Skip to content

Instantly share code, notes, and snippets.

@secoats
Created October 22, 2022 12:58
Show Gist options
  • Save secoats/fceca4ecec87b3b34524b666cf721c94 to your computer and use it in GitHub Desktop.
Save secoats/fceca4ecec87b3b34524b666cf721c94 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import time
import sys
import math
import winsound
alarm_frequency = 1000 # Hertz
alarm_duration = 80 # ms
alarm_ascii = '''
RING RING RING
.-.-.
((( (__I__) )))
.'_....._'.
/ / .12 . \\ \\
| | ' | ' | |
| | 9 / 3 | |
\ \ '.6.' / /
'.`-...-'.'
/'-- --'\\
`"""""""""`
TIME HAS RUN OUT
'''
mins = 0
if len(sys.argv) < 2:
mins = int(input("Minutes? > "))
else:
mins = int(sys.argv[1])
if mins <= 0:
print("Timer set for value zero or below")
sys.exit(0)
print(f"Timer set for {mins} minutes", end="\n\n")
seconds = mins * 60
current_timestamp = time.time()
stop_timestamp = current_timestamp + seconds
previous_line_length = 0
try:
while current_timestamp < stop_timestamp:
seconds_left = stop_timestamp - current_timestamp
running_minutes = math.floor(seconds_left / 60)
running_seconds = math.ceil(seconds_left % 60)
msg = f" {running_minutes} minutes left and {running_seconds} seconds"
if len(msg) > previous_line_length:
previous_line_length = len(msg)
else:
msg += (previous_line_length - len(msg)) * " "
print(msg, end="\r")
time.sleep(0.2)
current_timestamp = time.time()
print(" " * previous_line_length)
print(alarm_ascii)
for _ in range(5):
winsound.Beep(alarm_frequency, alarm_duration)
time.sleep(0.5)
except KeyboardInterrupt:
print("\n")
print("Timer was stopped manually")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment