tui: new design (Catppuccin Mocha, header, framed input, 3-col popup, sessions picker, /lsp panel)
This commit is contained in:
1 parent
7e29515821
commit
79bd3b8ea7
7 files changed
+907
-462
No files matched your search
+124
-73
@@ -120,19 +120,22 @@ impl Transcript {
|
||||
}
|
||||
}
|
||||
|
||||
/// `⠹ bash 12s` — `frame` picks the spinner glyph, `elapsed` is shown in whole seconds.
|
||||
pub fn activity_line(what: &str, elapsed: Duration, frame: usize) -> Line<'static> {
|
||||
const SPIN: [&str; 10] = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
||||
/// `⠹ working 38s · bash esc to abort` — `frame` picks the spinner glyph.
|
||||
pub fn activity_line(what: &str, elapsed: Duration, frame: usize, width: u16) -> Line<'static> {
|
||||
let th = theme::t();
|
||||
let g = if th.sym.ok == "+" {
|
||||
["-", "\\", "|", "/"][frame % 4]
|
||||
} else {
|
||||
SPIN[frame % 10]
|
||||
};
|
||||
Line::from(vec![
|
||||
Span::styled(format!(" {g} "), th.accent()),
|
||||
Span::styled(format!("{what} {}s", elapsed.as_secs()), th.dim()),
|
||||
])
|
||||
let left = vec![
|
||||
Span::styled(format!("{} ", th.spin(frame)), th.accent()),
|
||||
Span::styled(format!("working {}s", elapsed.as_secs()), th.accent()),
|
||||
Span::styled(format!(" {} {what}", th.sym.info), th.sec()),
|
||||
];
|
||||
let hint = Span::styled("esc to abort", th.dim());
|
||||
let lw: usize = left.iter().map(Span::width).sum();
|
||||
let mut l = left;
|
||||
if let Some(pad) = (width as usize).checked_sub(lw + hint.width() + 1) {
|
||||
l.push(Span::raw(" ".repeat(pad + 1)));
|
||||
l.push(hint);
|
||||
}
|
||||
Line::from(l)
|
||||
}
|
||||
|
||||
fn target(args: &Value) -> String {
|
||||
@@ -200,51 +203,55 @@ fn fit(s: &str, w: usize) -> String {
|
||||
out + ell
|
||||
}
|
||||
|
||||
/// ` ✓ edit src/a.rs ············ +3 −1 0.2s ▸`
|
||||
/// `● edit src/a.rs ············ +3 −1 · 0.2s ▸`; running: spinner and live seconds.
|
||||
fn tool_header(
|
||||
name: &str,
|
||||
args: &Value,
|
||||
result: &Option<(String, bool)>,
|
||||
started: Instant,
|
||||
took: Option<Duration>,
|
||||
folded: bool,
|
||||
width: usize,
|
||||
) -> Line<'static> {
|
||||
let th = theme::t();
|
||||
let (mark, st) = match result {
|
||||
None => (th.sym.pending, th.warn()),
|
||||
Some((_, false)) => (th.sym.ok, th.ok()),
|
||||
None => (
|
||||
th.spin((started.elapsed().as_millis() / 80) as usize),
|
||||
th.accent(),
|
||||
),
|
||||
Some((_, false)) => (th.sym.done, th.ok()),
|
||||
Some((_, true)) => (th.sym.err, th.err()),
|
||||
};
|
||||
let mut right = match result {
|
||||
Some((c, e)) => outcome(name, c, *e),
|
||||
None => String::new(),
|
||||
None => format!("{}s", started.elapsed().as_secs()),
|
||||
};
|
||||
if let Some(d) = took {
|
||||
right = format!("{right} {}", fmt_dur(d));
|
||||
right = format!("{right} {} {}", th.sym.info, fmt_dur(d));
|
||||
}
|
||||
if folded {
|
||||
right = format!("{right} {}", th.sym.more);
|
||||
}
|
||||
let left = format!(" {mark} {name:<5} ");
|
||||
let left = format!("{mark} {name:<6} ");
|
||||
let lw = Span::raw(left.as_str()).width();
|
||||
let rw = Span::raw(right.as_str()).width();
|
||||
let room = width.saturating_sub(lw + rw + 1).max(8);
|
||||
let tgt = fit(&target(args), room);
|
||||
let pad = width.saturating_sub(lw + Span::raw(tgt.as_str()).width() + rw);
|
||||
Line::from(vec![
|
||||
Span::styled(format!(" {mark} "), st),
|
||||
Span::styled(format!("{mark} "), st),
|
||||
Span::styled(
|
||||
format!("{name:<5} "),
|
||||
format!("{name:<6} "),
|
||||
th.plain().add_modifier(Modifier::BOLD),
|
||||
),
|
||||
Span::raw(tgt),
|
||||
Span::styled(tgt, th.sec()),
|
||||
Span::raw(" ".repeat(pad.max(1))),
|
||||
Span::styled(
|
||||
right,
|
||||
if matches!(result, Some((_, true))) {
|
||||
th.err()
|
||||
} else {
|
||||
th.dim()
|
||||
match result {
|
||||
Some((_, true)) => th.err(),
|
||||
None => th.accent(),
|
||||
_ => th.sec(),
|
||||
},
|
||||
),
|
||||
])
|
||||
@@ -279,7 +286,7 @@ fn render(it: &Item, expanded: bool, width: usize, out: &mut Vec<Line<'static>>)
|
||||
Item::Assistant(s) => {
|
||||
let mut md = vec![];
|
||||
markdown(s, &mut md);
|
||||
lead(md, th.sym.asst, th.accent(), out);
|
||||
lead(md, " ", th.plain(), out);
|
||||
}
|
||||
Item::Thinking(s) if expanded => {
|
||||
let st = th.dim().add_modifier(Modifier::ITALIC);
|
||||
@@ -301,6 +308,7 @@ fn render(it: &Item, expanded: bool, width: usize, out: &mut Vec<Line<'static>>)
|
||||
name,
|
||||
args,
|
||||
result,
|
||||
started,
|
||||
took,
|
||||
..
|
||||
} => {
|
||||
@@ -309,7 +317,9 @@ fn render(it: &Item, expanded: bool, width: usize, out: &mut Vec<Line<'static>>)
|
||||
Some((content, err)) => tool_body(name, content, *err, expanded, &mut body),
|
||||
None => false,
|
||||
};
|
||||
out.push(tool_header(name, args, result, *took, folded, width));
|
||||
out.push(tool_header(
|
||||
name, args, result, *started, *took, folded, width,
|
||||
));
|
||||
out.extend(body);
|
||||
}
|
||||
}
|
||||
@@ -319,10 +329,7 @@ fn render(it: &Item, expanded: bool, width: usize, out: &mut Vec<Line<'static>>)
|
||||
/// collapsed, the first findings; `ctrl-o` shows all.
|
||||
fn lens_block(texts: &[String], expanded: bool, width: usize, out: &mut Vec<Line<'static>>) {
|
||||
let th = theme::t();
|
||||
let rule = if th.sym.ok == "+" { "-" } else { "┄" };
|
||||
let head = format!(" {rule} lens ");
|
||||
let fill = rule.repeat(width.saturating_sub(Span::raw(head.as_str()).width()));
|
||||
out.push(Line::styled(format!("{head}{fill}"), th.dim()));
|
||||
out.push(Line::styled(th.rule("lens", width), th.dim()));
|
||||
for t in texts {
|
||||
let mut lines = t.lines();
|
||||
let first = lines.next().unwrap_or_default();
|
||||
@@ -338,7 +345,7 @@ fn lens_block(texts: &[String], expanded: bool, width: usize, out: &mut Vec<Line
|
||||
let (mark, st) = if title.contains("failing") || errors > 0 {
|
||||
(th.sym.err, th.err())
|
||||
} else if warnings > 0 {
|
||||
("!", th.warn())
|
||||
(th.sym.warn, th.warn())
|
||||
} else {
|
||||
(th.sym.ok, th.ok())
|
||||
};
|
||||
@@ -361,7 +368,7 @@ fn lens_block(texts: &[String], expanded: bool, width: usize, out: &mut Vec<Line
|
||||
format!(" · {}", counts.join(", "))
|
||||
};
|
||||
out.push(Line::from(vec![
|
||||
Span::styled(format!(" {mark} "), st),
|
||||
Span::styled(format!("{mark} "), st),
|
||||
Span::raw(title.to_string()),
|
||||
Span::styled(counts, th.dim()),
|
||||
]));
|
||||
@@ -379,16 +386,17 @@ fn lens_block(texts: &[String], expanded: bool, width: usize, out: &mut Vec<Line
|
||||
th.dim()
|
||||
};
|
||||
out.push(Line::from(vec![
|
||||
Span::styled(format!(" {} ", th.sym.gutter), th.dim()),
|
||||
Span::styled(format!(" {} ", th.sym.gutter), th.dim()),
|
||||
Span::styled(l.trim_start().to_string(), ls),
|
||||
]));
|
||||
}
|
||||
if n < rest.len() {
|
||||
out.push(Line::styled(
|
||||
format!(
|
||||
" {} {} more lines (ctrl-o)",
|
||||
" {} {} more lines {} ctrl-o expand",
|
||||
th.sym.ellipsis,
|
||||
rest.len() - n
|
||||
rest.len() - n,
|
||||
th.sym.info
|
||||
),
|
||||
th.dim(),
|
||||
));
|
||||
@@ -401,7 +409,8 @@ const DIFF_PREVIEW: usize = 6;
|
||||
const ERR_TAIL: usize = 5;
|
||||
|
||||
/// Body lines behind a gutter. Collapsed: edit diff preview, error tail, nothing otherwise.
|
||||
/// Returns true when output was hidden (header shows the fold marker).
|
||||
/// Diffs get new-file line numbers from the `@@` headers. Returns true when output was
|
||||
/// hidden (header shows the fold marker).
|
||||
fn tool_body(
|
||||
name: &str,
|
||||
content: &str,
|
||||
@@ -424,42 +433,80 @@ fn tool_body(
|
||||
} else {
|
||||
(&lines[..0], 0, lines.len())
|
||||
};
|
||||
let g = |st: Style| Span::styled(format!(" {} ", th.sym.gutter), st);
|
||||
let note = |s: String| Line::from(vec![g(th.dim()), Span::styled(s, th.dim())]);
|
||||
// number column wide enough for the biggest new-file line in the hunks
|
||||
let nw = if is_diff {
|
||||
lines
|
||||
.iter()
|
||||
.filter_map(|l| hunk_start(l))
|
||||
.map(|n| (n + lines.len()).to_string().len())
|
||||
.max()
|
||||
.unwrap_or(0)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let g = |n: Option<usize>| {
|
||||
let num = n.map_or(String::new(), |n| n.to_string());
|
||||
Span::styled(format!(" {num:>nw$} {} ", th.sym.gutter), th.dim())
|
||||
};
|
||||
let note = |s: String| Line::from(vec![g(None), Span::styled(s, th.dim())]);
|
||||
if before > 0 {
|
||||
out.push(note(format!(
|
||||
"{} {before} earlier lines (ctrl-o)",
|
||||
th.sym.ellipsis
|
||||
"{} {before} earlier lines {} ctrl-o expand",
|
||||
th.sym.ellipsis, th.sym.info
|
||||
)));
|
||||
}
|
||||
let mut new_ln: Option<usize> = None;
|
||||
for l in &lines[..before] {
|
||||
new_ln = advance(new_ln, l);
|
||||
}
|
||||
for l in show {
|
||||
let st = if is_diff {
|
||||
let (st, num) = if is_diff {
|
||||
let cur = new_ln.filter(|_| !l.starts_with('-') && !l.starts_with('@'));
|
||||
new_ln = advance(new_ln, l);
|
||||
match l.as_bytes().first() {
|
||||
Some(b'+') if !l.starts_with("+++") => th.add(),
|
||||
Some(b'-') if !l.starts_with("---") => th.del(),
|
||||
Some(b'@') => th.hunk(),
|
||||
_ => th.dim(),
|
||||
Some(b'+') if !l.starts_with("+++") => (th.add(), cur),
|
||||
Some(b'-') if !l.starts_with("---") => (th.del(), None),
|
||||
Some(b'@') => (th.hunk(), None),
|
||||
_ => (th.sec(), cur),
|
||||
}
|
||||
} else if err {
|
||||
th.err()
|
||||
(th.err(), None)
|
||||
} else {
|
||||
th.dim()
|
||||
(th.sec(), None)
|
||||
};
|
||||
out.push(Line::from(vec![
|
||||
g(th.dim()),
|
||||
Span::styled(l.to_string(), st),
|
||||
]));
|
||||
out.push(Line::from(vec![g(num), Span::styled(l.to_string(), st)]));
|
||||
}
|
||||
if after > 0 && is_diff {
|
||||
out.push(note(format!(
|
||||
"{} {after} more lines (ctrl-o)",
|
||||
th.sym.ellipsis
|
||||
"{} {after} more lines {} ctrl-o expand",
|
||||
th.sym.ellipsis, th.sym.info
|
||||
)));
|
||||
}
|
||||
// write output is just the header's "N lines"
|
||||
!expanded && after > 0 && !is_diff && name != "write"
|
||||
}
|
||||
|
||||
/// `@@ -a,b +c,d @@` → `c`.
|
||||
fn hunk_start(l: &str) -> Option<usize> {
|
||||
let r = l
|
||||
.strip_prefix("@@ ")?
|
||||
.split(' ')
|
||||
.nth(1)?
|
||||
.strip_prefix('+')?;
|
||||
r.split(',').next()?.parse().ok()
|
||||
}
|
||||
|
||||
/// New-file line number for the line after `l`.
|
||||
fn advance(cur: Option<usize>, l: &str) -> Option<usize> {
|
||||
if let Some(n) = hunk_start(l) {
|
||||
return Some(n);
|
||||
}
|
||||
match l.as_bytes().first() {
|
||||
Some(b'-') => cur,
|
||||
_ => cur.map(|n| n + 1),
|
||||
}
|
||||
}
|
||||
|
||||
/// Minimal markdown: fenced code, headings, bullets, inline `code` and **bold**.
|
||||
fn markdown(s: &str, out: &mut Vec<Line<'static>>) {
|
||||
let mut code = false;
|
||||
@@ -520,7 +567,6 @@ fn inline(l: &str) -> Line<'static> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use ratatui::style::Color;
|
||||
use serde_json::json;
|
||||
|
||||
fn text(ls: &[Line]) -> Vec<String> {
|
||||
@@ -569,9 +615,9 @@ mod tests {
|
||||
let ls = t.lines(false, 40);
|
||||
let s = text(&ls);
|
||||
assert_eq!(s[..2], ["❯ hi", " there"]);
|
||||
assert_eq!(s[3], "◆ Hello");
|
||||
assert_eq!(s[3], " Hello");
|
||||
assert!(
|
||||
s[5].starts_with(" ✓ edit a.rs ") && s[5].contains("+1 −1 0.0s"),
|
||||
s[5].starts_with("● edit a.rs ") && s[5].contains("+1 −1 · 0.0s"),
|
||||
"{s:?}"
|
||||
);
|
||||
assert_eq!(s[5].chars().count(), 40, "header fills the width");
|
||||
@@ -579,7 +625,9 @@ mod tests {
|
||||
.iter()
|
||||
.find(|l| text(std::slice::from_ref(l))[0] == " │ -old")
|
||||
.unwrap();
|
||||
assert_eq!(minus.spans[1].style.fg, Some(Color::Red));
|
||||
assert_eq!(minus.spans[1].style.fg, theme::t().err().fg);
|
||||
// new-file line numbers from the hunk header
|
||||
assert!(s.contains(&" 1 │ +new".to_string()), "{s:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -596,7 +644,7 @@ mod tests {
|
||||
let s = text(&out);
|
||||
assert_eq!(s[0].chars().count(), w, "{s:?}");
|
||||
assert!(
|
||||
s[0].starts_with(" ✗ bash cargo") && s[0].ends_with("exit 101 1.2s"),
|
||||
s[0].starts_with("✗ bash cargo") && s[0].ends_with("exit 101 · 1.2s"),
|
||||
"{s:?}"
|
||||
);
|
||||
}
|
||||
@@ -612,7 +660,7 @@ mod tests {
|
||||
let mut out = vec![];
|
||||
render(&w, false, 50, &mut out);
|
||||
assert!(
|
||||
text(&out)[0].ends_with(" 3 lines 1.2s"),
|
||||
text(&out)[0].ends_with(" 3 lines · 1.2s"),
|
||||
"{:?}",
|
||||
text(&out)
|
||||
);
|
||||
@@ -620,7 +668,7 @@ mod tests {
|
||||
let mut out = vec![];
|
||||
render(&r, false, 50, &mut out);
|
||||
assert_eq!(out.len(), 1);
|
||||
assert!(text(&out)[0].ends_with("2 lines 1.2s ▸"), "folded marker");
|
||||
assert!(text(&out)[0].ends_with("2 lines · 1.2s ▸"), "folded marker");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -629,8 +677,8 @@ mod tests {
|
||||
let mut out = vec![];
|
||||
assert!(!tool_body("bash", &body, true, false, &mut out));
|
||||
let s = text(&out);
|
||||
assert_eq!(s[0], " │ … 15 earlier lines (ctrl-o)");
|
||||
assert_eq!(s.last().unwrap(), " │ l20");
|
||||
assert_eq!(s[0], " │ … 15 earlier lines · ctrl-o expand");
|
||||
assert_eq!(s.last().unwrap(), " │ l20");
|
||||
out.clear();
|
||||
assert!(tool_body("bash", &body, false, false, &mut out) && out.is_empty());
|
||||
assert!(!tool_body("bash", &body, false, true, &mut out));
|
||||
@@ -640,7 +688,10 @@ mod tests {
|
||||
"edited a\n".to_string() + &(1..=10).map(|i| format!("+l{i}\n")).collect::<String>();
|
||||
tool_body("edit", &diff, false, false, &mut out);
|
||||
assert_eq!(out.len(), DIFF_PREVIEW + 1);
|
||||
assert_eq!(text(&out).last().unwrap(), " │ … 4 more lines (ctrl-o)");
|
||||
assert_eq!(
|
||||
text(&out).last().unwrap(),
|
||||
" │ … 4 more lines · ctrl-o expand"
|
||||
);
|
||||
let mut out = vec![];
|
||||
render(&Item::Thinking("a b c".into()), false, 40, &mut out);
|
||||
assert_eq!(text(&out), [" thought · 3 words ▸"]);
|
||||
@@ -658,15 +709,15 @@ mod tests {
|
||||
]));
|
||||
let s = text(&t.lines(false, 30));
|
||||
assert_eq!(s[0].chars().count(), 30);
|
||||
assert!(s[0].starts_with(" ┄ lens ┄"));
|
||||
assert!(s[0].starts_with("┈ lens ┈"));
|
||||
assert_eq!(
|
||||
s[1],
|
||||
" ✗ diagnostics for files changed this turn · 1 error, 4 warnings"
|
||||
"✗ diagnostics for files changed this turn · 1 error, 4 warnings"
|
||||
);
|
||||
assert_eq!(s[2], " │ src/a.rs:");
|
||||
assert_eq!(s[3], " │ 3:1 error: E0308 mismatched (rust-analyzer)");
|
||||
assert_eq!(s[6], " … 2 more lines (ctrl-o)");
|
||||
assert_eq!(s[7], " ✓ tests: all 21 selected tests pass now");
|
||||
assert_eq!(s[2], " │ src/a.rs:");
|
||||
assert_eq!(s[3], " │ 3:1 error: E0308 mismatched (rust-analyzer)");
|
||||
assert_eq!(s[6], " … 2 more lines · ctrl-o expand");
|
||||
assert_eq!(s[7], "✓ tests: all 21 selected tests pass now");
|
||||
assert_eq!(
|
||||
text(&t.lines(true, 30)).len(),
|
||||
10,
|
||||
@@ -676,14 +727,14 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn activity() {
|
||||
let l = activity_line("bash", Duration::from_millis(12_900), 2);
|
||||
assert_eq!(text(&[l]), [" ⠹ bash 12s"]);
|
||||
let l = activity_line("bash", Duration::from_millis(12_900), 2, 40);
|
||||
assert_eq!(text(&[l]), ["⠹ working 12s · bash esc to abort"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_md() {
|
||||
assert_eq!(text(&[inline("a `b` **c** d")]), ["a b c d"]);
|
||||
assert_eq!(inline("a `b` c").spans[1].style.fg, Some(Color::Yellow));
|
||||
assert_eq!(inline("a `b` c").spans[1].style.fg, theme::t().code().fg);
|
||||
assert_eq!(text(&[inline("x ` unclosed")]), ["x ` unclosed"]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user