teamctl-ui 0.8.4

Interactive TUI for teamctl — Triptych view, approvals modal, send-mail compose.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//! Triptych — the default Layout A. Sidebar + right-stack: an
//! Agents column on the left (current sidebar width), with Detail
//! stacked above Mailbox at 50/50 on the right. An Approvals stripe
//! is reserved at the top (rendered only when there's something to
//! surface) and a focus ring on the active pane.
//!
//! The Agents + Detail panes wire to live data:
//! - Agents lists `app.team.agents` with single-cell state glyphs
//!   driven by `data::state_glyph`. Selection is highlighted with
//!   the focus accent.
//! - Detail shows the last-N lines of `app.detail_buffer` (the
//!   tmux capture-pane scrollback for the focused agent), or an
//!   empty-state hint when no agent is selected.
//! - Mailbox stacks below Detail and pulls from the focused
//!   agent's mailbox tab buffer.

use ratatui::buffer::Buffer;
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::Line;
use ratatui::widgets::{Block, Borders, Paragraph, Widget};

use crate::app::App;
use crate::data::{state_glyph, tree_row_meta, AgentInfo, TreeRowMeta};
use crate::mailbox::{render_row, MailboxTab};
use crate::theme::ColorMode;

/// Top-level layout selector for the main view (Stage::Triptych).
/// PR-UI-1..5 used the Triptych shape exclusively; PR-UI-6 adds
/// Wall (orchestrator overview, up to 4 tiles + scroll) and
/// MailboxFirst (channel-feed centric for cross-team triage).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MainLayout {
    Triptych,
    Wall,
    MailboxFirst,
}

impl MainLayout {
    /// `Ctrl+W` (or standalone `w` from the SPEC chord map)
    /// toggles between Triptych ↔ Wall.
    pub fn toggle_wall(self) -> Self {
        if matches!(self, MainLayout::Wall) {
            MainLayout::Triptych
        } else {
            MainLayout::Wall
        }
    }

    /// `Ctrl+M` toggles between Triptych ↔ MailboxFirst.
    pub fn toggle_mailbox_first(self) -> Self {
        if matches!(self, MainLayout::MailboxFirst) {
            MainLayout::Triptych
        } else {
            MainLayout::MailboxFirst
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Pane {
    Roster,
    Detail,
    Mailbox,
}

impl Pane {
    /// `Tab` cycles `Agents → Detail → Mailbox → Agents`. With the
    /// sidebar + right-stack geometry that reads spatially as
    /// left → top-right → bottom-right → wrap to left.
    pub fn next(self) -> Self {
        match self {
            Pane::Roster => Pane::Detail,
            Pane::Detail => Pane::Mailbox,
            Pane::Mailbox => Pane::Roster,
        }
    }

    /// `Shift+Tab` cycles backward — `Agents → Mailbox → Detail →
    /// Agents`. Closes the no-easy-exit-from-mailbox UX gap: the
    /// operator Tabs into Mailbox, then Shift+Tab backs out cleanly
    /// without the `q`-confirm round-trip.
    pub fn prev(self) -> Self {
        match self {
            Pane::Roster => Pane::Mailbox,
            Pane::Detail => Pane::Roster,
            Pane::Mailbox => Pane::Detail,
        }
    }
}

pub fn draw(f: &mut ratatui::Frame<'_>, area: Rect, app: &App) {
    Triptych { app }.render(area, f.buffer_mut());
}

pub struct Triptych<'a> {
    pub app: &'a App,
}

impl Widget for Triptych<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        // PR-UI-4: the approvals stripe takes one line at the top
        // when there's at least one pending approval. The
        // `stripe_visible` const PR-UI-1 scaffolded as `false` is
        // now `app.has_pending_approvals()`.
        let stripe_visible = self.app.has_pending_approvals();
        let body = if stripe_visible {
            let v = Layout::default()
                .direction(Direction::Vertical)
                .constraints([Constraint::Length(1), Constraint::Min(0)])
                .split(area);
            render_approvals_stripe(buf, v[0], self.app);
            v[1]
        } else {
            area
        };

        // Outer split: Agents sidebar on the left at the same
        // 28-cell width the previous Triptych Roster column used,
        // then a right-hand stack that fills the rest of the row.
        let outer = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Length(28), // agents sidebar
                Constraint::Min(0),     // right-stack
            ])
            .split(body);

        // Inner split inside the right stack: Detail above at 60%,
        // Mailbox below at 40%. The ratio survives terminal-resize
        // events — `Constraint::Ratio` re-applies on every render.
        let right_stack = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Ratio(3, 5), Constraint::Ratio(2, 5)])
            .split(outer[1]);

        render_agents(buf, outer[0], self.app);
        render_detail(buf, right_stack[0], self.app);
        render_mailbox(buf, right_stack[1], self.app);
    }
}

fn render_approvals_stripe(buf: &mut Buffer, area: Rect, app: &App) {
    let n = app.pending_approvals.len();
    let plural = if n == 1 { "" } else { "s" };
    let text = format!("⚠  approvals: {n} pending{plural} — `a` to review");
    // Bright accent + reversed for the stripe — same affordance
    // pattern as the focused-pane border, applied to a full row so
    // the warning reads in any colour mode.
    let style = Style::default()
        .fg(app.capabilities.accent())
        .add_modifier(Modifier::REVERSED | Modifier::BOLD);
    Paragraph::new(text)
        .style(style)
        .alignment(Alignment::Left)
        .render(area, buf);
}

fn render_agents(buf: &mut Buffer, area: Rect, app: &App) {
    let focused = app.focused_pane == Pane::Roster;
    let block = pane_block("AGENTS", focused, app);
    let inner = block.inner(area);
    block.render(area, buf);

    if app.team.agents.is_empty() {
        let empty = Paragraph::new("(no agents)")
            .style(Style::default().fg(app.capabilities.muted()))
            .alignment(Alignment::Center);
        empty.render(inner, buf);
        return;
    }

    let ascii = matches!(app.capabilities.color, ColorMode::Monochrome);
    // T-211: pair each row with tree-render metadata so `agent_line`
    // can draw `├─` / `└─` glyphs for `reports_to` children. Teams
    // with no `reports_to` usage produce all-depth-0 metas and the
    // existing flat-render output is byte-identical (no prefix bytes
    // for depth 0).
    let metas = tree_row_meta(&app.team.agents);
    let lines: Vec<Line<'_>> = app
        .team
        .agents
        .iter()
        .zip(metas.iter())
        .enumerate()
        .map(|(i, (info, meta))| agent_line(info, *meta, Some(i) == app.selected_agent, ascii, app))
        .collect();
    let para = Paragraph::new(lines).alignment(Alignment::Left);
    para.render(inner, buf);
}

/// Render the indent + branch glyph for a tree row at the given
/// depth. Depth 0 produces a single leading space — same as today's
/// flat-list shape. Depth 1 produces ` ├─ ` / ` └─ ` (or ` |- ` /
/// `` `- `` in ASCII), with a matching leading space so the branch
/// glyph sits one indent past the depth-0 leading column (owner
/// alignment review, tg msg 1892). Depth >= 2 isn't a v1 goal per
/// the issue's non-goal list; the renderer clamps to the depth-1
/// shape behind a single extra indent so a chained schema (if one
/// ever slips past validation) at least lays out top-to-bottom
/// without crashing.
fn tree_prefix(meta: TreeRowMeta, ascii: bool) -> String {
    let branch = match (meta.is_last_sibling, ascii) {
        (false, false) => "├─",
        (true, false) => "└─",
        (false, true) => "|-",
        (true, true) => "`-",
    };
    match meta.depth {
        0 => " ".to_string(),
        1 => format!(" {branch} "),
        // Defensive clamp for any chain past v1's schema scope.
        _ => format!("   {branch} "),
    }
}

