diff options
| author | Christian Cleberg <[email protected]> | 2026-04-15 22:28:45 -0500 |
|---|---|---|
| committer | Christian Cleberg <[email protected]> | 2026-04-15 22:28:45 -0500 |
| commit | 4cbe592e0a0fae13f0a89918a66fd983dd84464f (patch) | |
| tree | c2aac9d6e3c7b0f44adf07b86385f2ce06f1cf10 /nba/standings.py | |
| parent | 00f695c765aead8aa0160a3c676cf85716178beb (diff) | |
| download | nba-scores-4cbe592e0a0fae13f0a89918a66fd983dd84464f.tar.gz nba-scores-4cbe592e0a0fae13f0a89918a66fd983dd84464f.tar.bz2 nba-scores-4cbe592e0a0fae13f0a89918a66fd983dd84464f.zip | |
feat: add Textual TUI with auto-refresh, leaders, playoff picture, and box score
Introduces an interactive terminal UI via the `--tui` flag backed by the
Textual framework. Scores and standings auto-refresh on a configurable
interval (default 60s, minimum 10s) using asyncio with nba_api calls
offloaded to a thread pool to keep the event loop unblocked.
New tabs:
- Scores / Standings (existing views, now live-updating)
- Leaders: top-25 by stat category, cycle with < / > keys
- Playoff Picture: conference seeding with clinch/elimination status
- Box Score: per-player live stats, load any game with keys 1–9
Refactored scores.py and standings.py to separate rendering from I/O
(get_scoreboard_table, get_east/west_standings_table), keeping the
existing static CLI path fully intact. Added -sc / -st shortcut flags.
Adds textual>=0.89.1,<7.0 dependency, build-system declaration for uv,
and package-data entry for the bundled .tcss stylesheet.
Diffstat (limited to 'nba/standings.py')
| -rw-r--r-- | nba/standings.py | 117 |
1 files changed, 60 insertions, 57 deletions
diff --git a/nba/standings.py b/nba/standings.py index 9b6a56e..3abb1c0 100644 --- a/nba/standings.py +++ b/nba/standings.py @@ -11,73 +11,76 @@ RED = "\033[91m" GREEN = "\033[32m" -def build_standings(standings) -> None: - """ - Prints team standings in two separate tables. - - Args: - standings (dict): Team standings data. - """ - eastern_data = [] - western_data = [] - eastern_rank = 1 - western_rank = 1 +def _build_conference_table(standings, conference: str) -> str: + """Build a formatted table string for one conference.""" + data = [] + rank = 1 for result_set in standings["resultSets"]: if result_set["name"] == "Standings": for team in result_set["rowSet"]: - conference = team[5] - team_name = team[4] + if team[5] != conference: + continue wins = team[12] losses = team[13] win_pct = team[14] - gb = team[37] - home_record = team[17] - away_record = team[18] - last_10 = team[19] streak = team[35] - if int(streak) < 0: - strk_color = f"{RED}{streak}{END}" - else: - strk_color = f"{GREEN}{streak}{END}" - - if conference == "East": - eastern_data.append( - [ - f"{eastern_rank}", - f"{team_name}", - f"{wins}-{losses}", - f"{win_pct:.3f}", - f"{gb}", - f"{strk_color}", - f"{last_10}", - f"{home_record}", - f"{away_record}", - ] - ) - eastern_rank += 1 - elif conference == "West": - western_data.append( - [ - f"{western_rank}", - f"{team_name}", - f"{wins}-{losses}", - f"{win_pct:.3f}", - f"{gb}", - f"{strk_color}", - f"{last_10}", - f"{home_record}", - f"{away_record}", - ] - ) - western_rank += 1 + strk_color = ( + f"{RED}{streak}{END}" if int(streak) < 0 else f"{GREEN}{streak}{END}" + ) + + data.append( + [ + f"{rank}", + team[4], + f"{wins}-{losses}", + f"{win_pct:.3f}", + team[37], + strk_color, + team[19], + team[17], + team[18], + ] + ) + rank += 1 headers = ["Rank", "Team", "W-L", "PCT", "GB", "STRK", "L10", "HOME", "AWAY"] + label = "Eastern" if conference == "East" else "Western" + return ( + f"{BOLD}{label} Conference Standings:{END}\n" + + tabulate(data, headers=headers, tablefmt="grid") + ) + + +def get_east_standings_table(standings) -> str: + """Returns the Eastern Conference standings as a formatted string.""" + return _build_conference_table(standings, "East") + - print(f"{BOLD}Eastern Conference Standings:{END}") - print(tabulate(eastern_data, headers=headers, tablefmt="grid")) +def get_west_standings_table(standings) -> str: + """Returns the Western Conference standings as a formatted string.""" + return _build_conference_table(standings, "West") + + +def get_standings_tables(standings) -> str: + """ + Builds and returns both conference standings as a formatted string. - print("\n") - print(f"{BOLD}Western Conference Standings:{END}") - print(tabulate(western_data, headers=headers, tablefmt="grid")) + Args: + standings (dict): Team standings data. + + Returns: + str: Formatted standings string with ANSI color codes for both conferences. + """ + return get_east_standings_table(standings) + "\n\n" + get_west_standings_table(standings) + + +def build_standings(standings) -> None: + """ + Prints team standings in two separate tables. + + Args: + standings (dict): Team standings data. + """ + print(get_standings_tables(standings)) |
