|
| 1 | +"""CodeFRAME TUI Dashboard — live terminal dashboard. |
| 2 | +
|
| 3 | +A Textual application showing tasks, events, and blockers |
| 4 | +with auto-refresh and keyboard navigation. |
| 5 | +""" |
| 6 | + |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +from textual.app import App, ComposeResult |
| 10 | +from textual.binding import Binding |
| 11 | +from textual.containers import Horizontal, Vertical |
| 12 | +from textual.reactive import reactive |
| 13 | +from textual.widgets import DataTable, Footer, Header, RichLog, Static |
| 14 | + |
| 15 | +from codeframe.core.workspace import Workspace |
| 16 | +from codeframe.tui.data_service import DashboardData, load_dashboard_data |
| 17 | + |
| 18 | + |
| 19 | +# Status → color mapping for task rows |
| 20 | +_STATUS_COLORS: dict[str, str] = { |
| 21 | + "DONE": "green", |
| 22 | + "IN_PROGRESS": "cyan", |
| 23 | + "READY": "yellow", |
| 24 | + "BACKLOG": "dim", |
| 25 | + "BLOCKED": "red", |
| 26 | + "FAILED": "red bold", |
| 27 | + "MERGED": "green dim", |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +class StatusBar(Static): |
| 32 | + """Top status bar showing task counts and workspace info.""" |
| 33 | + |
| 34 | + def update_from_data(self, data: DashboardData) -> None: |
| 35 | + counts = data.task_counts |
| 36 | + total = sum(counts.values()) |
| 37 | + done = counts.get("DONE", 0) + counts.get("MERGED", 0) |
| 38 | + active = counts.get("IN_PROGRESS", 0) |
| 39 | + blocked = counts.get("BLOCKED", 0) + counts.get("FAILED", 0) |
| 40 | + ready = counts.get("READY", 0) + counts.get("BACKLOG", 0) |
| 41 | + |
| 42 | + parts = [ |
| 43 | + f"[bold]{data.workspace_name}[/bold]", |
| 44 | + f"Tasks: {total}", |
| 45 | + f"[green]{done} done[/green]", |
| 46 | + f"[cyan]{active} active[/cyan]", |
| 47 | + f"[yellow]{ready} ready[/yellow]", |
| 48 | + ] |
| 49 | + if blocked > 0: |
| 50 | + parts.append(f"[red]{blocked} blocked/failed[/red]") |
| 51 | + if data.blocker_count > 0: |
| 52 | + parts.append(f"[red bold]{data.blocker_count} blockers[/red bold]") |
| 53 | + |
| 54 | + self.update(" | ".join(parts)) |
| 55 | + |
| 56 | + |
| 57 | +class DashboardApp(App): |
| 58 | + """CodeFRAME TUI Dashboard.""" |
| 59 | + |
| 60 | + CSS = """ |
| 61 | + Screen { |
| 62 | + layout: vertical; |
| 63 | + } |
| 64 | + #status-bar { |
| 65 | + height: 1; |
| 66 | + background: $surface; |
| 67 | + padding: 0 1; |
| 68 | + } |
| 69 | + #main-content { |
| 70 | + height: 1fr; |
| 71 | + } |
| 72 | + #task-panel { |
| 73 | + width: 2fr; |
| 74 | + border: solid $primary; |
| 75 | + } |
| 76 | + #right-panel { |
| 77 | + width: 1fr; |
| 78 | + } |
| 79 | + #event-log { |
| 80 | + height: 2fr; |
| 81 | + border: solid $secondary; |
| 82 | + } |
| 83 | + #blocker-panel { |
| 84 | + height: 1fr; |
| 85 | + border: solid $error; |
| 86 | + } |
| 87 | + DataTable { |
| 88 | + height: 1fr; |
| 89 | + } |
| 90 | + RichLog { |
| 91 | + height: 1fr; |
| 92 | + } |
| 93 | + .panel-title { |
| 94 | + background: $surface; |
| 95 | + padding: 0 1; |
| 96 | + text-style: bold; |
| 97 | + } |
| 98 | + """ |
| 99 | + |
| 100 | + TITLE = "CodeFRAME Dashboard" |
| 101 | + BINDINGS = [ |
| 102 | + Binding("q", "quit", "Quit"), |
| 103 | + Binding("r", "refresh", "Refresh"), |
| 104 | + Binding("tab", "focus_next", "Next Panel"), |
| 105 | + Binding("shift+tab", "focus_previous", "Prev Panel"), |
| 106 | + ] |
| 107 | + |
| 108 | + workspace: Optional[Workspace] = None |
| 109 | + refresh_interval: int = 2 |
| 110 | + data: reactive[Optional[DashboardData]] = reactive(None) |
| 111 | + |
| 112 | + def __init__( |
| 113 | + self, |
| 114 | + workspace: Workspace, |
| 115 | + refresh_interval: int = 2, |
| 116 | + **kwargs, |
| 117 | + ): |
| 118 | + super().__init__(**kwargs) |
| 119 | + self.workspace = workspace |
| 120 | + self.refresh_interval = refresh_interval |
| 121 | + |
| 122 | + def compose(self) -> ComposeResult: |
| 123 | + yield Header() |
| 124 | + yield StatusBar(id="status-bar") |
| 125 | + with Horizontal(id="main-content"): |
| 126 | + with Vertical(id="task-panel"): |
| 127 | + yield Static("Tasks", classes="panel-title") |
| 128 | + yield DataTable(id="task-table") |
| 129 | + with Vertical(id="right-panel"): |
| 130 | + with Vertical(id="event-log"): |
| 131 | + yield Static("Recent Events", classes="panel-title") |
| 132 | + yield RichLog(id="event-log-content", highlight=True, markup=True) |
| 133 | + with Vertical(id="blocker-panel"): |
| 134 | + yield Static("Open Blockers", classes="panel-title") |
| 135 | + yield RichLog(id="blocker-log", highlight=True, markup=True) |
| 136 | + yield Footer() |
| 137 | + |
| 138 | + def on_mount(self) -> None: |
| 139 | + # Set up task table columns |
| 140 | + table = self.query_one("#task-table", DataTable) |
| 141 | + table.add_columns("ID", "Title", "Status", "Priority") |
| 142 | + table.cursor_type = "row" |
| 143 | + |
| 144 | + # Initial data load |
| 145 | + self._refresh_data() |
| 146 | + |
| 147 | + # Auto-refresh |
| 148 | + self.set_interval(self.refresh_interval, self._refresh_data) |
| 149 | + |
| 150 | + def _refresh_data(self) -> None: |
| 151 | + """Load fresh data from the workspace and update all widgets.""" |
| 152 | + if not self.workspace: |
| 153 | + return |
| 154 | + |
| 155 | + data = load_dashboard_data(self.workspace) |
| 156 | + self.data = data |
| 157 | + |
| 158 | + self._update_status_bar(data) |
| 159 | + self._update_task_table(data) |
| 160 | + self._update_event_log(data) |
| 161 | + self._update_blocker_panel(data) |
| 162 | + |
| 163 | + if data.error: |
| 164 | + self.notify(f"Data loading error: {data.error}", severity="warning") |
| 165 | + |
| 166 | + def _update_status_bar(self, data: DashboardData) -> None: |
| 167 | + status_bar = self.query_one("#status-bar", StatusBar) |
| 168 | + status_bar.update_from_data(data) |
| 169 | + |
| 170 | + def _update_task_table(self, data: DashboardData) -> None: |
| 171 | + table = self.query_one("#task-table", DataTable) |
| 172 | + table.clear() |
| 173 | + |
| 174 | + for task in data.tasks: |
| 175 | + status_val = task.status.value if hasattr(task.status, "value") else str(task.status) |
| 176 | + color = _STATUS_COLORS.get(status_val, "white") |
| 177 | + table.add_row( |
| 178 | + task.id[:8], |
| 179 | + task.title[:50], |
| 180 | + f"[{color}]{status_val}[/{color}]", |
| 181 | + str(task.priority), |
| 182 | + ) |
| 183 | + |
| 184 | + def _update_event_log(self, data: DashboardData) -> None: |
| 185 | + log = self.query_one("#event-log-content", RichLog) |
| 186 | + log.clear() |
| 187 | + |
| 188 | + for event in reversed(data.events): # oldest first |
| 189 | + ts = event.created_at.strftime("%H:%M:%S") if hasattr(event.created_at, "strftime") else str(event.created_at)[:8] |
| 190 | + log.write(f"[dim]{ts}[/dim] {event.event_type}") |
| 191 | + |
| 192 | + def _update_blocker_panel(self, data: DashboardData) -> None: |
| 193 | + log = self.query_one("#blocker-log", RichLog) |
| 194 | + log.clear() |
| 195 | + |
| 196 | + if not data.blockers: |
| 197 | + log.write("[dim]No open blockers[/dim]") |
| 198 | + return |
| 199 | + |
| 200 | + for blocker in data.blockers: |
| 201 | + log.write(f"[red bold]{blocker.id[:8]}[/red bold]: {blocker.question[:60]}") |
| 202 | + |
| 203 | + def action_refresh(self) -> None: |
| 204 | + """Manual refresh via 'r' key.""" |
| 205 | + self._refresh_data() |
| 206 | + self.notify("Refreshed") |
0 commit comments