fn agent_line<'a>(
    info: &'a AgentInfo,
    meta: TreeRowMeta,
    selected: bool,
    ascii: bool,
    app: &App,
) -> Line<'a> {
    let glyph = state_glyph(info, ascii);
    // T-160: roster prefers the operator's display_name when set,
    // falling back to the YAML key (`info.agent`) — the canonical id
    // would over-prefix the project and is reserved for cross-project
    // surfaces like the detail header and wall-tile title.
    let label = info.display_name.as_deref().unwrap_or(&info.agent);
    let prefix = tree_prefix(meta, ascii);
    let display = format!("{prefix}{glyph}  {label}");
    let style = if selected {
        Style::default()
            .fg(app.capabilities.accent())
            .add_modifier(Modifier::REVERSED)
    } else {
        Style::default()
    };
    Line::styled(display, style)
}

fn render_detail(buf: &mut Buffer, area: Rect, app: &App) {
    let focused_pane = app.focused_pane == Pane::Detail;
    // T-108: when stream-mode is active, mark the border title with a
    // bright `[STREAM-KEYS]` tag so the pane the operator's typing
    // into is unambiguous even at a glance away from the statusline.
    let stream = matches!(app.stage, crate::app::Stage::StreamKeys);
    let title = match app
        .selected_agent
        .and_then(|i| app.team.agents.get(i))
        .map(|a| crate::data::agent_label(&app.team, &a.id))
    {
        Some(label) if stream => format!("DETAIL · {label}  [STREAM-KEYS]"),
        Some(label) => format!("DETAIL · {label}"),
        None if stream => "DETAIL  [STREAM-KEYS]".to_string(),
        None => "DETAIL".to_string(),
    };
    // While stream-mode is active, force the focus-ring style on the
    // detail pane regardless of `focused_pane`. Pane focus and stream-
    // mode are aligned in practice (entry is gated on detail focus),
    // but a future refactor that lets focus drift mid-mode shouldn't
    // visually downgrade the active stream pane.
    let outer_block = pane_block(&title, focused_pane || stream, app);
    let inner = outer_block.inner(area);
    outer_block.render(area, buf);

    if app.selected_agent.is_none() || app.team.agents.is_empty() {
        let muted = Style::default().fg(app.capabilities.muted());
        Paragraph::new("(select an agent on the left to follow its session)")
            .style(muted)
            .alignment(Alignment::Center)
            .render(inner, buf);
        return;
    }

    // PR-UI-7 fixup (qa Gap D): when `detail_splits` is non-empty
    // the detail pane subdivides — primary cell shows the focused
    // agent, additional cells show each split's agent. Operators
    // see the actual visual effect of `Ctrl+|` / `Ctrl+-`.
    if !app.detail_splits.is_empty() {
        render_detail_splits(buf, inner, app);
        return;
    }

    if app.detail_buffer.is_empty() {
        let muted = Style::default().fg(app.capabilities.muted());
        Paragraph::new("(no scrollback yet — agent may be starting up)")
            .style(muted)
            .alignment(Alignment::Center)
            .render(inner, buf);
        return;
    }

    // Tail the buffer to whatever fits; ratatui already clips lines
    // that overrun the rect, but pre-trimming saves a render-time
    // copy of thousands of lines we'd never see.
    let cap = inner.height as usize;
    let start = app.detail_buffer.len().saturating_sub(cap);
    // T-074 bug 3: parse the ANSI escape sequences captured by
    // `tmux capture-pane -e` into styled spans. `Line::raw` would
    // render the escapes as literal `\x1b[...` garbage; `into_text`
    // turns SGR codes (colours, bold, dim, …) into ratatui spans
    // so the agent's terminal output renders coloured. Lines that
    // contain no ANSI degrade gracefully to plain spans.
    use ansi_to_tui::IntoText;
    let lines: Vec<Line<'_>> = app.detail_buffer[start..]
        .iter()
        .flat_map(|s| match s.as_bytes().into_text() {
            Ok(text) => text.lines.into_iter().collect::<Vec<_>>(),
            Err(_) => vec![Line::raw(s.clone())],
        })
        .collect();
    Paragraph::new(lines).render(inner, buf);
}

