@@ -35,21 +35,34 @@ def enable_ansi_escape_codes(): # pragma: no cover
3535 )
3636
3737
38- def get_required_width (chars ):
39- """Calculate the minimum width required to display MM:SS format."""
40- # MM:SS format has 4 digits, 1 colon, and 1 space after each char
41- digit_width = max (len (line ) for line in chars ["0" ].splitlines ())
42- colon_width = max (len (line ) for line in chars [":" ].splitlines ())
43- # Total: 4 digits + 1 colon + 5 spaces (after each character)
44- return digit_width * 4 + colon_width + 5
38+ def _format_time_string (seconds ):
39+ """Return the MM:SS string used for display based on seconds."""
40+ seconds = max (0 , int (seconds ))
41+ minutes , seconds = divmod (seconds , 60 )
42+ return f"{ minutes :02d} :{ seconds :02d} "
4543
4644
47- def get_chars_for_terminal ():
48- """Return the largest CHARS dictionary that fits in the current terminal."""
45+ def get_required_width (chars , time_string ):
46+ """Calculate the minimum width required to display the given time string."""
47+ char_widths = {
48+ char : max (len (line ) for line in glyph .splitlines ())
49+ for char , glyph in chars .items ()
50+ }
51+ # Each character in the timer output has a trailing space appended
52+ return sum (char_widths [char ] + 1 for char in time_string )
53+
54+
55+ def get_chars_for_terminal (seconds = 0 ):
56+ """Return the largest CHARS dictionary that fits in the current terminal.
57+
58+ Args:
59+ seconds: Current countdown value, used to account for wide minute values.
60+ """
4961 width , height = shutil .get_terminal_size ()
62+ time_string = _format_time_string (seconds )
5063 for size in DIGIT_SIZES :
5164 chars = CHARS_BY_SIZE [size ]
52- required_width = get_required_width (chars )
65+ required_width = get_required_width (chars , time_string )
5366 # For size 3 (smallest multi-line), allow it without padding
5467 # For larger sizes, require 1 line of padding on top and bottom (2 total)
5568 padding_needed = 0 if size == 3 else 2
0 commit comments