krz/nba-scores

A CLI/TUI app for NBA scores.

clone: git clone https://gitbay.org/krz/nba-scores.git

main: nba/tui/widgets.py · raw

 1"""
 2Custom Textual widgets for the NBA scores TUI.
 3"""
 4
 5from textual.reactive import reactive
 6from textual.widgets import Static
 7
 8
 9class ScoresWidget(Static):
10    """Displays the NBA scoreboard as a scrollable ANSI-formatted table."""
11
12
13class StandingsWidget(Static):
14    """Displays the NBA standings as a scrollable ANSI-formatted table."""
15
16
17class CountdownBar(Static):
18    """
19    Docked status bar that counts down to the next auto-refresh.
20
21    Maintains its own 1-second ticker and re-renders via a reactive attribute.
22    """
23
24    seconds: reactive[int] = reactive(60)
25
26    def __init__(self, interval: int, **kwargs) -> None:
27        super().__init__(**kwargs)
28        self._interval = interval
29        self.seconds = interval
30
31    def on_mount(self) -> None:
32        self.set_interval(1, self._tick)
33
34    def _tick(self) -> None:
35        if self.seconds > 0:
36            self.seconds -= 1
37
38    def watch_seconds(self, value: int) -> None:
39        if value <= 0:
40            self.update("Refreshing...")
41        else:
42            self.update(
43                f"Next refresh in {value}s  |  "
44                "\\[1-9] Box Score  |  \\[k] Bracket  |  "
45                "\\[</>] Leaders Cat  |  \\[r] Refresh  |  \\[q] Quit"
46            )
47
48    def reset(self, interval: int) -> None:
49        """Reset the countdown to the given interval."""
50        self._interval = interval
51        self.seconds = interval