/// Subdivide the detail-pane area when `detail_splits` is
/// non-empty. Composition (qa Gap D fixup):
///
/// - Cell 0 always shows the focused agent (the original detail
///   stream); cells 1..=N show each split's agent in order.
/// - The operator's mental model is "vertical adds a column,
///   horizontal adds a row." We honour that by folding vertical
///   splits into columns first, then horizontal splits subdivide
///   each column. With all-vertical or all-horizontal splits the
///   layout is straightforward; with a mix the columns grow
///   left-to-right and the horizontal splits stack within their
///   column.
/// - Each cell renders the agent's id + state glyph in the title
///   bar and the focused agent's `detail_buffer` lines as content.
///   Non-focused splits show a `(focus this split to stream)`
///   placeholder — multi-stream pane captures land in T-068
///   alongside the per-tile Wall captures.
/// - The focused split (per `app.selected_split`) gets the accent
///   focus-ring border; others get the muted border.
fn render_detail_splits(buf: &mut Buffer, area: Rect, app: &App) {
    use ratatui::layout::Direction as Dir;

    // Build the cell list: [focused, split_0, split_1, ...].
    // Each cell carries (agent_id, orientation_hint, is_focused_split).
    // `orientation_hint` for the focused agent defaults to Vertical
    // so the first split's chord choice drives the layout.
    let focused_id = app
        .selected_agent_id()
        .unwrap_or_else(|| "<no agent>".into());
    let mut cells: Vec<(String, crate::app::SplitOrientation, bool)> = Vec::new();
    cells.push((
        focused_id,
        // Match whatever the first split orientation is (or Vertical
        // if no splits — the no-splits path is short-circuited
        // above this fn's caller).
        app.detail_splits
            .first()
            .map(|(_, o)| *o)
            .unwrap_or(crate::app::SplitOrientation::Vertical),
        app.selected_split == 0 && app.focused_pane == Pane::Detail,
    ));
    for (i, (id, orientation)) in app.detail_splits.iter().enumerate() {
        cells.push((
            id.clone(),
            *orientation,
            app.selected_split == i + 1 && app.focused_pane == Pane::Detail,
        ));
    }

    // Group cells into columns: a Vertical split starts a new
    // column; Horizontal splits stack within the current column.
    let mut columns: Vec<Vec<usize>> = vec![vec![0]];
    for (idx, (_, orientation, _)) in cells.iter().enumerate().skip(1) {
        match orientation {
            crate::app::SplitOrientation::Vertical => columns.push(vec![idx]),
            crate::app::SplitOrientation::Horizontal => {
                columns.last_mut().expect("seed column").push(idx);
            }
        }
    }

    let col_count = columns.len();
    let col_constraints: Vec<Constraint> = (0..col_count)
        .map(|_| Constraint::Ratio(1, col_count as u32))
        .collect();
    let col_areas = ratatui::layout::Layout::default()
        .direction(Dir::Horizontal)
        .constraints(col_constraints)
        .split(area);

    for (col_idx, col_cells) in columns.iter().enumerate() {
        let col_area = col_areas[col_idx];
        let row_count = col_cells.len();
        let row_constraints: Vec<Constraint> = (0..row_count)
            .map(|_| Constraint::Ratio(1, row_count as u32))
            .collect();
        let row_areas = ratatui::layout::Layout::default()
            .direction(Dir::Vertical)
            .constraints(row_constraints)
            .split(col_area);
        for (row_idx, &cell_idx) in col_cells.iter().enumerate() {
            let cell_area = row_areas[row_idx];
            let (agent_id, _, is_focused_split) = &cells[cell_idx];
            render_split_cell(buf, cell_area, app, agent_id, *is_focused_split);
        }
    }
}

