Skip to content

Styled text spans: more than one colour per line #60

Closed

Description

@ralyodio

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 readm3 actually 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.

https://docs.rs/ratatui/latest/ratatui/text/index.html

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

  1. added
    enhancement
    on Sep 8, 2026
  2. added a commit that references this issue on Sep 12, 2026
    43d0e3e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions