summaryrefslogtreecommitdiff
path: root/timers.py
diff options
context:
space:
mode:
authorhmj6502 <hashim@hmj6502.com>2025-12-19 10:08:37 +0000
committerhmj6502 <hashim@hmj6502.com>2025-12-19 10:08:37 +0000
commit980e83baf09953efb57b613448fa7f8660ef6973 (patch)
treed169d3b2aa522ac2d1e7596b63fafe847f022b70 /timers.py
parent8ad2527392fbe3fc69a1bca4fa093ace69f3289e (diff)
downloadlock-n-log-980e83baf09953efb57b613448fa7f8660ef6973.tar.gz
lock-n-log-980e83baf09953efb57b613448fa7f8660ef6973.tar.bz2
lock-n-log-980e83baf09953efb57b613448fa7f8660ef6973.zip
add stopwatch functionality
lots of code duplication... next step is to get rid of that
Diffstat (limited to 'timers.py')
-rw-r--r--timers.py31
1 files changed, 30 insertions, 1 deletions
diff --git a/timers.py b/timers.py
index 0141222..ee04e86 100644
--- a/timers.py
+++ b/timers.py
@@ -2,13 +2,15 @@ import datetime
import sys, termios, tty
import inputs
+frequency = 120 # 120Hz refresh
+
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
+ timeout = 1/frequency
old_settings = termios.tcgetattr(sys.stdin)
tty.setcbreak(sys.stdin.fileno())
@@ -22,11 +24,38 @@ def countdown(length):
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)
+
+def stopwatch():
+ """stopwatch timer; returns time elapsed"""
+ start = datetime.datetime.now()
+ accum = 0
+ timeout = 1/frequency
+
+ old_settings = termios.tcgetattr(sys.stdin)
+ tty.setcbreak(sys.stdin.fileno())
+ while True:
+ elapsed = datetime.datetime.now() - start
+
+ ch = inputs.getch_nb(timeout)
+ if ch == 'q':
+ break
+
+ # see https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
+ print("\x1b[2K\r" + inputs.delta_to_HM(elapsed), end="", flush=True)
+
+ accum += timeout
+ if accum >= 1:
+ accum = 0
+ termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
+
+ return (elapsed, start)