Repository navigation
Description
You cannot colour part of a line. One Style applies to the whole string, so anything that needs two colours in one run of text has to be split into separate widgets at hand-computed columns.
Today
export function drawText(surface: Surface, content: string, options: TextOptions = {}): void {
if (surface.empty) return;
const style: Style = {
fg: options.fg ?? surface.theme.foreground,
bg: options.bg,
attrs: attrsOf(options),
};
const lines = options.wrap ? wrap(content, surface.width) : content.split("\n");
...
}packages/hqtui/src/widgets/text.ts:27. One style, computed once, applied to every line. The same is true of list items, table cells, log messages, tree labels and panel titles: each takes a string plus at most one colour.
What this blocks
- Syntax highlighting of any kind.
- A log line whose level is coloured but whose message is not — today
log()colours the level column because the widget special-cases it; arbitrary inline emphasis is not possible. - Diff rendering (added/removed spans inside a line).
- Highlighting search matches inside a table cell or list item.
- Markdown-ish inline emphasis, which is what
readm3actually wants.
Every one of those is currently either impossible or a manual column-arithmetic exercise against stringWidth.
What ratatui does
Span (styled string) → Line (spans plus alignment) → Text (lines). It is a core primitive, not a widget: every widget that accepts text accepts impl Into<Text>, so styling composes everywhere for free.
Proposal
Introduce a span type and make it accepted anywhere a string is today:
type Span = { text: string; fg?: Color; bg?: Color; attrs?: number; bold?: boolean; ... };
type Line = Span[];
type RichText = string | Span[] | Span[][];
ui.text([
{ text: "ERROR ", fg: theme.danger, bold: true },
{ text: "connection refused" },
]);string stays valid everywhere — it is the one-span case — so this is additive and no existing call site changes.
The mechanical work is real: wrap, truncate and fit in packages/hqtui/src/unicode.ts all operate on strings and would need span-aware equivalents that split runs at grapheme boundaries without losing style. That is the part to get right first, because every widget depends on it.
Cost
The largest item on the list. It touches the text core plus every widget that renders text, in all six ports, with fixtures regenerated. Worth scheduling deliberately rather than drifting into — and worth doing sooner rather than later, because the migration only grows with the widget count.
Activity
feat(text): styled spans, so a line can carry more than one colour (p…