Coverage for src/countdown/timer.py: 100%
18 statements
« prev ^ index » next coverage.py v7.11.1, created at 2026-03-27 20:23 -0700
« prev ^ index » next coverage.py v7.11.1, created at 2026-03-27 20:23 -0700
1"""Time parsing and formatting utilities."""
3import re
5DURATION_RE = re.compile(
6 r"""
7 ^
8 (?: # Optional minutes
9 ( \d+ ) # one or more digits
10 m # "m"
11 )?
12 (?: # Optional seconds
13 ( \d+ ) # one or more digits
14 s # "s"
15 )?
16 $
17""",
18 re.VERBOSE,
19)
22def duration(string):
23 """Convert given XmXs string to seconds (as an integer)."""
24 match = DURATION_RE.search(string)
25 if not match:
26 raise ValueError(f"Invalid duration: {string}")
27 minutes, seconds = match.groups()
28 return int(minutes or 0) * 60 + int(seconds or 0)
31def get_number_lines(seconds, chars):
32 """Return list of lines which make large MM:SS glyphs for given seconds.
34 Args:
35 seconds: The time in seconds to format
36 chars: Dictionary of character glyphs to use for rendering
38 Returns:
39 List of strings, one per line of the ASCII art display
40 """
41 digit_height = len(next(iter(chars.values())).splitlines())
42 lines = [""] * digit_height
43 minutes, seconds = divmod(seconds, 60)
44 time = f"{minutes:02d}:{seconds:02d}"
45 for char in time:
46 char_lines = chars[char].splitlines()
47 for i, line in enumerate(char_lines):
48 lines[i] += line + " "
49 return lines