tui: new design (Catppuccin Mocha, header, framed input, 3-col popup, sessions picker, /lsp panel)

This commit is contained in:
hermes committed 2026-10-09 14:51:00 +00:00
1 parent 7e29515821
commit 79bd3b8ea7
7 files changed
+907 -462

No files matched your search

+135 -58
View File
@@ -4,8 +4,9 @@ use crate::theme;
use crate::transcript::{Item, Transcript};
use hx_core::session::{self, Meta};
use ratatui::layout::{Constraint, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Clear, Paragraph};
use ratatui::widgets::{Block, BorderType, Borders, Clear, Paragraph};
use std::path::{Path, PathBuf};
use std::time::SystemTime;
@@ -134,48 +135,62 @@ impl Sessions {
Act::None
}
/// Full-screen: scope tabs and count, framed query, list | preview, key hints.
pub fn draw(&mut self, f: &mut ratatui::Frame) {
let th = theme::t();
let a = f.area();
let r = Rect::new(a.x + 1, a.y, a.width.saturating_sub(2), a.height);
let scope = if self.all_scope {
"this project ▸ all projects"
} else {
"this project ▸ all (tab)"
};
f.render_widget(Clear, a);
let ms: Vec<Meta> = self.matches().into_iter().cloned().collect();
let block = Block::bordered().border_style(th.dim()).title(Span::styled(
format!(" sessions · {scope} · {} ", ms.len()),
th.dim(),
));
let inner = block.inner(r);
f.render_widget(Clear, r);
f.render_widget(block, r);
let list_h = (ms.len() as u16).clamp(1, (inner.height / 2).max(3));
let [q, _, list, sep, prev, help] = Layout::vertical([
let [tabs, q, body, help] = Layout::vertical([
Constraint::Length(1),
Constraint::Length(1),
Constraint::Length(list_h),
Constraint::Length(1),
Constraint::Min(0),
Constraint::Length(3),
Constraint::Min(1),
Constraint::Length(1),
])
.areas(inner);
.areas(a);
let (on, off) = (th.plain(), th.dim());
let count = Span::styled(format!("{} of {}", ms.len(), self.pool().len()), th.sec());
let left = vec![
Span::styled("sessions", th.accent().add_modifier(Modifier::BOLD)),
Span::raw(" "),
Span::styled("this project", if self.all_scope { off } else { on }),
Span::styled(format!(" {} ", th.sym.info), th.dim()),
Span::styled("all projects", if self.all_scope { on } else { off }),
];
let lw: usize = left.iter().map(Span::width).sum();
let mut l = left;
l.push(Span::raw(" ".repeat(
(tabs.width as usize).saturating_sub(lw + count.width()),
)));
l.push(count);
f.render_widget(Line::from(l), tabs);
let frame = Block::bordered()
.border_type(BorderType::Rounded)
.border_style(th.frame());
let qi = frame.inner(q);
f.render_widget(frame, q);
f.render_widget(
Line::from(vec![
Span::styled(format!("{} ", th.sym.user), th.accent()),
Span::raw(self.query.clone()),
Span::styled(self.query.clone(), th.plain()),
]),
q,
qi,
);
f.set_cursor_position((q.x + 2 + self.query.chars().count() as u16, q.y));
f.set_cursor_position((qi.x + 2 + self.query.chars().count() as u16, qi.y));
let [list, rule, prev] = Layout::horizontal([
Constraint::Percentage(45),
Constraint::Length(2),
Constraint::Min(1),
])
.areas(body);
let top = self
.sel
.saturating_sub(list.height.saturating_sub(1) as usize);
let w = list.width as usize;
let rows: Vec<Line> = if ms.is_empty() {
vec![Line::styled(" no sessions", th.dim())]
vec![Line::styled(" no sessions", th.dim())]
} else {
ms.iter()
.enumerate()
@@ -186,60 +201,118 @@ impl Sessions {
};
f.render_widget(Paragraph::new(rows), list);
f.render_widget(
Block::new().borders(Borders::TOP).border_style(th.dim()),
sep,
Block::new().borders(Borders::LEFT).border_style(th.dim()),
Rect {
x: rule.x + 1,
width: 1,
..rule
},
);
if let Some(m) = ms.get(self.sel) {
if self.preview.as_ref().is_none_or(|(p, _)| *p != m.path) {
self.preview = Some((m.path.clone(), preview(&m.path, prev.width)));
let mut ls = vec![
Line::styled(m.title.clone(), th.plain().add_modifier(Modifier::BOLD)),
Line::styled(self.meta_line(m), th.sec()),
Line::default(),
];
ls.extend(preview(&m.path, prev.width.saturating_sub(1)));
self.preview = Some((m.path.clone(), ls));
}
if let Some((_, ls)) = &self.preview {
let skip = ls.len().saturating_sub(prev.height as usize);
f.render_widget(Paragraph::new(ls[skip..].to_vec()), prev);
let pr = Rect {
x: prev.x + 1,
width: prev.width.saturating_sub(1),
..prev
};
let room = pr.height as usize;
let shown: Vec<Line> = if ls.len() > room && room > 4 {
// head of the session plus a count of what's cut
let mut v = ls[..room - 1].to_vec();
v.push(Line::styled(
format!("{} more lines", th.sym.ellipsis),
th.dim(),
));
v
} else {
ls.clone()
};
f.render_widget(Paragraph::new(shown), pr);
}
}
let help_text = match &self.confirm {
Some(p) => Line::styled(format!("delete {}? [y/n]", p.display()), th.warn()),
None => Line::styled(
"enter resume · tab scope · ctrl-d delete · esc close",
"type filter · tab project/all · ↑↓ select · enter resume · ctrl-d delete · esc close",
th.dim(),
),
};
f.render_widget(help_text, help);
}
/// `~/Repos/hx · sonnet-5-5 · 14 msgs · 2h ago`
fn meta_line(&self, m: &Meta) -> String {
let mut v = vec![];
if self.all_scope {
v.push(m.project.clone());
}
if let Some(md) = &m.model {
v.push(md.strip_prefix("claude-").unwrap_or(md).to_string());
}
v.push(format!("{} msgs", m.messages));
if m.branches > 1 {
v.push(format!("{} branches", m.branches));
}
v.push(ago(m.modified));
v.join(&format!(" {} ", theme::t().sym.info))
}
/// ` ● title ····· 2h` — band on the selected row, `●` marks the open session.
fn row(&self, m: &Meta, sel: bool, w: usize) -> Line<'static> {
let th = theme::t();
let st = if sel { th.sel() } else { th.plain() };
let dim = if sel { st } else { th.dim() };
let mark = if sel { th.sym.more } else { " " };
let cur = if m.path == self.current { "●" } else { " " };
let fork = if m.branches > 1 {
format!("⑂{} ", m.branches)
let st = if sel {
th.sel().add_modifier(Modifier::BOLD)
} else {
String::new()
th.plain()
};
let tail = if self.all_scope {
m.project.clone()
let band = if sel { th.sel() } else { Style::default() };
let cur = if m.path == self.current {
th.sym.dirty
} else {
m.model.clone().unwrap_or_default()
" "
};
let tail = tail.strip_prefix("claude-").unwrap_or(&tail);
let right = format!("{fork}{:>4} msg {:<16}", m.messages, cut(tail, 16));
let left = format!("{mark}{cur} {:<9} ", ago(m.modified));
let room = w.saturating_sub(left.chars().count() + right.chars().count() + 1);
let mut right = short_age(m.modified);
if self.all_scope {
right = format!("{} {right}", cut(&m.project, 16));
}
let room = w.saturating_sub(right.chars().count() + 5);
let title = cut(&m.title, room);
let pad = room.saturating_sub(title.chars().count());
let pad = w.saturating_sub(title.chars().count() + right.chars().count() + 4);
Line::from(vec![
Span::styled(left, dim),
Span::styled(format!(" {cur} "), band.patch(th.accent())),
Span::styled(title, st),
Span::styled(" ".repeat(pad + 1), st),
Span::styled(right, dim),
Span::styled(" ".repeat(pad), band),
Span::styled(right, band.patch(th.sec())),
Span::styled(" ", band),
])
}
}
/// `now`, `5m`, `2h`, `1d`, `3w`.
fn short_age(t: SystemTime) -> String {
let s = SystemTime::now()
.duration_since(t)
.unwrap_or_default()
.as_secs();
match s {
0..60 => "now".into(),
60..3600 => format!("{}m", s / 60),
3600..86400 => format!("{}h", s / 3600),
86400..604800 => format!("{}d", s / 86400),
_ => format!("{}w", s / 604800),
}
}
fn cut(s: &str, n: usize) -> String {
if s.chars().count() <= n {
return s.into();
@@ -396,24 +469,28 @@ mod tests {
let rows = screen(&mut s, 100, 20);
let all = rows.join("\n");
assert!(
rows[0].contains("sessions · this project ▸ all (tab) · 1"),
rows[0].starts_with("sessions this project · all projects")
&& rows[0].ends_with("1 of 2"),
"{all}"
);
assert!(rows[1].contains("❯ pars"), "{all}");
assert!(rows[2].contains("❯ pars"), "{all}");
assert!(
rows[3].contains("▸ just now parser: handle trailing commas")
&& rows[3].contains("3 msg opus"),
rows[4].contains(" parser: handle trailing commas") && rows[4].contains(" now "),
"{all}"
);
assert!(all.contains("opus · 3 msgs · just now"), "meta: {all}");
assert!(
all.contains("❯ parser: handle trailing commas"),
"preview: {all}"
);
assert!(
all.contains("✓ edit src/parse.rs"),
all.contains("● edit src/parse.rs"),
"preview tool line: {all}"
);
assert!(rows[18].contains("enter resume · tab scope"), "{all}");
assert!(
rows[19].starts_with("type filter · tab project/all"),
"{all}"
);
// all projects scope shows the project column
s.query.clear();
@@ -421,20 +498,20 @@ mod tests {
assert_eq!(titles(&s)[0], "elsewhere");
let rows = screen(&mut s, 100, 20);
assert!(
rows[3].contains("elsewhere") && rows[3].contains("other"),
rows[4].contains("elsewhere") && rows[4].contains("other"),
"{rows:#?}"
);
s.key(k(KeyCode::Tab));
// current session is marked and can't be deleted; others need y
let rows = screen(&mut s, 100, 20);
assert!(rows[3].contains("▸● just now review PR 73"), "{rows:#?}");
assert!(rows[4].starts_with(" ● review PR 73"), "{rows:#?}");
s.key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL));
assert!(s.confirm.is_none());
s.key(k(KeyCode::Down));
s.key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL));
assert_eq!(s.confirm.as_ref(), Some(&first));
assert!(screen(&mut s, 100, 20)[18].contains("[y/n]"));
assert!(screen(&mut s, 100, 20)[19].contains("[y/n]"));
s.key(k(KeyCode::Char('n')));
assert!(first.exists());
s.key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL));