//! Screen 1: sessions overview (ORCHESTRATOR.md 7.1, `Main.dc.html`). use super::theme::{self, bold, fg, inverted}; use crate::view::{Chip, EventLine, Mark, Need, Overview, Row, Status, hms}; use ratatui::Frame; use ratatui::layout::{Constraint, Layout, Rect}; use ratatui::style::{Color, Style}; use ratatui::text::{Line, Span}; use ratatui::widgets::{Block, Paragraph}; use unicode_width::UnicodeWidthStr; /// What the header shows that is not derived from events. pub struct Header { pub repo: String, pub rev: String, pub now_ms: u64, } const W_ID: usize = 5; const W_STATUS: usize = 7; const W_TASK: usize = 24; const W_TESTS: usize = 10; const W_DIFF: usize = 10; const W_CTX: usize = 10; pub fn draw(f: &mut Frame, o: &Overview, h: &Header, sel: usize, need_sel: usize) { let area = f.area(); f.render_widget( Block::new().style(Style::new().bg(theme::BG).fg(theme::TEXT)), area, ); let rows_h = (o.rows.len().max(1) * 2 + 3) as u16; let sessions_h = rows_h.clamp(5, area.height.saturating_sub(14).max(5)); let [head, stats, sessions, lower, keys] = Layout::vertical([ Constraint::Length(1), Constraint::Length(1), Constraint::Length(sessions_h), Constraint::Fill(1), Constraint::Length(1), ]) .areas(area); let [needs, events] = Layout::horizontal([Constraint::Length(62), Constraint::Fill(1)]).areas(lower); f.render_widget(header_line(o, h, head.width as usize), head); f.render_widget(stats_line(o), stats); sessions_box(f, sessions, o, sel); needs_box(f, needs, &o.needs, need_sel); events_box(f, events, &o.events); f.render_widget(key_bar(), keys); } /// Cuts `s` to `w` display columns, ending in `…` when it was too long. fn fit(s: &str, w: usize) -> String { if s.width() <= w { return s.to_string(); } let mut out = String::new(); for c in s.chars() { if out.width() + 1 >= w { break; } out.push(c); } out.push('…'); out } /// Appends `parts` and pads the cell to `w` columns plus one gap column. fn cell<'a>(line: &mut Vec>, parts: Vec>, w: usize) { let used: usize = parts.iter().map(|s| s.width()).sum(); line.extend(parts); line.push(Span::raw(" ".repeat((w + 1).saturating_sub(used)))); } fn bar(done: usize, total: usize) -> Vec> { let n = total.min(24); let d = (done * n).checked_div(total).unwrap_or(0); vec![ Span::styled("█".repeat(d), fg(theme::OK)), Span::styled("█".repeat(n - d), fg(theme::BORDER)), ] } fn header_line(o: &Overview, h: &Header, width: usize) -> Line<'static> { let goal = if o.goal.is_empty() { "no goal set" } else { &o.goal }; let right = format!("{} · {} {}", h.repo, h.rev, hms(h.now_ms)); let left_fixed = " hx·orch ".width() + " goal ".width(); let goal_w = width.saturating_sub(left_fixed + right.width() + 2); let goal = fit(goal, goal_w); let gap = goal_w.saturating_sub(goal.width()) + 2; Line::from(vec![ Span::styled(" hx·orch ", inverted(theme::RUN)), Span::styled(" goal ", fg(theme::DIM)), Span::styled( goal, Style::new().add_modifier(ratatui::style::Modifier::BOLD), ), Span::raw(" ".repeat(gap)), Span::styled(right, fg(theme::DIM)), ]) } fn stats_line(o: &Overview) -> Line<'static> { let mut v = vec![Span::styled(" tasks ", fg(theme::DIM))]; if o.total == 0 { v.push(Span::raw("—")); } else { v.extend(bar(o.merged, o.total)); v.push(Span::raw(format!(" {}/{} merged", o.merged, o.total))); } v.push(Span::styled(" acceptance ", fg(theme::DIM))); match o.checks { Some((ok, total)) => { v.extend(bar(ok, total)); v.push(Span::raw(format!(" {ok}/{total} checks"))); } None => v.push(Span::raw("—")), } v.push(Span::raw(" ")); v.push(Span::styled( format!("{} running", o.running), fg(theme::RUN), )); v.push(Span::raw(" · ")); v.push(Span::styled( format!("{} waiting on you", o.waiting), fg(theme::WAIT), )); v.push(Span::raw(" · ")); v.push(Span::styled( format!("{} blocked", o.blocked), fg(theme::AGENT), )); v.push(Span::styled( format!( " spent ${:.2} · {} elapsed", o.cost, crate::view::ago(o.elapsed_secs) ), fg(theme::DIM), )); Line::from(v) } /// A box with its title set into the top border, like `┌ Sessions 6 ──`. fn boxed<'a>(title: &str, count: Option, color: Color, border: Color) -> Block<'a> { let mut t = vec![Span::styled(format!(" {title}"), bold(color))]; if let Some(n) = count { t.push(Span::styled(format!(" {n}"), fg(theme::DIM))); } t.push(Span::raw(" ")); Block::bordered() .border_style(fg(border)) .title(Line::from(t)) } fn status_color(s: Status) -> Color { match s { Status::Done => theme::OK, Status::Fix | Status::Fail => theme::ERR, Status::Wait => theme::WAIT, Status::Run => theme::RUN, Status::Blocked => theme::AGENT, } } fn chip_span(c: &Chip) -> Span<'static> { match c.mark { Mark::Done => Span::styled(format!("✓{}", c.name), fg(theme::OK)), Mark::Active => Span::styled(format!("●{}", c.name), inverted(theme::RUN)), Mark::Failed => Span::styled(format!("✗{}", c.name), fg(theme::ERR)), Mark::Waiting => Span::styled(format!("◆{}", c.name), inverted(theme::WAIT)), Mark::Todo => Span::styled(format!("·{}", c.name), fg(theme::TODO)), } } fn machine_spans(r: &Row) -> Vec> { let mut v = vec![]; for (i, c) in r.chips.iter().enumerate() { if i > 0 { v.push(Span::styled(" › ", fg(theme::SEP))); } v.push(chip_span(c)); } v } fn ctx_bar(pct: u8) -> Span<'static> { let n = (pct as usize + 5) / 10; let color = if pct > 65 { theme::WAIT } else { theme::DIM }; Span::styled( format!("{}{}", "▰".repeat(n), "▱".repeat(10 - n)), fg(color), ) } fn sessions_box(f: &mut Frame, area: Rect, o: &Overview, sel: usize) { let block = boxed("Sessions", Some(o.rows.len()), theme::TEXT, theme::BORDER); let inner = block.inner(area); f.render_widget(block, area); if o.rows.is_empty() { let hint = " no sessions yet · orch session --repo . --machine task --name NAME"; f.render_widget(Paragraph::new(Span::styled(hint, fg(theme::DIM))), inner); return; } let w_machine = o .rows .iter() .map(|r| machine_spans(r).iter().map(|s| s.width()).sum::()) .max() .unwrap_or(0) .clamp(12, 72); let mut head = vec![]; cell(&mut head, vec![Span::raw(" id")], W_ID); cell(&mut head, vec![Span::raw("status")], W_STATUS); cell(&mut head, vec![Span::raw("task")], W_TASK); cell(&mut head, vec![Span::raw("machine")], w_machine); cell(&mut head, vec![Span::raw("tests")], W_TESTS); cell(&mut head, vec![Span::raw("diff")], W_DIFF); cell(&mut head, vec![Span::raw("context")], W_CTX); head.push(Span::raw("last activity")); let mut lines = vec![Line::from(head).style(fg(theme::DIM))]; for (i, r) in o.rows.iter().enumerate() { let on = i == sel; let cursor = if on { Span::styled("▶ ", fg(theme::RUN)) } else { Span::raw(" ") }; let mut l1 = vec![]; cell(&mut l1, vec![cursor, Span::raw(r.id.clone())], W_ID); cell( &mut l1, vec![Span::styled(r.status.label(), bold(status_color(r.status)))], W_STATUS, ); cell( &mut l1, vec![Span::styled(fit(&r.task, W_TASK), bold(theme::TEXT))], W_TASK, ); cell(&mut l1, machine_spans(r), w_machine); let tests_color = match r.tests_ok { Some(true) => theme::OK, Some(false) => theme::ERR, None => theme::TODO, }; cell( &mut l1, vec![Span::styled(r.tests.clone(), fg(tests_color))], W_TESTS, ); let diff = if r.added + r.removed == 0 { vec![] } else { vec![ Span::styled(format!("+{}", r.added), fg(theme::OK)), Span::raw(" "), Span::styled(format!("−{}", r.removed), fg(theme::ERR)), ] }; cell(&mut l1, diff, W_DIFF); let ctx = r .ctx .map_or_else(|| Span::styled("—", fg(theme::TODO)), ctx_bar); cell(&mut l1, vec![ctx], W_CTX); let rest = (inner.width as usize) .saturating_sub(W_ID + W_STATUS + W_TASK + w_machine + W_TESTS + W_DIFF + W_CTX + 8); l1.push(Span::styled(fit(&r.last, rest), fg(theme::DIM))); let mut l2 = vec![]; cell(&mut l2, vec![], W_ID + W_STATUS + 1); cell( &mut l2, vec![Span::styled(fit(&r.sub, W_TASK), fg(theme::DIM))], W_TASK, ); // the retry counter sits next to the machine file name: on the chip line it would push every column right let mut m2 = vec![Span::styled(r.machine.clone(), fg(theme::DIM))]; if !r.retry.is_empty() { m2.push(Span::styled(format!(" {}", r.retry), fg(theme::ERR))); } if r.looping { m2.push(Span::styled(" · looping", bold(theme::WAIT))); } cell(&mut l2, m2, w_machine); let bg = if on { Style::new().bg(theme::SEL) } else { Style::new() }; lines.push(Line::from(l1).style(bg)); lines.push(Line::from(l2).style(bg)); } f.render_widget(Paragraph::new(lines), inner); } fn needs_box(f: &mut Frame, area: Rect, needs: &[Need], focus: usize) { let border = if needs.is_empty() { theme::BORDER } else { theme::WAIT }; let block = boxed("◆ Needs you", Some(needs.len()), theme::WAIT, border); let inner = block.inner(area); f.render_widget(block, area); let mut lines = vec![]; for (i, n) in needs.iter().enumerate() { // several items: the keys act on the focused one, tab moves the focus let mark = if i == focus && needs.len() > 1 { Span::styled("▶", fg(theme::WAIT)) } else { Span::raw(" ") }; let age = if n.age_secs > 0 { format!(" · {}", crate::view::ago(n.age_secs)) } else { String::new() }; lines.push(Line::from(vec![ mark, Span::styled(n.session.clone(), fg(theme::WAIT)), Span::raw(" "), Span::styled(n.task.clone(), bold(theme::TEXT)), Span::styled(format!(" · {}{age}", n.what), fg(theme::DIM)), ])); let w = (inner.width as usize).saturating_sub(1); if !n.detail.is_empty() { lines.push(Line::from(Span::raw(format!(" {}", fit(&n.detail, w))))); } let keys = n.keys(); if !keys.is_empty() { lines.push(Line::from(Span::styled(format!(" {keys}"), fg(theme::DIM)))); } lines.push(Line::raw("")); } if needs.is_empty() { lines.push(Line::from(Span::styled( " nothing waits on you", fg(theme::DIM), ))); } f.render_widget(Paragraph::new(lines), inner); } fn kind_color(kind: &str) -> Color { match kind { "test" | "catch" => theme::ERR, "state" | "resume" => theme::RUN, "tool" => theme::AGENT, "edit" => theme::OK, "notify" | "end" => theme::WAIT, _ => theme::DIM, } } fn events_box(f: &mut Frame, area: Rect, events: &[EventLine]) { let mut title = vec![Span::styled(" Events", bold(theme::TEXT))]; title.push(Span::styled(" all sessions · f filter ", fg(theme::DIM))); let block = Block::bordered() .border_style(fg(theme::BORDER)) .title(Line::from(title)); let inner = block.inner(area); f.render_widget(block, area); let take = inner.height as usize; let skip = events.len().saturating_sub(take); let lines: Vec = events[skip..] .iter() .map(|e| { Line::from(vec![ Span::styled(format!("{} ", hms(e.at_ms)), fg(theme::DIM)), Span::styled(format!("{:<4}", e.session), fg(theme::RUN)), Span::styled(format!("{:<7}", fit(&e.kind, 7)), fg(kind_color(&e.kind))), Span::raw(fit(&e.text, (inner.width as usize).saturating_sub(21))), ]) }) .collect(); f.render_widget(Paragraph::new(lines), inner); } fn key_bar() -> Line<'static> { let mut v = vec![Span::raw(" ")]; for (k, what) in [ ("j/k", "move"), ("enter", "open"), ("g", "goal"), ("tab", "next item"), ("q", "quit"), ] { v.push(Span::styled( format!(" {k} "), Style::new().fg(theme::BG).bg(theme::DIM), )); v.push(Span::styled(format!(" {what} "), fg(theme::DIM))); } Line::from(v) }