Skip to content

Draw the colours a code-block renderer prints - #3771

Merged
sinelaw merged 1 commit into
masterfrom
ccr-783c7247-2jcxvs
Nov 17, 2026
Merged

sinelaw merged 1 commit into
masterfrom
ccr-783c7247-2jcxvs

Conversation

@sinelaw

@sinelaw sinelaw commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Compose mode draws a code block with whatever its renderers command prints. Tools that draw in colour (delta, bat, chafa, a coloured plot) print ANSI escape codes, and those went into the frame as text. The codes are invisible on screen but were counted as columns, so:

  • a coloured row came out short;
  • the frame's right rail went ragged;
  • a drawing made of coloured cells, such as an image, was garbled.

What changed:

  • Plugin (markdown_compose.ts):
    • Each row the tool prints is read into its text and its styled runs.
    • SGR codes become colours: the 16 colours, 256-colour, truecolor, bold, italic and underline.
    • Every other escape code (OSC titles, cursor moves and similar) and every other control character is dropped.
    • Tabs expand to stops of 8.
  • addConceal takes a new replacementOverlays argument: styled byte ranges within the replacement text. The conceal pass emits one token per styled run, and replacements without overlays are emitted as before.
  • Virtual-line textOverlays now carry fg, bg and italic as well as bold and underline. They draw rows past the block's last source line.
  • Generated types: VirtualLineTextOverlay is now exported in fresh.d.ts; the types and API docs were regenerated.

Known limitation: a row that falls on a blank line inside the block is inline virtual text, which takes only one style. It is drawn in the frame's colour, but its width is still correct.

Tests:

  • New e2e test a_tools_colours_are_drawn_and_its_escape_codes_take_no_columns. It uses a stub tool that prints red, bold truecolor green and a blue background, plus an OSC title. The test checks that:

    • no escape text is drawn;
    • every row's rail lines up with the frame corner;
    • the cell colours are right, including on the virtual-line row.

    Against the old plugin it fails: the rail lands at column 95 instead of 104.

  • Unit tests for the styled conceal emission and the virtual-line colours.

  • All 171 markdown_compose e2e tests pass.

  • fmt, clippy (no new warnings) and check-types.sh are clean.

  • I agree my contributions are licensed under GPL-3.0-or-later and may be relicensed in the future at the maintainer's discretion (see CONTRIBUTING.md).


Generated by Claude Code

Compose mode draws a code block with whatever its `renderers` command
prints. Tools that draw in colour - delta, bat, chafa, a coloured plot -
print ANSI escape codes, and those went into the frame as text: invisible
when drawn but counted as columns, so a coloured row came out short, the
frame's right rail went ragged, and a drawing made of coloured cells (an
image) was garbled. Users had to pipe every tool through sed to strip the
codes, losing the colour the tool was chosen for.

The plugin now reads each row into its text and its styled runs: SGR codes
become colours (16 colours, 256-colour and truecolor, bold, italic,
underline), and every other escape code is dropped. The runs reach the
screen as overlays on the row's text, which needed two editor changes:

- `addConceal` takes `replacementOverlays`, styled byte ranges within the
  replacement. The conceal pass emits one token per run, carrying its
  style; a range with none is drawn as before.
- A virtual line's `textOverlays` now carry `fg`, `bg` and `italic` as
  well as bold and underline, for drawing rows past the block's last line.

A row that lands on a blank source line is still inline virtual text,
which takes a single style, so it is drawn in the frame's colour; its
width is right all the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012MC4RTeiS6hZV3vHLXYqD9
@sinelaw
sinelaw enabled auto-merge (squash) November 17, 2026 17:57
@sinelaw
sinelaw merged commit e22858b into master Nov 17, 2026
42 checks passed
@sinelaw
sinelaw deleted the ccr-783c7247-2jcxvs branch November 17, 2026 18:26
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.

2 participants