summaryrefslogtreecommitdiff
path: root/timers.py
diff options
context:
space:
mode:
authorhmjsp <hmjsp@liber8bits.com>2025-12-19 09:45:00 +0000
committerhmjsp <hmjsp@liber8bits.com>2025-12-19 09:45:00 +0000
commit8a058b3c4b56cf3145bbe113b2040b7b6e9d04d1 (patch)
treec9b98f03a02acf78fb6e4fb6af657369c92c1bf0 /timers.py
parent84547002a57ed05f1d874ad607bd29816c1d8e30 (diff)
downloadchronologgy-8a058b3c4b56cf3145bbe113b2040b7b6e9d04d1.zip
move countdown timer to seperate file
Diffstat (limited to 'timers.py')
-rw-r--r--timers.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/timers.py b/timers.py
new file mode 100644
index 0000000..0141222
--- /dev/null
+++ b/timers.py
@@ -0,0 +1,32 @@
+import datetime
+import sys, termios, tty
+import inputs
+
+def countdown(length):
+ """countdown timer; length in seconds. returns start and time left (>0 if quit early)"""
+ time_left = length
+ start = datetime.datetime.now()
+ end = start + datetime.timedelta(seconds=time_left) # change to mins in final
+ accum = 0
+ timeout = 1/120 # 120Hz refresh
+
+ old_settings = termios.tcgetattr(sys.stdin)
+ tty.setcbreak(sys.stdin.fileno())
+ while True:
+ now = datetime.datetime.now()
+ time_left = end - now
+ if time_left.total_seconds() <= 0:
+ break
+
+ ch = inputs.getch_nb(timeout)
+ if ch == 'q':
+ print("\nexited early!")
+ break
+ # see https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
+ print("\x1b[2K\r" + inputs.delta_to_HM(time_left), end="", flush=True)
+ accum += timeout
+ if accum >= 1:
+ accum = 0
+ termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
+
+ return (time_left, start)