Skip to content

Erase only the OSC 66 block a write touches, not every block sharing its span - #189

Merged
tomlm merged 2 commits into
tomlm:mainfrom
confetty-sh:stack/xterm-net-osc-claude
Nov 9, 2026
Merged

tomlm merged 2 commits into
tomlm:mainfrom
confetty-sh:stack/xterm-net-osc-claude

Conversation

@JohnCampionJr

Copy link
Copy Markdown
Collaborator

Fixes tomlm/Iciclecreek.Avalonia.Terminal#211

Cause

BufferLine.NoteSizedRun joins adjacent OSC 66 blocks that share a sizing into one span, so three separate w=2 blocks at [0,2), [2,4), [4,6) are stored as one span [0,6). When a later write overlapped any part of that span, EraseSizedRunsOver removed the whole span and blanked all of its cells, which took the untouched neighbouring blocks with it. OpenCode prints each CJK character as its own w=2 block, so pressing Backspace (two spaces over the last character) blanked the whole line. With w=1 the same thing happens: ABC becomes empty.

Fix

Spans are still joined, so SizedRuns and every reader of it see the same shape as before. Erasure now works on the blocks inside a span:

  • EraseSizedRunsOver widens the erased range only to the edges of the blocks it touches. It blanks just those blocks and puts the rest of the span back as up to two runs, in place, so the list stays left to right.
  • Finding the edges (new BlockStartAt / BlockEndAt):
    • Explicit w: every block in the span is s * w columns, because they share a sizing, so the edges are arithmetic.
    • w=0: each grapheme is its own block of s times its width, so the edges are read from the cells outside the written range. A block's first cell carries its full width and the rest are zero-width.
    • Reading the cells has one catch. By the time the sized-run bookkeeping runs, the print path's wide-cell repair (RepairAround) has already turned the other half of a two-column cell into a one-column space. So a cell whose width isn't a multiple of s is not taken as a block start. At s=1 with w=0 (a fractional-only sizing) such a space can't be told apart from a block, and it stays in the span as a one-column blank. It is blank either way.

A partial overwrite of one multicell block still erases that whole block, as before.

Tests

New TextSizingBlockBoundaryTests (23 tests). 15 of them failed before the fix; the 7 that passed are the controls and single-block cases.

  • The issue's repro: three w=2 blocks 甲乙丙, then CSI 1;5H and two spaces, leaves 甲乙. Three w=1 blocks ABC leave AB.
  • The captured OpenCode sequence (six blocks on row 17, then the Backspace chunk) leaves 甲乙中文测.
  • Overwriting the first, middle or last block, with spaces or with a replacement character, including writes that start on a block's second half and a wide character straddling two blocks.
  • EL, ECH and DCH from inside the span.
  • A single block (one character, or a multi-character payload) is still erased whole, including when it sits between same-sizing neighbours.
  • w=0 scaled blocks from one sequence, including blocks of different widths (s=2 a甲b).
  • Erasing the row under a tall s=2 block erases only the block above that column.
  • After a split, SizedRuns stays left to right, TryGetSizedRunAt answers per piece, and a new block written at the end joins the span again.
  • The ordinary-CJK controls from the issue (BS BS EL, CUB 2 + EL / ECH 2 / DCH 2, two spaces) still leave 甲乙.

Results:

  • All TextSizing, Reflow and LineExitedViewport tests pass (134).
  • Mutation check over the TextSizing tests: 10 of 10 mutants killed. They covered reverting either edge to the whole span, dropping the rounding in either arithmetic edge, trusting a repaired cell on either side, ignoring whether the block reaches the column, dropping either surviving piece, and blanking to the span's end.
  • Iciclecreek.Avalonia.Terminal's sized-text and rendering tests built against this change all pass (60: SizedTextRendering, Clipping, FaintOpacity, WindowGeometryReport, LineCacheTearing, FrameCoherence, RendererViewport).

Performance

There is no change on the print path. The new work runs only inside EraseSizedRunsOver, for a run the write actually overlaps, which already removed that run and rewrote its cells. Per overlapped run it adds:

  • one modulo (explicit w), or a scan of at most one block's width of cells (w=0);
  • at most two List.Insert calls into a line's handful of runs.

Spans stay joined on purpose. Renderers call TryGetSizedRunAt per cell, linearly over a line's runs, so storing one run per block would make a long line of OSC 66 blocks quadratic to draw. I reasoned this through and did not run the bench.

🤖 Generated with Claude Code

@JohnCampionJr JohnCampionJr left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-merge review (Codex), for the PR author

One should-fix finding: overwriting the continuation of a narrow s=2/w=0 block can preserve half of that erased block in SizedRuns; the leftover span still occupies the row beneath and displaces later output.

Checked explicit-width boundaries, variable-width blocks, print/erase/resize call ordering, and the control's per-cell coverage use. The combined core PRs passed 212 filtered tests and the combined view PRs passed 71 filtered tests. Two focused regression assertions reproduce the leftover span and the displaced output. No performance benchmark was run.

Comment thread src/XTerm.NET/Buffer/BufferLine.cs Resolved
@JohnCampionJr
JohnCampionJr force-pushed the stack/xterm-net-osc-claude branch from 8f613db to f391fec Compare November 6, 2026 01:30

@JohnCampionJr JohnCampionJr left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-merge review follow-up (Codex), for the PR author

Rechecked the post-review delta at current head f391fec (the pushed form of the cited e5d6a67 fix). Resolved: both retained reproductions pass; the remaining span is [0,2), [4,6), and Y lands in the intended column below. The changed boundary rule and regression test look sound. No new findings in this delta. The combined filtered core run passed 231 tests.

@tomlm
tomlm merged commit eaed05d into tomlm:main Nov 9, 2026
5 checks passed
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.

CJK characters cannot be deleted properly with Backspace: OSC 66 overwrite erases preceding independent blocks

2 participants