fn render_split_cell(
    buf: &mut Buffer,
    area: Rect,
    app: &App,
    agent_id: &str,
    is_focused_split: bool,
) {
    let ascii = matches!(app.capabilities.color, ColorMode::Monochrome);
    let glyph = app
        .team
        .agents
        .iter()
        .find(|a| a.id == agent_id)
        .map(|info| crate::data::state_glyph(info, ascii))
        .unwrap_or("?");
    let label = crate::data::agent_label(&app.team, agent_id);
    let title = format!(" {glyph} {label} ");
    let border = if is_focused_split {
        Style::default()
            .fg(app.capabilities.accent())
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(app.capabilities.muted())
    };
    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(border);
    let inner = block.inner(area);
    block.render(area, buf);

    // Only the focused split streams the live detail buffer.
    // Non-focused splits show the placeholder — multi-stream
    // captures land in T-068 alongside Wall's per-tile streaming.
    let muted = Style::default().fg(app.capabilities.muted());
    if !is_focused_split {
        Paragraph::new("(focus this split to stream)")
            .style(muted)
            .alignment(Alignment::Center)
            .render(inner, buf);
        return;
    }
    if app.detail_buffer.is_empty() {
        Paragraph::new("(no scrollback yet)")
            .style(muted)
            .alignment(Alignment::Center)
            .render(inner, buf);
        return;
    }
    let cap = inner.height as usize;
    let start = app.detail_buffer.len().saturating_sub(cap);
    let lines: Vec<Line<'_>> = app.detail_buffer[start..]
        .iter()
        .map(|s| Line::raw(s.clone()))
        .collect();
    Paragraph::new(lines).render(inner, buf);
}

fn render_mailbox(buf: &mut Buffer, area: Rect, app: &App) {
    let focused = app.focused_pane == Pane::Mailbox;
    let block = pane_block("MAILBOX", focused, app);
    let inner = block.inner(area);
    block.render(area, buf);

    if inner.height == 0 {
        return;
    }

    // Reserve the top line for the tab indicator; everything below
    // is rows from the active tab's buffer.
    let layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(1), Constraint::Min(0)])
        .split(inner);

    render_mailbox_tabs(buf, layout[0], app);
    render_mailbox_body(buf, layout[1], app);
}

fn render_mailbox_tabs(buf: &mut Buffer, area: Rect, app: &App) {
    // `Inbox  Sent  Channel  Wire` — active tab gets the focus
    // accent (REVERSED so it reads as a highlight bar even in
    // monochrome terminals where colour alone wouldn't carry the
    // signal).
    let active_style = Style::default()
        .fg(app.capabilities.accent())
        .add_modifier(Modifier::REVERSED);
    let muted = Style::default().fg(app.capabilities.muted());
    let mut spans: Vec<ratatui::text::Span<'_>> = Vec::with_capacity(7);
    for (i, tab) in MailboxTab::ALL.iter().enumerate() {
        if i > 0 {
            spans.push(ratatui::text::Span::styled("  ", muted));
        }
        let label = format!(" {} ", tab.label());
        let style = if app.mailbox_tab == *tab {
            active_style
        } else {
            muted
        };
        spans.push(ratatui::text::Span::styled(label, style));
    }
    Paragraph::new(Line::from(spans)).render(area, buf);
}

fn render_mailbox_body(buf: &mut Buffer, area: Rect, app: &App) {
    if app.selected_agent_id().is_none() {
        let muted = Style::default().fg(app.capabilities.muted());
        Paragraph::new("(select an agent)")
            .style(muted)
            .alignment(Alignment::Center)
            .render(area, buf);
        return;
    }

    let rows = app.mailbox.rows(app.mailbox_tab);
    if rows.is_empty() {
        let muted = Style::default().fg(app.capabilities.muted());
        Paragraph::new(app.mailbox_tab.empty_hint())
            .style(muted)
            .alignment(Alignment::Center)
            .render(area, buf);
        return;
    }

    // Tail to whatever fits — same shape as the detail pane.
    let cap = area.height as usize;
    let start = rows.len().saturating_sub(cap);
    // T-231: pass the active tab so render_row can pick the right
    // prefix (sender for Inbox/Channel/Wire, recipient for Sent).
    let lines: Vec<Line<'_>> = rows[start..]
        .iter()
        .map(|r| Line::raw(render_row(r, &app.team, app.mailbox_tab)))
        .collect();
    Paragraph::new(lines).render(area, buf);
}

fn pane_block<'a>(title: &'a str, focused: bool, app: &App) -> Block<'a> {
    let border = if focused {
        Style::default()
            .fg(app.capabilities.accent())
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(app.capabilities.muted())
    };
    Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(border)
}