Skip to content

Commit

Permalink
Start paused.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottgasch committed Sep 7, 2024
1 parent fe3ad37 commit 6495213
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions examples/timer/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@


class Timer:
def __init__(self, duration: float):
self.running = True
self.start = time.time()
def __init__(self, duration: float, initially_running: bool = True):
self.running = initially_running
self.start_time = time.time()
self.original_duration = duration
self.duration = duration
self.run_number = 1
Expand All @@ -45,7 +45,7 @@ def get_remaining_time(self) -> float:
def get_elapsed_time(self) -> float:
if self.running:
now = time.time()
return now - self.start + self.elapsed_before_pause
return now - self.start_time + self.elapsed_before_pause
else:
return self.elapsed_before_pause

Expand All @@ -55,14 +55,14 @@ def is_expired(self):

def reset(self):
self.run_number += 1
self.start = time.time()
self.start_time = time.time()
self.duration = self.original_duration
self.elapsed_before_pause = 0
self.running = True

def reset_and_pause(self):
self.run_number += 1
self.start = time.time()
self.start_time = time.time()
self.duration = self.original_duration
self.elapsed_before_pause = 0
self.running = False
Expand All @@ -72,15 +72,18 @@ def pause(self):
self.running = False

def unpause(self):
self.start = time.time()
self.duration = self.original_duration - self.elapsed_before_pause
self.running = True
if not self.running:
self.start_time = time.time()
self.duration = self.original_duration - self.elapsed_before_pause
self.running = True

start = unpause


@bootstrap.initialize
def main() -> Optional[int]:
duration = config.config["time_limit"]
timer = Timer(duration.seconds)
timer = Timer(duration.seconds, not config.config["pause_between"])
print("Countdown timer... [space]=pause/unpause, [r]eset, [q]uit (or ^C).")

with input_utils.KeystrokeReader() as get_keystroke:
Expand All @@ -96,7 +99,10 @@ def main() -> Optional[int]:
timer.unpause()
elif key == 'r':
print(end="\r\n")
timer.reset()
if config.config["pause_between"]:
timer.reset_and_pause()
else:
timer.reset()
elif key == 'q' or key == chr(3):
sys.exit(0)

Expand Down

0 comments on commit 6495213

Please sign in to comment.