tui: render assistant markdown with pulldown-cmark (tables, lists, quotes, links)
Tables are drawn as box-drawn grids that wrap to the terminal width, falling back to header: value lists when too narrow. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c2800b9554
commit
9614ac32ab
6 files changed
+537
-67
No files matched your search
@@ -332,7 +332,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);
|
||||
crate::markdown::render(s, width.saturating_sub(2), &mut md);
|
||||
lead(md, " ", th.plain(), out);
|
||||
}
|
||||
Item::Thinking(s) if expanded => {
|
||||
@@ -554,63 +554,6 @@ fn advance(cur: Option<usize>, l: &str) -> Option<usize> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Minimal markdown: fenced code, headings, bullets, inline `code` and **bold**.
|
||||
pub(crate) fn markdown(s: &str, out: &mut Vec<Line<'static>>) {
|
||||
let mut code = false;
|
||||
for l in s.lines() {
|
||||
if l.trim_start().starts_with("```") {
|
||||
code = !code;
|
||||
out.push(Line::styled(l.to_string(), theme::t().dim()));
|
||||
continue;
|
||||
}
|
||||
if code {
|
||||
out.push(Line::styled(l.to_string(), theme::t().block_code()));
|
||||
} else if l.starts_with('#') {
|
||||
out.push(Line::styled(
|
||||
l.trim_start_matches('#').trim().to_string(),
|
||||
theme::t()
|
||||
.plain()
|
||||
.add_modifier(Modifier::BOLD | Modifier::UNDERLINED),
|
||||
));
|
||||
} else {
|
||||
out.push(inline(l));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn inline(l: &str) -> Line<'static> {
|
||||
let mut spans = vec![];
|
||||
let mut rest = l;
|
||||
while !rest.is_empty() {
|
||||
let next = [
|
||||
("`", theme::t().code()),
|
||||
("**", theme::t().plain().add_modifier(Modifier::BOLD)),
|
||||
]
|
||||
.into_iter()
|
||||
.filter_map(|(d, st)| {
|
||||
let i = rest.find(d)?;
|
||||
let j = rest[i + d.len()..].find(d)?;
|
||||
Some((i, j, d, st))
|
||||
})
|
||||
.min_by_key(|x| x.0);
|
||||
match next {
|
||||
Some((i, j, d, st)) => {
|
||||
spans.push(Span::raw(rest[..i].to_string()));
|
||||
spans.push(Span::styled(
|
||||
rest[i + d.len()..i + d.len() + j].to_string(),
|
||||
st,
|
||||
));
|
||||
rest = &rest[i + d.len() * 2 + j..];
|
||||
}
|
||||
None => {
|
||||
spans.push(Span::raw(rest.to_string()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Line::from(spans)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -798,11 +741,4 @@ mod tests {
|
||||
t.live.clear();
|
||||
assert_eq!(t.lines(false, 40).len(), 2, "no tail when nothing is live");
|
||||
}
|
||||
|
||||
#[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, theme::t().code().fg);
|
||||
assert_eq!(text(&[inline("x ` unclosed")]), ["x ` unclosed"]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user