Skip to content

Instantly share code, notes, and snippets.

@shereifsrf
Created August 2, 2024 05:12
Show Gist options
  • Save shereifsrf/a5acfa6cb31e0eadb914f0f533ee9169 to your computer and use it in GitHub Desktop.
Save shereifsrf/a5acfa6cb31e0eadb914f0f533ee9169 to your computer and use it in GitHub Desktop.
import time
import winsound
import threading
import msvcrt # Only works on Windows
def user_prompt() -> tuple[int, int]:
focus_dur = int(input("Enter the focus duration (minutes): "))
break_dur = int(input("Enter the break duration (minutes): "))
return focus_dur, break_dur
def play_sound(stop_event):
while not stop_event.is_set():
winsound.Beep(1000, 1000) # Beep at 1000 Hz for 1000 ms (1 second)
time.sleep(1)
def countdown_timer(seconds: int):
while seconds:
mins, secs = divmod(seconds, 60)
timer = "{:02d}:{:02d}".format(mins, secs)
print(timer, end="\r")
time.sleep(1)
seconds -= 1
def stop_beeping_on_keypress(stop_event):
print("\nPress any key to stop the beeping.")
msvcrt.getch() # Wait for a key press
stop_event.set()
def main():
focus_dur, break_dur = user_prompt()
# Call focus duration
countdown_timer(focus_dur) # Convert minutes to seconds
stop_event = threading.Event()
beep_thread = threading.Thread(target=play_sound, args=(stop_event,))
beep_thread.start()
stop_beeping_on_keypress(stop_event)
beep_thread.join()
# Call break duration
countdown_timer(break_dur) # Convert minutes to seconds
stop_event = threading.Event()
beep_thread = threading.Thread(target=play_sound, args=(stop_event,))
beep_thread.start()
stop_beeping_on_keypress(stop_event)
beep_thread.join()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment