Skip to content

Styled text spans: more than one colour per line - #65

Merged
ralyodio merged 1 commit into
mainfrom
spans-richtext
Sep 8, 2026
Merged

ralyodio merged 1 commit into
mainfrom
spans-richtext

Conversation

@ralyodio

@ralyodio ralyodio commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Closes #60 for TypeScript. The six ports are follow-up work — see the last section.

What was wrong

drawText applied one Style to the whole string:

const style: Style = { fg: options.fg ?? surface.theme.foreground, ... };
const lines = options.wrap ? wrap(content, surface.width) : content.split("\n");

so two colours on one line meant splitting into separate widgets at hand-computed columns. Syntax highlighting, inline log-level colouring, diffs and search-match highlighting were all off the table.

What this adds

Span → SpanLine → RichText, plus span-aware measuring, truncation, padding and word wrap in a new packages/hqtui/src/richtext.ts; a Surface.spans() renderer; and RichText acceptance on drawText and ui.text / ui.label / ui.heading.

ui.text([
  { text: "ERROR ", fg: theme.danger, bold: true },
  { text: "connection refused" },
]);

A bare string is the one-span case, so no existing call site changes. Spans inherit the widget's style for anything they don't set, which is why ui.label([{ text: "muted" }, { text: "red", fg: RED }]) keeps the label colour on the first span.

Why you can trust the layout

Two text layout engines that disagree by one column is a bug you find in a screenshot six months later. So the span versions are held to the exact output of the string versions they mirror — richtext.test.ts asserts truncateSpans/fitSpans/wrapSpans match truncate/fit/wrap column for column across a corpus of CJK, emoji, ZWJ family sequences, combining marks, leading/trailing whitespace, blank lines and overlong unbroken tokens, at eight widths.

That test earned its keep on the first run: it caught wrapSpans not trimming its final line, which wrap does. That would have shipped as one stray trailing space per wrapped paragraph.

Why no existing frame moves

The plain-string path inside drawText is deliberately left intact rather than routed through the span code. The two agree, but agree and emit identical cells are different claims, and every committed fixture depends on the second one.

Both generators confirm it, with no drift:

  • bun ports/conformance/generate.ts
  • bun apps/demo/scripts/native-parity.ts — 120 cases

Checks

  • 232 tests pass under bun test and bun run test:node (was 208; +24 here)
  • bun run typecheck clean

Not in this PR

  • The six ports. This is additive and changes no existing frame, so ports/{rust,go,python,zig,cpp} and the COBOL adapter stay in parity — they simply don't offer spans yet. Porting is the rest of Styled text spans: more than one colour per line #60, and the parity corpus above is the spec to port against.
  • Widgets beyond text. List items, table cells, log messages and panel titles still take string. Those are the next increment now that the core exists, and they are what a JSON response inspector or a diff view actually needs.

🤖 Generated with Claude Code

https://claude.ai/code/session_017Df2FNu5DhinMV2soRz3cy

Closes #60 for TypeScript.

drawText applied a single Style to the whole string, so anything needing
two colours on one line had to be split into separate widgets at
hand-computed columns. That ruled out syntax highlighting, inline log
level colouring, diffs and search-match highlighting.

Adds Span/SpanLine/RichText plus span-aware measuring, truncation,
padding and word wrap, a Surface.spans renderer, and RichText acceptance
on drawText and ui.text/label/heading. A bare string is the one-span
case, so every existing call site is untouched.

The span layout is held to the exact output of the string functions it
mirrors: richtext.test.ts asserts truncateSpans/fitSpans/wrapSpans agree
with truncate/fit/wrap column for column across a corpus of CJK, emoji,
ZWJ sequences, combining marks and overlong tokens. That parity test
earned its keep immediately -- it caught wrapSpans not trimming its
final line, which wrap does.

The plain-string path in drawText is deliberately left intact rather
than routed through the span code. The two agree, but "agree" and "emit
identical cells" are different claims and every committed fixture
depends on the second. Both fixture generators confirm it: no drift in
ports/conformance or in the 120 demo-parity cases.

232 tests pass under Bun and Node.

The six ports do not have spans yet. This is additive and changes no
existing frame, so they stay in parity; porting is the remaining work
on #60.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_017Df2FNu5DhinMV2soRz3cy
@ralyodio
ralyodio merged commit 43d0e3e into main Sep 8, 2026
13 of 14 checks passed
@ralyodio
ralyodio deleted the spans-richtext branch September 8, 2026 14:02
@ralyodio ralyodio mentioned this pull request Sep 8, 2026
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.

Styled text spans: more than one colour per line

1 participant