Three-pane subconscious debug screen with shared widgets
New layout for F3 screen: - Top-left: agent list using ratatui List widget with ListState - Middle-left: expandable agent state (persistent across runs) - Bottom-left: memory store activity by provenance, walked keys - Right: context tree from fork point, reusing SectionTree Tab/Shift-Tab cycles focus clockwise between panes; focused pane gets white border. Each pane handles its own input when focused. Extracted user/widgets.rs: - SectionTree (moved from mod.rs): expand/collapse tree for ContextSection - pane_block_focused(): standard bordered block with focus indicator - format_age()/format_ts_age(): shared duration formatting Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
edfa1c37f5
commit
818cdcc4e5
4 changed files with 399 additions and 288 deletions
175
src/user/widgets.rs
Normal file
175
src/user/widgets.rs
Normal file
|
|
@ -0,0 +1,175 @@
|
|||
// widgets.rs — Shared TUI helpers and reusable components
|
||||
|
||||
use ratatui::{
|
||||
style::{Color, Modifier, Style},
|
||||
text::Line,
|
||||
widgets::{Block, Borders},
|
||||
crossterm::event::KeyCode,
|
||||
};
|
||||
use crate::agent::context::ContextSection;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Standard pane block — cyan border, right-aligned title.
|
||||
pub fn pane_block(title: &str) -> Block<'_> {
|
||||
Block::default()
|
||||
.title_top(Line::from(format!(" {} ", title)).right_aligned())
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().fg(Color::Cyan))
|
||||
}
|
||||
|
||||
/// Focused pane block — brighter border to indicate focus.
|
||||
pub fn pane_block_focused(title: &str, focused: bool) -> Block<'_> {
|
||||
let color = if focused { Color::White } else { Color::Cyan };
|
||||
Block::default()
|
||||
.title_top(Line::from(format!(" {} ", title)).right_aligned())
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().fg(color))
|
||||
}
|
||||
|
||||
/// Format a duration in seconds as a compact human-readable string.
|
||||
pub fn format_age(secs: f64) -> String {
|
||||
if secs < 60.0 { format!("{:.0}s", secs) }
|
||||
else if secs < 3600.0 { format!("{:.0}m", secs / 60.0) }
|
||||
else if secs < 86400.0 { format!("{:.1}h", secs / 3600.0) }
|
||||
else { format!("{:.0}d", secs / 86400.0) }
|
||||
}
|
||||
|
||||
/// Format a unix epoch timestamp as age from now.
|
||||
pub fn format_ts_age(ts: i64) -> String {
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs() as i64;
|
||||
format_age((now - ts).max(0) as f64)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// SectionTree — expand/collapse tree renderer for ContextSection
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub struct SectionTree {
|
||||
pub selected: Option<usize>,
|
||||
pub expanded: std::collections::HashSet<usize>,
|
||||
pub scroll: u16,
|
||||
}
|
||||
|
||||
impl SectionTree {
|
||||
pub fn new() -> Self {
|
||||
Self { selected: None, expanded: std::collections::HashSet::new(), scroll: 0 }
|
||||
}
|
||||
|
||||
pub fn item_count(&self, sections: &[ContextSection]) -> usize {
|
||||
fn count(section: &ContextSection, expanded: &std::collections::HashSet<usize>, idx: &mut usize) -> usize {
|
||||
let my_idx = *idx;
|
||||
*idx += 1;
|
||||
let mut total = 1;
|
||||
if expanded.contains(&my_idx) {
|
||||
for child in §ion.children {
|
||||
total += count(child, expanded, idx);
|
||||
}
|
||||
}
|
||||
total
|
||||
}
|
||||
let mut idx = 0;
|
||||
sections.iter().map(|s| count(s, &self.expanded, &mut idx)).sum()
|
||||
}
|
||||
|
||||
pub fn handle_nav(&mut self, code: KeyCode, sections: &[ContextSection]) {
|
||||
let item_count = self.item_count(sections);
|
||||
match code {
|
||||
KeyCode::Up => {
|
||||
self.selected = Some(self.selected.unwrap_or(0).saturating_sub(1));
|
||||
self.scroll_to_selected();
|
||||
}
|
||||
KeyCode::Down => {
|
||||
let max = item_count.saturating_sub(1);
|
||||
self.selected = Some(self.selected.map_or(0, |s| (s + 1).min(max)));
|
||||
self.scroll_to_selected();
|
||||
}
|
||||
KeyCode::Right | KeyCode::Enter => {
|
||||
if let Some(sel) = self.selected {
|
||||
self.expanded.insert(sel);
|
||||
}
|
||||
}
|
||||
KeyCode::Left => {
|
||||
if let Some(sel) = self.selected {
|
||||
self.expanded.remove(&sel);
|
||||
}
|
||||
}
|
||||
KeyCode::PageUp => { self.scroll = self.scroll.saturating_sub(20); }
|
||||
KeyCode::PageDown => { self.scroll += 20; }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn scroll_to_selected(&mut self) {
|
||||
if let Some(sel) = self.selected {
|
||||
let sel_line = sel as u16;
|
||||
if sel_line < self.scroll + 2 {
|
||||
self.scroll = sel_line.saturating_sub(2);
|
||||
} else if sel_line > self.scroll + 30 {
|
||||
self.scroll = sel_line.saturating_sub(15);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_sections(&self, sections: &[ContextSection], lines: &mut Vec<Line>) {
|
||||
let mut idx = 0;
|
||||
for section in sections {
|
||||
self.render_one(section, 0, lines, &mut idx);
|
||||
}
|
||||
}
|
||||
|
||||
fn render_one(
|
||||
&self,
|
||||
section: &ContextSection,
|
||||
depth: usize,
|
||||
lines: &mut Vec<Line>,
|
||||
idx: &mut usize,
|
||||
) {
|
||||
let my_idx = *idx;
|
||||
let selected = self.selected == Some(my_idx);
|
||||
let expanded = self.expanded.contains(&my_idx);
|
||||
let has_children = !section.children.is_empty();
|
||||
let has_content = !section.content.is_empty();
|
||||
let expandable = has_children || has_content;
|
||||
|
||||
let indent = " ".repeat(depth + 1);
|
||||
let marker = if !expandable { " " } else if expanded { "▼" } else { "▶" };
|
||||
let label = format!("{}{} {:30} {:>6} tokens", indent, marker, section.name, section.tokens);
|
||||
let style = if selected {
|
||||
Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)
|
||||
} else {
|
||||
Style::default()
|
||||
};
|
||||
lines.push(Line::styled(label, style));
|
||||
*idx += 1;
|
||||
|
||||
if expanded {
|
||||
if has_children {
|
||||
for child in §ion.children {
|
||||
self.render_one(child, depth + 1, lines, idx);
|
||||
}
|
||||
} else if has_content {
|
||||
let content_indent = format!("{} │ ", " ".repeat(depth + 1));
|
||||
let content_lines: Vec<&str> = section.content.lines().collect();
|
||||
let show = content_lines.len().min(50);
|
||||
for line in &content_lines[..show] {
|
||||
lines.push(Line::styled(
|
||||
format!("{}{}", content_indent, line),
|
||||
Style::default().fg(Color::DarkGray),
|
||||
));
|
||||
}
|
||||
if content_lines.len() > 50 {
|
||||
lines.push(Line::styled(
|
||||
format!("{}... ({} more lines)", content_indent, content_lines.len() - 50),
|
||||
Style::default().fg(Color::DarkGray),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue