Repository navigation
fix: frame escape sequences correctly when truncating and wrapping - #138
Merged
Merged
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Item 2 from the review — the CSI-only assumption in
style/overflow.zigandstyle/compress.zigthat #136 fixed inmeasure.zig. It turned out to be worse than "OSC isn't handled".The bug
overflow.zigcarried five copies of the same escape scanner,compress.ziga sixth. All of them assume a sequence isESC [… 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[?25hHelloends ath, but a scan for the firstA-Za-zruns past it to theHof "Hello" and swallows it: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:
In
compressAnsithe 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.zigalso carried its own hand-rolled table of wide codepoint ranges. It now defers tounicode.charWidth, so wrapping agrees with whatmeasure.widthreported, and combining marks stop counting as a column.Also
applyOverflowis now exported.zz.Overflowwas public but the function that applies it was not reachable from outside the library.Tests
9 new cases in
tests/style_tests.zigplus 6 forescapeSequenceLeninansi.zig. 7 of the 9 fail against the current code — I checked by stashing the fix and re-running:zig build testandzig buildclean on 0.16.0.