From 9614ac32ab96498d1c3299914de82be6c9004c1f Mon Sep 17 00:00:00 2001 From: Johannes Kresner Date: Wed, 18 Nov 2026 18:28:35 +0100 Subject: [PATCH] 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 --- Cargo.lock | 18 ++ crates/hx-tui/Cargo.toml | 3 +- crates/hx-tui/src/lib.rs | 4 +- crates/hx-tui/src/markdown.rs | 500 ++++++++++++++++++++++++++++++++ crates/hx-tui/src/theme.rs | 13 + crates/hx-tui/src/transcript.rs | 66 +---- 6 files changed, 537 insertions(+), 67 deletions(-) create mode 100644 crates/hx-tui/src/markdown.rs diff --git a/Cargo.lock b/Cargo.lock index 5fec231..2411259 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1132,6 +1132,7 @@ dependencies = [ "hx-ai", "hx-core", "ignore", + "pulldown-cmark", "ratatui", "serde_json", "similar", @@ -1958,6 +1959,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "pulldown-cmark" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3db2be46835f013dd3f4fa7f5f7402c537787b54ebb6fe2b94bbdd7e2669bb7b" +dependencies = [ + "bitflags 2.13.2", + "memchr", + "unicase", +] + [[package]] name = "quinn" version = "0.11.12" @@ -3165,6 +3177,12 @@ version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971" +[[package]] +name = "unicase" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357cc3acc6a036009fd6c973ed009037c732d60d0b4f6c673e9041497482a28f" + [[package]] name = "unicode-ident" version = "1.0.26" diff --git a/crates/hx-tui/Cargo.toml b/crates/hx-tui/Cargo.toml index b8476ff..957a601 100644 --- a/crates/hx-tui/Cargo.toml +++ b/crates/hx-tui/Cargo.toml @@ -12,7 +12,8 @@ futures = "0.3.34" hx-ai = { version = "0.1.0", path = "../hx-ai" } hx-core = { version = "0.1.0", path = "../hx-core" } ignore = "0.4.33" -ratatui = { version = "0.30.2", features = ["unstable-rendered-line-info"] } +pulldown-cmark = { version = "0.13", default-features = false } +ratatui ={ version = "0.30.2", features = ["unstable-rendered-line-info"] } serde_json = "1.0.151" similar = { version = "3.2.0", features = ["inline"] } tokio = { version = "1.53.1", features = ["rt", "macros", "sync", "time", "rt-multi-thread"] } diff --git a/crates/hx-tui/src/lib.rs b/crates/hx-tui/src/lib.rs index 6a4e185..8f38099 100644 --- a/crates/hx-tui/src/lib.rs +++ b/crates/hx-tui/src/lib.rs @@ -5,6 +5,7 @@ pub mod btw_view; pub mod choose; pub mod doc_view; pub mod editor; +pub mod markdown; pub mod permit_view; pub mod picker; pub mod popup; @@ -566,7 +567,8 @@ pub async fn run(setup: Setup) -> std::io::Result<()> { Ui::Choose(q, reply) => { if let Some(p) = &q.plan { let mut md = vec![]; - transcript::markdown(p, &mut md); + let w = crossterm::terminal::size().map_or(80, |(w, _)| w as usize); + markdown::render(p, w.saturating_sub(4), &mut md); let v = doc_view::DocView::new( "plan", md, diff --git a/crates/hx-tui/src/markdown.rs b/crates/hx-tui/src/markdown.rs new file mode 100644 index 0000000..ff3d27d --- /dev/null +++ b/crates/hx-tui/src/markdown.rs @@ -0,0 +1,500 @@ +//! Markdown → styled lines via pulldown-cmark: headings, lists, quotes, rules, fenced +//! code, inline emphasis/links and box-drawn tables. Half-streamed input is fine: the +//! parser treats an unclosed fence as code and an unfinished table as plain text. + +use crate::theme; +use pulldown_cmark::{Alignment, CodeBlockKind, Event, Options, Parser, Tag, TagEnd}; +use ratatui::style::{Modifier, Style}; +use ratatui::text::{Line, Span}; + +type Cell = Vec>; + +struct Table { + aligns: Vec, + rows: Vec>, + row: Vec, + cell: Cell, +} + +struct W { + out: Vec>, + cur: Vec>, + styles: Vec