Skip to content

feat(infinity-agent-cli): render streamed Markdown in the TUI - #196

Draft
MingweiSamuel wants to merge 4 commits into
mainfrom
mingwei/tui-markdown
Draft

MingweiSamuel wants to merge 4 commits into
mainfrom
mingwei/tui-markdown

Conversation

@MingweiSamuel

@MingweiSamuel MingweiSamuel commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Model output in the CLI was printed as raw Markdown (**bold**, # headings, | tables |). This adds a streaming Markdown renderer and uses it to style agent text as it arrives.

New crate: infinity-markdown

A streaming Markdown → styled-segments renderer with no dependencies beyond unicode-width.

  • Renderer::push(&str) -> Vec<Segment> / finish(): feed text in arbitrary chunks (boundaries may fall inside **, ](, etc.); get back runs of text with a Style (bold, italic, underline, strikethrough, dim, 16-color fg) and an optional href.
  • Built for output that can't be redrawn (terminal scrollback): everything is decided with at most one character of lookahead, so text streams with essentially no delay. Inline styles are optimistic — switched on at an opening delimiter (CommonMark flanking rules, which only need the previous/next char) and off at the closer or end of line. An unclosed ** styles the rest of its line rather than ever buffering a paragraph.
  • Held back only where unavoidable: block prefixes for the few chars needed to classify a line (- , 1. , # , ---, fences), link text (the URL follows it), and tables (column widths depend on every cell).
  • Supported: ATX headings, bullet/ordered/task lists (•, ☐, ☑), block quotes (│ gutter), thematic breaks, fenced code blocks (framed with dim rules + language label, contents verbatim), */_ emphasis, strong, ~~strike~~, code spans, backslash escapes, links/images (text + dim (url) when they differ), and GFM tables. Unknown syntax passes through verbatim.
  • Tables are drawn with box characters. Column widths are the natural widths, shrunk widest-first to fit the terminal; cell text then word-wraps inside its column. Delimiter-row alignment is honoured, cells get full inline rendering, header cells are bold, and a blank row separates body rows. Ragged rows are padded/truncated to the header's column count; \| is a literal pipe. A | line is released as text as soon as the next line can't be a delimiter row. If even one column of content per cell doesn't fit, the source lines are shown as text.
  • Theme makes per-construct styles configurable; set_width controls rule/fence/table width.

infinity-agent-cli

  • New markdown module maps infinity_markdown::Style to ratatui styles and prints segments via print_spans_above (only SGR reaches the viewport's OutputTracker).
  • terminal.rs: root-thread TextChunks go through the renderer; end_stream and ResponseDone call finish() to flush held text (e.g. a table at the end of a message). Child-thread status text and ACP output are untouched.
  • Fix in inline_viewport::write_spans: each span's modifiers were computed as a delta on the previous span (next = mods; next.insert(add)), so a plain span after an italic one never emitted italic-off; styles accumulated until the end-of-print reset. Span styles are now absolute (add_modifier - sub_modifier), matching ratatui's own semantics. This was latent until the renderer became the first caller to turn a modifier off mid-line.

Tests

  • Renderer: ~50 unit tests on inline/block/table behaviour, plus chunking-transparency checks (per-char and every two-way split of several documents must render identically to a single push).
  • write_spans: byte-level checks that SGR off-codes (23, 22) are emitted between spans.
  • TUI harness: new Emulator::row_attrs (vt100 + alacritty) and TuiHarness::screen_with_attrs, rendering a per-cell attribute grid (B/I/U/S/D/C) under the screen so on/off styling is verifiable. New snapshots: markdown_streams_rendered, markdown_styles_switch_off, markdown_tables.

Not in this PR

  • OSC 8 hyperlinks (Segment.href is already populated; needs OutputTracker to pass OSC strings through).
  • A raw-Markdown toggle.
  • Tables without leading pipes (a | b / --|--).

MingweiSamuel and others added 4 commits November 18, 2026 18:59
Adds a new `infinity-markdown` crate — a streaming Markdown renderer — and
wires it into the CLI so model output is styled as it arrives instead of
showing raw `**`, `#`, `` ` `` markers.

## `crates/infinity-markdown` (new)

* `Renderer::push(&str) -> Vec<Segment>` / `finish()`: feed text in
  arbitrary chunks; get back runs of text with a `Style` (bold, italic,
  underline, strikethrough, dim, 16-color fg) and an optional `href`.
* Decides what each character means with at most one character of
  lookahead, so text streams with essentially no delay. Inline styles are
  *optimistic*: switched on at an opening delimiter (using CommonMark
  flanking rules, which only need the previous/next char) and off at the
  closer or end of line — an unclosed `**` styles the rest of its line
  rather than ever buffering a paragraph.
* Only two constructs are held back: link text (the URL follows it) and
  block prefixes (`- `, `1. `, `# `, `---`, fence lines) for the few chars
  needed to classify the line.
* Supported: headings, bullet/ordered/task lists (`•`, `☐`, `☑`), block
  quotes (`│` gutter), thematic breaks, fenced code blocks (framed with
  dim rules and the language label, contents verbatim), `*`/`_` emphasis,
  strong, `~~strike~~`, code spans, backslash escapes, links/images
  (text + dim ` (url)` when they differ). Unknown syntax passes through.
* `Theme` makes the per-construct styles configurable; `set_width`
  controls rule/fence width.
* Tests include a chunking-transparency check: per-char and every two-way
  split of several documents must render identically to a single push.

## `crates/infinity-agent-cli`

* New `markdown` module maps `infinity_markdown::Style` to ratatui styles
  and prints segments via `print_spans_above` (so only SGR sequences reach
  the viewport's `OutputTracker`).
* `terminal.rs`: root-thread `TextChunk`s go through the renderer;
  `end_stream` and `ResponseDone` call `finish()` to flush held text.
  Child-thread text (status line) and ACP output are untouched.
* New TUI snapshot test `markdown_streams_rendered` with chunk boundaries
  inside `**` and `](`.

Not in this change (next stages): tables (box drawing), OSC 8 hyperlinks
(needs `OutputTracker` to pass OSC through), a raw-markdown toggle.


Co-authored-by: Infinity 🤖 <infinity@hydro.run>
Follow-up to the streaming Markdown rendering: italics/bold stayed on
after the closing delimiter until the end of the line.

## Root cause

`InlineViewport::write_spans` computed each span's modifiers as a delta on
the *previous span* (`next = mods; next.insert(add); next.remove(sub)`), so a
plain span after an italic one never removed `ITALIC`. Span styles are
meant to be absolute (ratatui patches each span onto the line style, not
onto its neighbour). The bug was latent: until now no caller ever turned a
modifier off mid-line.

Fix: `next = span.style.add_modifier - span.style.sub_modifier`.

## Tests

* `inline_viewport::tests`: byte-level checks that `write_spans` emits the
  SGR "off" codes (`23`, `22`) between spans.
* Test harness: new `Emulator::row_attrs` (vt100 + alacritty) and
  `TuiHarness::screen_with_attrs`, rendering a per-cell attribute grid
  (`B`old/`I`talic/`U`nderline/`S`trike/`D`im/`C`olored) under the screen —
  the text-only snapshots could not see this class of bug.
* `tui_snapshots::markdown_styles_switch_off`: streams a response mixing
  emphasis with plain text, lists, quotes, headings, links and an unclosed
  `*`, and snapshots the attribute grid.


Co-authored-by: Infinity 🤖 <infinity@hydro.run>
Stage 2 of Markdown rendering in the CLI: pipe tables are drawn as boxed
grids instead of raw `| a | b |` text.

## Behaviour

* A line starting with `|` is held; if the next line is a delimiter row
  (`|---|:-:|--:|`) the table starts, otherwise the line is released as
  text. The candidate is rejected as early as the first character that
  can't belong to a delimiter row.
* Rows accumulate while lines start with `|`. The table is drawn on the
  first non-row line, a blank line, or `finish()` — including the common
  case of a message ending in a table row with no trailing newline.
* Cells get full inline rendering (bold, code, links); header cells use
  `Theme::table_header` (bold), borders `Theme::table_border`.
* Column widths are the natural (widest cell) widths, shrunk widest-first
  to fit the terminal; cell text then word-wraps inside its column (long
  words split by character; wide chars measured with `unicode-width`).
  Delimiter-row alignment (left/center/right) is honoured.
* Ragged rows are padded/truncated to the header's column count; `\|`
  is a literal pipe. If even one column of content per cell doesn't fit,
  the source lines are shown as text.

## Code

* New `table.rs`: `parse_row`, `parse_delimiter_row`, `column_widths`,
  `wrap_cell`, `pad_line`, `border` (pure functions, unit-tested).
* `render.rs`: `Line::TableCandidate` / `Line::Table` states,
  `render_table`, `emit_table_row`, and `render_inline` (renders a cell's
  inline Markdown in isolation by swapping out the output/inline state).
  `finish()` now also handles a lone held `|` line.
* Tests: renderer-level tables (alignment, wrapping, ragged rows, fallback,
  termination variants, hold/release timing) plus per-char chunking
  transparency; CLI snapshot `markdown_tables` streams two tables in
  awkward chunks and checks the attribute grid.


Co-authored-by: Infinity 🤖 <infinity@hydro.run>
Rows whose cells wrap were hard to tell apart; an empty row
(`│       │       │`) is now emitted between consecutive body rows. The
header separator and outer borders are unchanged, and single-row tables
look the same as before.

* `render.rs`: `emit_table_blank_row`, called between body rows in
  `render_table`.
* Updated renderer tests and the `markdown_tables` CLI snapshots.


Co-authored-by: Infinity 🤖 <infinity@hydro.run>
@MingweiSamuel MingweiSamuel changed the title feat(infinity-markdown): blank row between table body rows feat(infinity-agent-cli): render streamed Markdown in the TUI Nov 20, 2026
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying infinity with  Cloudflare Pages  Cloudflare Pages

Latest commit: 5f72807
Status: ✅  Deploy successful!
Preview URL: https://75a7989b.infinity-dc7.pages.dev
Branch Preview URL: https://mingwei-tui-markdown.infinity-dc7.pages.dev

View logs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant