Skip to content

fix: frame escape sequences correctly when truncating and wrapping - #138

Merged
meszmate merged 1 commit into
mainfrom
fix/overflow-compress-string-sequences
Aug 14, 2026
Merged

meszmate merged 1 commit into
mainfrom
fix/overflow-compress-string-sequences

Conversation

@meszmate

Copy link
Copy Markdown
Owner

Item 2 from the review — the CSI-only assumption in style/overflow.zig and style/compress.zig that #136 fixed in measure.zig. It turned out to be worse than "OSC isn't handled".

The bug

overflow.zig carried five copies of the same escape scanner, compress.zig a sixth. All of them assume a sequence is ESC [ … followed by a letter. That is wrong twice over:

A CSI can end in any byte from 0x40 to 0x7E, not just a letter. \x1b[?25hHello ends at h, but a scan for the first A-Za-z runs past it to the H of "Hello" and swallows it:

applyOverflow("\x1b[?25hHello, World!", 5, .hidden)
  before -> "\x1b[?25hHello, W"      (14 columns, and the H is inside the "sequence")
  after  -> "\x1b[?25hHello"

OSC, DCS and APC are not recognised at all, so their payloads are measured as visible text. A hyperlink's URL or a Kitty image's base64 eats the entire width budget:

applyOverflow("\x1b]8;;https://example.com\x07Hello, World!" ++ end, 5, .hidden)
  before -> "\x1b]8;;" + garbage    (the URL counted as 24 columns)
  after  -> "\x1b]8;;https://example.com\x07Hello"

In compressAnsi the same gap means a tmux passthrough — which wraps SGR bytes inside its payload by design — can have those inner bytes treated as top-level sequences and rewritten by the reset dedup.

The fix

ansi.escapeSequenceLen(text, pos) frames a sequence without interpreting it: CSI by parameter/intermediate/final byte ranges, SS3, and the string sequences (OSC/DCS/APC/PM/SOS) by their terminators. An unfinished sequence reports the remaining length so callers always advance.

All six sites route through it. Six copies of subtly wrong scanning become one function with tests.

overflow.zig also carried its own hand-rolled table of wide codepoint ranges. It now defers to unicode.charWidth, so wrapping agrees with what measure.width reported, and combining marks stop counting as a column.

Also

applyOverflow is now exported. zz.Overflow was public but the function that applies it was not reachable from outside the library.

Tests

9 new cases in tests/style_tests.zig plus 6 for escapeSequenceLen in ansi.zig. 7 of the 9 fail against the current code — I checked by stashing the fix and re-running:

FAIL overflow.hidden measures a hyperlink as zero width
FAIL overflow.hidden steps over an image sequence
FAIL overflow.hidden handles CSI sequences ending in a non-letter
FAIL overflow.ellipsis measures a hyperlink as zero width
FAIL overflow.char_wrap measures a hyperlink as zero width
FAIL overflow.word_wrap measures a hyperlink as zero width
FAIL overflow uses shared width tables for wide characters
FAIL compressAnsi keeps CSI sequences ending in a non-letter intact

zig build test and zig build clean on 0.16.0.

`overflow.zig` carried five copies of the same escape scanner, and
`compress.zig` a sixth. All of them assumed a sequence is `ESC [` ...
followed by a letter, which is wrong twice over:

- A CSI can end in any byte from 0x40 to 0x7E. `\x1b[?25hHello` ends at
  'h', but a scan for the first A-Za-z runs on to the 'H' of "Hello" and
  swallows it.
- OSC, DCS and APC are not recognised at all, so a hyperlink or an
  inline image has its payload measured as visible text -- and a tmux
  passthrough, which wraps SGR bytes by design, could have those inner
  bytes rewritten by the reset dedup.

Adds `ansi.escapeSequenceLen`, which frames a sequence without
interpreting it, and routes all six sites through it. Six copies of
subtly wrong scanning become one function with tests.

`overflow.zig` also carried a hand-rolled table of wide codepoint
ranges. It now defers to `unicode.charWidth`, so wrapping agrees with
what `measure.width` reported and combining marks stop counting as a
column.

`applyOverflow` is exported: `zz.Overflow` was public but the function
that applies it was not reachable.
@meszmate
meszmate merged commit 0f32acd into main Aug 14, 2026
9 checks passed
@meszmate
meszmate deleted the fix/overflow-compress-string-sequences branch August 28, 2026 12:09
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