From 43469f4731e5de03e3104a2a2ba7a43e5b347476 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 5 Dec 2026 10:27:07 -0600 Subject: [PATCH] terminal(fix[erase]): Sever wraps once per span why: Full-row display erases checked _sever_incoming_wrap on every cleared row even when the predecessor row had already been cleared in the same non-selective span, and DECCOLM clears skipped severing incoming scrollback wraps. what: - Add Screen._clear_rows with a consistent sever_boundary contract across non-selective and selective erases - Route ED 0/1/2/22 and DECCOLM full-row clears through _clear_rows and guard EL 0 / ED 0 incoming-wrap checks to c_col <= 1 - Add regression tests for bounded wrap checks, wrap/history boundaries, narrow-to-wide splits, DECCOLM, BCE, cols=1, DECSED, and ?2026 CoW - Record the fix in CHANGES under 0.0.1a7 Fixes Fixes #199 --- CHANGES | 7 + src/libvt/screen.py | 35 ++++ src/libvt/terminal.py | 35 ++-- tests/test_screen.py | 432 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 491 insertions(+), 18 deletions(-) diff --git a/CHANGES b/CHANGES index 51daf804..48cc0731 100644 --- a/CHANGES +++ b/CHANGES @@ -28,6 +28,13 @@ returned by {meth}`~libvt.snapshot.Snapshot.to_dict`. ### Fixes +#### Reduce full-display erase overhead (#199) + +Full-row display erases (`ED 0`, `ED 1`, `ED 2`, and `ED 22`) and `DECCOLM` +clears on {class}`~libvt.terminal.Terminal` now check incoming wrap boundaries +once per erased span rather than per cleared row. `DECCOLM` clears also sever +incoming scrollback wraps when clearing the display. + #### Reject attribute deletion on snapshots (#198) Deleting an attribute on {class}`~libvt.snapshot.Snapshot` now raises diff --git a/src/libvt/screen.py b/src/libvt/screen.py index fd50e446..1485a7b8 100644 --- a/src/libvt/screen.py +++ b/src/libvt/screen.py @@ -49,6 +49,7 @@ ) if t.TYPE_CHECKING: + from libvt._grid import _CellExt from libvt.graphics import GraphicsTracker, ImageMeta, ImagePlacement @@ -307,6 +308,40 @@ def _sever_incoming_wrap( self._clear_row_spacer_head(prev) prev.wrapped = False + def _clear_rows( + self, + start: int, + stop: int, + bce_word: int, + bce_ext: _CellExt | None, + *, + selective: bool = False, + sever_boundary: bool = True, + ) -> None: + """Clear full viewport rows ``[start, stop)`` with boundary wrap handling. + + When ``sever_boundary`` is ``True``, check and sever an incoming wrap + from the row before ``start`` (or scrollback when ``start == 0``). + Pass ``sever_boundary=False`` only when the caller already cleared the + predecessor's wrap link; selective erases still check internal rows + ``idx > start`` because protected cells may keep a prior row wrapped. + """ + if start >= stop: + return + rows = self._rows + cols = self._cols + if not selective: + if sever_boundary: + self._sever_incoming_wrap(start) + target_rows = rows if start == 0 and stop == len(rows) else rows[start:stop] + for row_obj in target_rows: + row_obj.reset_blank(cols, bce_word, bce_ext) + return + for idx in range(start, stop): + if sever_boundary or idx > start: + self._sever_incoming_wrap(idx, selective=True) + rows[idx].clear_range(0, cols, bce_word, bce_ext, selective=True) + def _heal_before_erase( self, row: int, start: int, end: int, *, selective: bool ) -> None: diff --git a/src/libvt/terminal.py b/src/libvt/terminal.py index 44d08dc7..1663967a 100644 --- a/src/libvt/terminal.py +++ b/src/libvt/terminal.py @@ -3492,9 +3492,7 @@ def _apply_deccolm(self, *, enabled: bool) -> None: ) if not self._modes.is_set(Mode.DECNCSM): bce_w, bce_e = self._bce_pair() - for r in range(scr.rows): - scr._rows[r].clear_range(0, scr.cols, bce_w, bce_e) - scr._rows[r].wrapped = False + scr._clear_rows(0, scr.rows, bce_w, bce_e) if scr._gfx is not None: scr._gfx.clear_active_screen(scr._scrolled_total, scr.rows) self._notify_erase(Rect(0, 0, scr.rows, scr.cols), EraseCause.ED) @@ -3661,7 +3659,8 @@ def _erase_display( if mode == 0: if not phantom: - scr._sever_incoming_wrap(c_row, start=c_col, selective=selective) + if c_col <= 1: + scr._sever_incoming_wrap(c_row, start=c_col, selective=selective) scr._heal_before_erase(c_row, c_col, scr.cols, selective=selective) scr._rows[c_row].clear_range( c_col, @@ -3682,9 +3681,14 @@ def _erase_display( policy=sixel_pol, ) self._notify_erase(Rect(c_row, c_col, 1, scr.cols - c_col), cause) - for r in range(c_row + 1, scr.rows): - scr._sever_incoming_wrap(r, selective=selective) - scr._rows[r].clear_range(0, scr.cols, bce_w, bce_e, selective=selective) + scr._clear_rows( + c_row + 1, + scr.rows, + bce_w, + bce_e, + selective=selective, + sever_boundary=phantom or selective, + ) if c_row + 1 < scr.rows: if not selective and scr._gfx is not None and scr._gfx.has_sixels: scr._gfx.erase_sixel_rect( @@ -3701,10 +3705,8 @@ def _erase_display( ) elif mode == 1: end_c = scr.cols if phantom else c_col + 1 - for r in range(c_row): - scr._sever_incoming_wrap(r, selective=selective) - scr._rows[r].clear_range(0, scr.cols, bce_w, bce_e, selective=selective) - if c_row == 0 or end_c >= scr.cols: + scr._clear_rows(0, c_row, bce_w, bce_e, selective=selective) + if c_row == 0 or (selective and end_c >= scr.cols): scr._sever_incoming_wrap(c_row, selective=selective) scr._heal_before_erase(c_row, 0, end_c, selective=selective) scr._rows[c_row].clear_range( @@ -3738,9 +3740,7 @@ def _erase_display( self._notify_erase(Rect(0, 0, c_row, scr.cols), cause) self._notify_erase(Rect(c_row, 0, 1, end_c), cause) elif mode == 2: - for r in range(scr.rows): - scr._sever_incoming_wrap(r, selective=selective) - scr._rows[r].clear_range(0, scr.cols, bce_w, bce_e, selective=selective) + scr._clear_rows(0, scr.rows, bce_w, bce_e, selective=selective) if not selective and scr._gfx is not None: scr._gfx.clear_active_screen(scr._scrolled_total, scr.rows) self._notify_erase(Rect(0, 0, scr.rows, scr.cols), cause) @@ -3748,9 +3748,7 @@ def _erase_display( if not self._screens.active_is_alt: scr.push_scrollback_and_clear(bce_w) else: - for r in range(scr.rows): - scr._sever_incoming_wrap(r) - scr._rows[r].clear_range(0, scr.cols, bce_w, bce_e) + scr._clear_rows(0, scr.rows, bce_w, bce_e) if scr._gfx is not None: scr._gfx.clear_active_screen(scr._scrolled_total, scr.rows) self._notify_erase(Rect(0, 0, scr.rows, scr.cols), cause) @@ -3775,7 +3773,8 @@ def _erase_line( c_col = scr._cur_col sixel_pol = self._profile.screen.sixel_erase_policy if mode == 0: - scr._sever_incoming_wrap(c_row, start=c_col, selective=selective) + if c_col <= 1: + scr._sever_incoming_wrap(c_row, start=c_col, selective=selective) scr._heal_before_erase(c_row, c_col, scr.cols, selective=selective) scr._rows[c_row].clear_range( c_col, diff --git a/tests/test_screen.py b/tests/test_screen.py index 33db6428..c58fdbb6 100644 --- a/tests/test_screen.py +++ b/tests/test_screen.py @@ -4,6 +4,7 @@ import collections.abc import copy +import cProfile import gc import pickle import random @@ -23,6 +24,7 @@ from libvt.screen import Cursor, RowView, Screen from libvt.style import ( AnsiColor, + Color, Hyperlink, IndexedColor, RgbColor, @@ -398,6 +400,436 @@ def test_erase_history_wrap_cow_and_held_frame(hard_lines: int) -> None: assert not term.changes_since(delta.token).full_resync +@pytest.mark.parametrize( + ("frame", "expected_cursor", "expected_cols"), + [ + pytest.param( + b"\x1b[H" + + b"row0 " * 14 + + b"\x1b[0K\r\n" + + b"row1 " * 14 + + b"\x1b[1K\x1b[2J", + (1, 70), + 80, + id="ed2-redraw", + ), + pytest.param( + b"\x1b[H" + b"row0 " * 14 + b"\r\n" + b"row1 " * 14 + b"\x1b[H\x1b[J", + (0, 0), + 80, + id="ed0-home", + ), + pytest.param( + b"\x1b[H" + b"row0 " * 14 + b"\r\n" + b"row1 " * 14 + b"\x1b[24;80H\x1b[1J", + (23, 79), + 80, + id="ed1-bottom", + ), + pytest.param( + b"\x1b[?1049h\x1b[H" + b"row0 " * 14 + b"\x1b[22J", + (0, 70), + 80, + id="ed22-alt", + ), + pytest.param( + b"\x1b[?40h\x1b[H" + b"row0 " * 14 + b"\x1b[?3l", + (0, 0), + 80, + id="deccolm-80", + ), + pytest.param( + b"\x1b[?40h\x1b[H" + b"row0 " * 14 + b"\x1b[?3h", + (0, 0), + 132, + id="deccolm-132", + ), + ], +) +def test_full_display_erase_checks_incoming_wrap_only_at_boundary( + frame: bytes, + expected_cursor: tuple[int, int], + expected_cols: int, +) -> None: + """Full-row display erases and DECCOLM check incoming wraps once per span.""" + repeats = 400 + term = libvt.Terminal(rows=24, cols=80) + with cProfile.Profile() as profile: + term.feed(frame * repeats) + + sever_calls = sum( + entry.callcount + for entry in profile.getstats() + if not isinstance(entry.code, str) + and entry.code.co_name == "_sever_incoming_wrap" + ) + assert sever_calls == repeats + assert term.cols == expected_cols + assert term.lines == ("",) * 24 + assert (term.cursor.row, term.cursor.col) == expected_cursor + assert all(not row.wrapped for row in term.screen._rows) + _assert_grid_invariants(term) + + +@pytest.mark.parametrize("bytewise", [False, True], ids=["whole", "bytewise"]) +@pytest.mark.parametrize( + ("seq", "expected_cols"), + [(b"\x1b[?40h\x1b[?3h", 132), (b"\x1b[?40h\x1b[?3l", 80)], + ids=["deccolm-132", "deccolm-80"], +) +def test_deccolm_severs_history_wrap_and_honors_profile( + *, + bytewise: bool, + seq: bytes, + expected_cols: int, +) -> None: + """``DECCOLM`` clears sever scrollback wraps unless the profile keeps them.""" + stream = b"a" * 200 + seq + for profile, expected_history_wrapped in ( + (DEFAULT_PROFILE, False), + (GHOSTTY_PROFILE, True), + ): + term = libvt.Terminal(rows=2, cols=80, profile=profile) + if bytewise: + for byte in stream: + term.feed(bytes((byte,))) + else: + term.feed(stream) + _assert_grid_invariants(term) + assert term.cols == expected_cols + assert term.lines == ("", "") + assert (term.cursor.row, term.cursor.col) == (0, 0) + assert [r.wrapped for r in term.screen._rows] == [False, False] + assert term.history[-1].text == "a" * 80 + assert term.history[-1].wrapped is expected_history_wrapped + + +@pytest.mark.parametrize("bytewise", [False, True], ids=["whole", "bytewise"]) +def test_erase_display_preserves_and_severs_visible_wrap_boundaries( + *, + bytewise: bool, +) -> None: + """``ED 0``, ``ED 1``, and ``ED 3`` keep surviving wraps and sever erased ones.""" + + def _run( + data: bytes, + *, + rows: int = 3, + cols: int = 8, + profile: Profile = DEFAULT_PROFILE, + ) -> libvt.Terminal: + term = libvt.Terminal(rows=rows, cols=cols, profile=profile) + if bytewise: + for byte in data: + term.feed(bytes((byte,))) + else: + term.feed(data) + _assert_grid_invariants(term) + return term + + # ED 0 from row 1 col 0 severs surviving row 0 wrap. + t_ed0_col0 = _run(b"abcdefghijklmnopqr\x1b[2;1H\x1b[J") + assert t_ed0_col0.lines == ("abcdefgh", "", "") + assert [r.wrapped for r in t_ed0_col0.screen._rows] == [False, False, False] + assert (t_ed0_col0.cursor.row, t_ed0_col0.cursor.col) == (1, 0) + + # ED 0 from row 1 col 3 keeps row 0 -> row 1 wrap and severs row 1 -> row 2 wrap. + t_ed0_mid = _run(b"abcdefghijklmnopqrstuv\x1b[2;4H\x1b[J") + assert t_ed0_mid.lines == ("abcdefgh", "ijk", "") + assert [r.wrapped for r in t_ed0_mid.screen._rows] == [True, False, False] + assert t_ed0_mid.unwrapped_text == "abcdefghijk" + assert (t_ed0_mid.cursor.row, t_ed0_mid.cursor.col) == (1, 3) + + # ED 0 with phantom pending wrap leaves row 0 intact but severs its wrap into row 1. + phantom_prof = Profile(screen=ScreenBehavior(pending_wrap_phantom_col=True)) + t_ed0_phantom = _run( + b"abcdefghijklmnopqr\x1b[1;8HX\x1b[J", + profile=phantom_prof, + ) + assert t_ed0_phantom.lines == ("abcdefgX", "", "") + assert [r.wrapped for r in t_ed0_phantom.screen._rows] == [False, False, False] + assert ( + t_ed0_phantom.cursor.row, + t_ed0_phantom.cursor.col, + t_ed0_phantom.cursor.pending_wrap, + ) == (0, 7, True) + + # ED 1 from row 1 col 2 clears row 0 and preserves row 1 -> row 2 wrap. + t_ed1_mid = _run(b"abcdefghijklmnopqrstuv\x1b[2;3H\x1b[1J") + assert t_ed1_mid.lines == ("", " lmnop", "qrstuv") + assert [r.wrapped for r in t_ed1_mid.screen._rows] == [False, True, False] + assert (t_ed1_mid.cursor.row, t_ed1_mid.cursor.col) == (1, 2) + + # ED 1 from row 1 col 7 clears rows 0..1 completely and severs row 1 -> row 2 wrap. + t_ed1_eol = _run(b"abcdefghijklmnopqrstuv\x1b[2;8H\x1b[1J") + assert t_ed1_eol.lines == ("", "", "qrstuv") + assert [r.wrapped for r in t_ed1_eol.screen._rows] == [False, False, False] + assert (t_ed1_eol.cursor.row, t_ed1_eol.cursor.col) == (1, 7) + + # ED 1 from bottom-right clears all rows and severs the incoming history wrap. + t_ed1_full = _run(b"abcdefghijklmnopqrstuvwxyz1234\x1b[3;8H\x1b[1J") + assert t_ed1_full.lines == ("", "", "") + assert [r.wrapped for r in t_ed1_full.screen._rows] == [False, False, False] + assert [(r.text, r.wrapped) for r in t_ed1_full.history] == [("abcdefgh", False)] + t_ed1_full.feed(b"\x1b[HFresh") + t_ed1_full.resize(rows=3, cols=16, reflow=True) + assert t_ed1_full.lines == ("Fresh", "", "") + + # ED 3 clears scrollback while leaving viewport rows and wraps intact. + t_ed3 = _run(b"abcdefghijklmnopqr\x1b[3J", rows=2, cols=8) + assert not t_ed3.history + assert t_ed3.lines == ("ijklmnop", "qr") + assert [r.wrapped for r in t_ed3.screen._rows] == [True, False] + + +@pytest.mark.parametrize("bytewise", [False, True], ids=["whole", "bytewise"]) +def test_erase_display_wide_boundaries_bce_cols1_and_decsed( + *, + bytewise: bool, +) -> None: + """Display erases preserve CJK boundaries, BCE, ``cols=1``, and ``DECSED``.""" + + def _run( + data: bytes, + *, + rows: int = 3, + cols: int = 8, + profile: Profile = DEFAULT_PROFILE, + ) -> libvt.Terminal: + term = libvt.Terminal(rows=rows, cols=cols, profile=profile) + if bytewise: + for byte in data: + term.feed(bytes((byte,))) + else: + term.feed(data) + _assert_grid_invariants(term) + return term + + # CJK at cols-1 (SPACER_HEAD on row 0 -> WIDE on row 1) with ED 0 / ED 1. + t_cjk_ed0 = _run("abcdefg界xyz\x1b[2;2H\x1b[J".encode()) + assert t_cjk_ed0.lines == ("abcdefg", " ", "") + assert [r.wrapped for r in t_cjk_ed0.screen._rows] == [False, False, False] + assert t_cjk_ed0[0][7].width_role is WidthRole.NARROW + assert t_cjk_ed0[1][0].width_role is WidthRole.NARROW + + # Narrow wrap (no SPACER_HEAD) into wide cell at (1, 0..1): ED 0 / EL 0 at col 1 + # must sever row 0's wrap. + t_narrow_wide_ed0 = _run("abcdefgh界xyz\x1b[2;2H\x1b[J".encode()) + assert t_narrow_wide_ed0.lines == ("abcdefgh", " ", "") + assert not t_narrow_wide_ed0[0].wrapped + assert [r.wrapped for r in t_narrow_wide_ed0.screen._rows] == [False, False, False] + assert (t_narrow_wide_ed0.cursor.row, t_narrow_wide_ed0.cursor.col) == (1, 1) + assert t_narrow_wide_ed0[0][7].width_role is WidthRole.NARROW + assert t_narrow_wide_ed0[1][0].width_role is WidthRole.NARROW + + t_narrow_wide_el0 = _run("abcdefgh界xyz\x1b[2;2H\x1b[K".encode()) + assert t_narrow_wide_el0.lines == ("abcdefgh", " ", "") + assert not t_narrow_wide_el0[0].wrapped + assert [r.wrapped for r in t_narrow_wide_el0.screen._rows] == [False, False, False] + assert (t_narrow_wide_el0.cursor.row, t_narrow_wide_el0.cursor.col) == (1, 1) + assert t_narrow_wide_el0[0][7].width_role is WidthRole.NARROW + assert t_narrow_wide_el0[1][0].width_role is WidthRole.NARROW + + t_cjk_ed1_r0 = _run("abcdefg界xyz\x1b[1;8H\x1b[1J".encode()) + assert t_cjk_ed1_r0.lines == ("", " xyz", "") + assert [r.wrapped for r in t_cjk_ed1_r0.screen._rows] == [False, False, False] + assert t_cjk_ed1_r0[1][0].width_role is WidthRole.NARROW + + t_cjk_ed1_r1 = _run("abcdefg界xyz\x1b[2;1H\x1b[1J".encode()) + assert t_cjk_ed1_r1.lines == ("", " xyz", "") + assert [r.wrapped for r in t_cjk_ed1_r1.screen._rows] == [False, False, False] + + # Selective DECSED 0 / 1 / 2 keep protected cells and incoming wrap links. + prot_wide_stream = '\x1b[1"qabc\x1b[0"qdefg\x1b[1"q界\x1b[0"qijklmnop' + for seq, expected_row1_wrapped in ( + (b"\x1b[1;1H\x1b[?0J", False), + (b"\x1b[3;4H\x1b[?1J", True), + (b"\x1b[3;8H\x1b[?1J", False), + (b"\x1b[?2J", False), + ): + t_sel = _run(prot_wide_stream.encode() + seq) + assert t_sel[0].text == "abc" + assert t_sel[0].wrapped + assert t_sel[1].text == "界" + assert t_sel[1][0].text == "界" + assert t_sel[1][0].protected + assert t_sel[1][1].width_role is WidthRole.SPACER_TAIL + assert t_sel[1][2].text == " " + assert t_sel[1].wrapped is expected_row1_wrapped + assert t_sel[2].text == "" + + # Selective DECSED severs a protected predecessor's wrap when row 1 is erasable. + prot_prev_stream = '\x1b[1"qabcdefgh\x1b[0"qijklmnop' + for seq in ( + b"\x1b[1;1H\x1b[?0J", + b"\x1b[2;8H\x1b[?1J", + b"\x1b[3;4H\x1b[?1J", + b"\x1b[?2J", + ): + t_sel_prev = _run(prot_prev_stream.encode() + seq) + assert t_sel_prev.lines == ("abcdefgh", "", "") + assert not t_sel_prev[0].wrapped + assert [r.wrapped for r in t_sel_prev.screen._rows] == [False, False, False] + + # BCE on ED 2, ED 0, and ED 1 marks cleared full rows used==cols, text_used==0. + t_bce = _run(b"abcdef\r\nghijkl\r\nmnopqr\x1b[41m\x1b[2J") + assert t_bce.lines == ("", "", "") + for row_idx in range(3): + raw_row = t_bce.screen._rows[row_idx] + assert (raw_row.used, raw_row.text_used, raw_row.wrapped) == (8, 0, False) + assert all(t_bce[row_idx][c].style.bg is AnsiColor.RED for c in range(8)) + + t_bce_ed0 = _run(b"abcdef\r\nghijkl\r\nmnopqr\x1b[42m\x1b[2;3H\x1b[0J") + assert t_bce_ed0.lines == ("abcdef", "gh", "") + assert ( + t_bce_ed0.screen._rows[2].used, + t_bce_ed0.screen._rows[2].text_used, + t_bce_ed0.screen._rows[2].wrapped, + ) == (8, 0, False) + assert all(t_bce_ed0[2][c].style.bg is AnsiColor.GREEN for c in range(8)) + + t_bce_ed1 = _run(b"abcdef\r\nghijkl\r\nmnopqr\x1b[43m\x1b[2;3H\x1b[1J") + assert t_bce_ed1.lines == ("", " jkl", "mnopqr") + assert ( + t_bce_ed1.screen._rows[0].used, + t_bce_ed1.screen._rows[0].text_used, + t_bce_ed1.screen._rows[0].wrapped, + ) == (8, 0, False) + assert all(t_bce_ed1[0][c].style.bg is AnsiColor.YELLOW for c in range(8)) + assert all(t_bce_ed1[1][c].style.bg is AnsiColor.YELLOW for c in range(3)) + assert all(t_bce_ed1[1][c].style.bg is None for c in range(3, 8)) + + # One-column geometry across ED 0, ED 1, ED 2, and phantom pending wrap. + t_c1_ed0 = _run(b"ABCD\x1b[2;1H\x1b[J", rows=3, cols=1) + assert t_c1_ed0.lines == ("B", "", "") + assert t_c1_ed0.history[-1].wrapped + assert [r.wrapped for r in t_c1_ed0.screen._rows] == [False, False, False] + + t_c1_ed1 = _run(b"ABCD\x1b[2;1H\x1b[1J", rows=3, cols=1) + assert t_c1_ed1.lines == ("", "", "D") + assert not t_c1_ed1.history[-1].wrapped + assert [r.wrapped for r in t_c1_ed1.screen._rows] == [False, False, False] + + t_c1_ed2 = _run(b"ABCD\x1b[2J", rows=3, cols=1) + assert t_c1_ed2.lines == ("", "", "") + assert not t_c1_ed2.history[-1].wrapped + assert [r.wrapped for r in t_c1_ed2.screen._rows] == [False, False, False] + + phantom_prof = Profile(screen=ScreenBehavior(pending_wrap_phantom_col=True)) + t_c1_phantom = _run(b"ABC\x1b[HB\x1b[J", rows=2, cols=1, profile=phantom_prof) + assert t_c1_phantom.lines == ("B", "") + assert t_c1_phantom.history[-1].wrapped + assert [r.wrapped for r in t_c1_phantom.screen._rows] == [False, False] + + +@pytest.mark.parametrize( + ("erase", "expected_cursor"), + [ + (b"\x1b[H\x1b[J", (0, 0)), + (b"\x1b[3;8H\x1b[1J", (2, 7)), + (b"\x1b[2J", (2, 6)), + ], + ids=["ed0", "ed1", "ed2"], +) +def test_erase_display_cow_and_held_frame_isolation( + erase: bytes, + expected_cursor: tuple[int, int], +) -> None: + """Full-display erases isolate prior snapshots and ``?2026`` held frames.""" + term = libvt.Terminal(rows=3, cols=8) + term.feed(b"abcdefghijklmnopqrstuvwxyz1234") + before_snap = term.snapshot() + committed_token = term.sync_token() + working_token = term.sync_token(view="working") + before_runs: list[list[tuple[int, int, str, Color]]] = [ + [(0, 8, "ijklmnop", None)], + [(0, 8, "qrstuvwx", None)], + [(0, 6, "yz1234", None)], + ] + erased_runs: list[list[tuple[int, int, str, Color]]] = [ + [(0, 8, " ", AnsiColor.BLUE)] + ] * 3 + + def _row_runs( + frame: libvt.Snapshot, + ) -> list[list[tuple[int, int, str, Color]]]: + return [ + [(r.start_col, r.end_col, r.text, r.style.bg) for r in frame[i].runs] + for i in range(frame.rows) + ] + + assert before_snap.lines == ("ijklmnop", "qrstuvwx", "yz1234") + assert (before_snap.cursor.row, before_snap.cursor.col) == (2, 6) + assert [before_snap[i].wrapped for i in range(before_snap.rows)] == [ + True, + True, + False, + ] + assert before_snap.history[-1].wrapped + assert _row_runs(before_snap) == before_runs + + term.feed(b"\x1b[?2026h\x1b[44m" + erase) + # Committed frame remains frozen on the pre-hold snapshot. + assert term.committed.lines == ("ijklmnop", "qrstuvwx", "yz1234") + assert (term.committed.cursor.row, term.committed.cursor.col) == (2, 6) + assert [term.committed[i].wrapped for i in range(term.committed.rows)] == [ + True, + True, + False, + ] + assert term.committed.history[-1].wrapped + assert _row_runs(term.committed) == before_runs + assert not term.changes_since(committed_token).has_changes + + # Working buffer reflects the erase, cursor, style spans, and delta immediately. + live = term.snapshot() + assert live.lines == ("", "", "") + assert (live.cursor.row, live.cursor.col) == expected_cursor + assert [live[i].wrapped for i in range(live.rows)] == [False, False, False] + assert not live.history[-1].wrapped + assert _row_runs(live) == erased_runs + working_delta = term.changes_since(working_token, view="working") + assert working_delta.has_changes + assert working_delta.resync_reason is ResyncReason.EPOCH_CHANGED + assert working_delta.dirty_rows == (0, 1, 2) + assert working_delta.snapshot.lines == ("", "", "") + assert ( + working_delta.snapshot.cursor.row, + working_delta.snapshot.cursor.col, + ) == expected_cursor + assert [(r.text, r.wrapped) for r in working_delta.new_history] == [ + ("abcdefgh", False) + ] + + term.feed(b"\x1b[?2026l") + delta = term.changes_since(committed_token) + assert delta.has_changes + assert delta.resync_reason is ResyncReason.EPOCH_CHANGED + assert delta.dirty_rows == (0, 1, 2) + assert delta.snapshot.lines == ("", "", "") + assert (delta.snapshot.cursor.row, delta.snapshot.cursor.col) == expected_cursor + assert [(r.text, r.wrapped) for r in delta.new_history] == [("abcdefgh", False)] + assert term.committed.lines == ("", "", "") + assert (term.committed.cursor.row, term.committed.cursor.col) == expected_cursor + assert [term.committed[i].wrapped for i in range(term.committed.rows)] == [ + False, + False, + False, + ] + assert not term.committed.history[-1].wrapped + assert _row_runs(term.committed) == erased_runs + + # Earlier snapshot remains untouched (CoW isolation). + assert before_snap.lines == ("ijklmnop", "qrstuvwx", "yz1234") + assert (before_snap.cursor.row, before_snap.cursor.col) == (2, 6) + assert [before_snap[i].wrapped for i in range(before_snap.rows)] == [ + True, + True, + False, + ] + assert before_snap.history[-1].wrapped + assert _row_runs(before_snap) == before_runs + + def test_reflow_keeps_blank_viewport_padding_after_a_hard_history_line() -> None: """A width change preserves existing bottom padding after a hard line break.""" term = libvt.Terminal(rows=2, cols=8)