From 1dd32f5dd7bb228713206389646c54edfd585b0a Mon Sep 17 00:00:00 2001 From: hmj6502 Date: Fri, 19 Dec 2025 11:12:45 +0000 Subject: combine timer and stopwatch into one function they work effectively the same, so add if statements for minor changes. time_left no longer used at all --- main.py | 49 ++++++++++++++++++------------------------------- timers.py | 41 ++++++++++------------------------------- 2 files changed, 28 insertions(+), 62 deletions(-) diff --git a/main.py b/main.py index 897234c..c59ac4c 100755 --- a/main.py +++ b/main.py @@ -24,44 +24,31 @@ while not option: print("") if option == 't': + timer = "timer" print("timer length (in minutes)? ", end="", flush=True) timer_length = inputs.get_pos_int() while not timer_length: print("please enter a positive integer") print("timer length (in minutes)? ", end="", flush=True) timer_length = inputs.get_pos_int() - - time_left, start = timers.countdown(timer_length) - - print("\nfocus session finished!\nlogging...") - session = OrderedDict() - session["id"] = hashlib.sha256(str(start).encode('utf8')).hexdigest() - session["date"] = start.strftime("%Y-%m-%d") - session["start"] = start.strftime("%H:%M") - session["elapsed"] = int(timer_length - time_left.total_seconds()) - session["type"] = "focus" - session["tag"] = tag - session["timer"] = "timer" - session["ratio"] = 0.2 - #print(session) - - f.sesh_to_log(session, "lock-n-log.csv") - else: + timer = "stopwatch" + timer_length = 0 print("whenever you're ready, press enter to start", end="", flush=True) input() - elapsed, start = timers.stopwatch() - - print("\nfocus session finished!\nlogging...") - session = OrderedDict() - session["id"] = hashlib.sha256(str(start).encode('utf8')).hexdigest() - session["date"] = start.strftime("%Y-%m-%d") - session["start"] = start.strftime("%H:%M") - session["elapsed"] = int(elapsed.total_seconds()) - session["type"] = "focus" - session["tag"] = tag - session["timer"] = "stopwatch" - session["ratio"] = 0.2 - #print(session) - f.sesh_to_log(session, "lock-n-log.csv") +elapsed, start = timers.timer(timer_length) + +print("\nfocus session finished!\nlogging...") +session = OrderedDict() +session["id"] = hashlib.sha256(str(start).encode('utf8')).hexdigest() +session["date"] = start.strftime("%Y-%m-%d") +session["start"] = start.strftime("%H:%M") +session["elapsed"] = int(elapsed.total_seconds()) +session["type"] = "focus" +session["tag"] = tag +session["timer"] = timer +session["ratio"] = 0.2 +#print(session) + +f.sesh_to_log(session, "lock-n-log.csv") diff --git a/timers.py b/timers.py index ee04e86..cf8e91d 100644 --- a/timers.py +++ b/timers.py @@ -4,11 +4,10 @@ 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 +def timer(length=0): + """countdown and stopwatch; pass length as 0 for stopwatch""" start = datetime.datetime.now() - end = start + datetime.timedelta(seconds=time_left) # change to mins in final + end = start + datetime.timedelta(seconds=length) # change to mins in final accum = 0 timeout = 1/frequency @@ -16,42 +15,22 @@ def countdown(length): tty.setcbreak(sys.stdin.fileno()) while True: now = datetime.datetime.now() + elapsed = now - start time_left = end - now - if time_left.total_seconds() <= 0: + if length > 0 and 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) - -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 + if length > 0: + time_display = time_left + else: + time_display = elapsed # see https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 - print("\x1b[2K\r" + inputs.delta_to_HM(elapsed), end="", flush=True) + print("\x1b[2K\r" + inputs.delta_to_HM(time_display), end="", flush=True) accum += timeout if accum >= 1: -- cgit v1.2.3