Spaces:
Running on Zero
Running on Zero
| """Deck viewer: what you're running, grouped and counted. Any key closes.""" | |
| from __future__ import annotations | |
| from collections import Counter | |
| from rich.table import Table | |
| from rich.text import Text | |
| from textual import events | |
| from textual.app import ComposeResult | |
| from textual.screen import ModalScreen | |
| from textual.widgets import Static | |
| from scrypt.engine.cards import Card | |
| class DeckViewScreen(ModalScreen): | |
| CSS = """ | |
| DeckViewScreen { align: center middle; } | |
| #deck-table { width: 60; max-height: 80%; padding: 1 2; background: $surface; | |
| border: solid grey; } | |
| """ | |
| def __init__(self, deck: list[Card], title: str = "your deck"): | |
| super().__init__() | |
| self.deck = deck | |
| self.title = title | |
| def compose(self) -> ComposeResult: | |
| counts = Counter(c.id for c in self.deck) | |
| specs = {c.id: c for c in self.deck} | |
| table = Table(title=f"{self.title} — {len(self.deck)} cards", expand=True) | |
| table.add_column("card") | |
| table.add_column("#", justify="right") | |
| table.add_column("stats", justify="center") | |
| table.add_column("cost") | |
| table.add_column("sigils") | |
| for cid, n in sorted(counts.items(), key=lambda kv: (-specs[kv[0]].power, kv[0])): | |
| spec = specs[cid] | |
| table.add_row( | |
| spec.name, str(n), f"{spec.power}⚔ {spec.health}♥", | |
| str(spec.cost), " ".join(spec.sigils), | |
| ) | |
| yield Static(table, id="deck-table") | |
| def on_key(self, event: events.Key) -> None: | |
| self.dismiss(None) | |