Skip to content

Preserve cell styles when clipping overtyped text - #305

Closed
civilwarinfrance wants to merge 1 commit into
jquast:masterfrom
civilwarinfrance:fix/overtyping-cell-styles
Closed

civilwarinfrance wants to merge 1 commit into
jquast:masterfrom
civilwarinfrance:fix/overtyping-cell-styles

Conversation

@civilwarinfrance

Copy link
Copy Markdown
Contributor

Clipping a colored line after a backspace or carriage return can apply the wrong style to both overwritten and untouched characters. For example, clip('\x1b[31mabc\x1b[0m\bX', 0, 3) currently renders all of abX in red, although X was written after the reset and should use the default color.

Record the immutable SGR state alongside painted cells and restore it when reconstructing them in column order. Track the reconstructed output's final style for the trailing reset. This also preserves the style of untouched text when an overwrite introduces a new color. The C extension already delegates cursor movement to this Python painter, so it uses the same fix.

Validation:

  • Added 64 regression cases covering backspace, carriage return, cursor-left/absolute positioning, clipped windows, resets, foreground/background colors, bold, truecolor, combining characters, CJK and emoji. Original code: 60 failures and 4 passing controls.
  • Python-only suite: 1,834 passed, 47 skipped. Extension-enabled suite: 1,870 passed, 11 skipped, including the existing path-parity tests.
  • An additional local comparison of 4,000 generated ASCII/style/cursor cases against Pyte found zero mismatches with this change (3,876 mismatches before it). Pyte is only a local verification tool; no dependency was added.
  • Flake8, Pylint, strict Mypy, docformatter, pydocstyle, doc8, codespell and the strict Sphinx build pass. Changed files pass isort; the full isort check reports an existing formatting difference in unchanged wcwidth/__init__.py, also present on the original revision.

AI assistance was used to investigate, implement and test this change.

jquast added a commit that referenced this pull request Nov 9, 2026
This is a set of improvements on the fix described by AI Static Analysis in #305, I will include the text below. Parse each SGR once only while painting, and 'queue' the parsed parameters, memoize the state apply and sequence builder. Use tuples for
cell style instead of a 'parallel dict'.

> Clipping a colored line after a backspace or carriage return can apply the wrong style to both overwritten and untouched characters. For example, clip('\x1b[31mabc\x1b[0m\bX', 0, 3) currently renders all of abX in red, although X was written after the reset and should use the default color.

> Record the immutable SGR state alongside painted cells and restore it when reconstructing them in column order. Track the reconstructed output's final style for the trailing reset. This also preserves the style of untouched text when an overwrite introduces a new color. The C extension already delegates cursor movement to this Python painter, so it uses the same fix.

---------

Co-authored-by: leo <[email protected]>
@jquast jquast closed this in #315 Nov 9, 2026
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