From c4bfbe18347c92b7c10237b7a77e5358506e3330 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:14:28 -0600 Subject: [PATCH 1/2] terminal(fix[rep]): Keep Ghostty REP history why: Ghostty repeats its last spacing character after cursor movement. what: - Keep repeat history through controls for the Ghostty profile. - Retain hard-reset clearing and spacing-character semantics. - Exercise intervening motion, style, OSC and DCS. Fixes #104 --- src/libvt/config.py | 4 ++++ src/libvt/profiles.py | 1 + src/libvt/terminal.py | 33 ++++++++++++++++++----------- tests/test_terminal.py | 48 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 12 deletions(-) diff --git a/src/libvt/config.py b/src/libvt/config.py index 50c5603..259a01a 100644 --- a/src/libvt/config.py +++ b/src/libvt/config.py @@ -205,6 +205,9 @@ class ScreenBehavior: rep_wraps : bool When ``True``, ``REP`` (``CSI Ps b``) autowraps across the right margin; when ``False``, ``REP`` halts at phantom column ``cols``. + rep_uses_last_graphic : bool + Repeat the last spacing character across intervening controls, until a + hard reset. When false, an intervening control clears the REP source. default_g1_charset : Charset Initial ``G1`` character-set designation on startup and ``RIS``. decstr_enabled : bool @@ -261,6 +264,7 @@ class ScreenBehavior: hpr_vpr_enabled: bool = True il_dl_resets_col: bool = True rep_wraps: bool = True + rep_uses_last_graphic: bool = False default_g1_charset: Charset = "B" decstr_enabled: bool = True declrmm_enabled: bool = True diff --git a/src/libvt/profiles.py b/src/libvt/profiles.py index 8e01572..7668cec 100644 --- a/src/libvt/profiles.py +++ b/src/libvt/profiles.py @@ -55,6 +55,7 @@ queries=QueryBehavior( xtsmgraphics_enabled=False, ), + screen=ScreenBehavior(rep_uses_last_graphic=True), ) #: ``Kitty`` host terminal profile (CSI u keyboard stack, XTGETTCAP, reflow). diff --git a/src/libvt/terminal.py b/src/libvt/terminal.py index cdb7c22..a96d7c1 100644 --- a/src/libvt/terminal.py +++ b/src/libvt/terminal.py @@ -3226,6 +3226,10 @@ def _emit_reply(self, payload: bytes) -> None: if len(self._replies) + len(payload) <= self._limits.max_reply_bytes: self._replies.extend(payload) + def _interrupt_rep(self) -> None: + if not self._profile.screen.rep_uses_last_graphic: + self._last_printed = "" + def _clear_cluster_anchor(self) -> None: self._last_printed_pos = None self._cluster_state = CLUSTER_NONE @@ -3353,7 +3357,8 @@ def reset(self, *, soft: bool = False) -> None: self._protected = False self._hyperlink = None self._sync_pen_cache() - self._last_printed = "" + if not soft or not self._profile.screen.rep_uses_last_graphic: + self._last_printed = "" self._clear_cluster_anchor() def_g: tuple[Charset, Charset, Charset, Charset] = ( "B", @@ -3498,7 +3503,7 @@ def reset(self, *, soft: bool = False) -> None: def _on_screen_mutate(self, structural: bool) -> None: self._before_mutate() - self._last_printed = "" + self._interrupt_rep() self._last_printed_pos = None self._cluster_state = CLUSTER_NONE self._cluster_len = 0 @@ -4186,7 +4191,8 @@ def _handle_print(self, text: str) -> None: ) self._cluster_state = next_state self._cluster_len = prev_cluster_len + 1 - self._last_printed = combined + if not self._profile.screen.rep_uses_last_graphic: + self._last_printed = combined self._last_printed_pos = (nr, eff_left) if eff_left + 1 >= eff_right: self._set_cursor( @@ -4252,7 +4258,8 @@ def _handle_print(self, text: str) -> None: if pc == 0 and w_override == 1: scr.heal_wrapped_spacer_head(pr) self._cluster_len += 1 - self._last_printed = row_obj.char_at(pc) + if not self._profile.screen.rep_uses_last_graphic: + self._last_printed = row_obj.char_at(pc) if ( w_override == 2 and cur_role == int(WidthRole.NARROW) @@ -4433,7 +4440,9 @@ def _handle_print(self, text: str) -> None: policy=sixel_pol, ) - self._last_printed = ch_full + self._last_printed = ( + ch if self._profile.screen.rep_uses_last_graphic else ch_full + ) self._last_printed_pos = (c_row, placed_col) self._cluster_state = next_state self._cluster_len = len(ch_full) @@ -4476,7 +4485,7 @@ def _cursor_left(self, scr: Screen, count: int, *, is_backspace: bool) -> None: ) def _handle_execute(self, byte: int) -> None: - self._last_printed = "" + self._interrupt_rep() scr = self._screens.active _, _, left, right = self._effective_margins(scr) if byte == 0x07: # BEL @@ -4570,7 +4579,7 @@ def _restore_cursor(self) -> None: self._apply_saved_cursor(saved) def _handle_esc(self, intermediates: bytes, final: int) -> None: - self._last_printed = "" + self._interrupt_rep() scr = self._screens.active if not intermediates: if final == 0x37: # ESC 7 DECSC @@ -5402,7 +5411,7 @@ def _handle_csi( final: int, ) -> None: prev_printed = self._last_printed - self._last_printed = "" + self._interrupt_rep() if params.colon_mask != 0 and not ( prefix == 0 and not intermediates and final == ord("m") ): @@ -6095,7 +6104,7 @@ def _handle_csi( self._unhandled("csi", f"CSI {ch_final}") def _handle_window_rename(self, raw: bytes) -> None: - self._last_printed = "" + self._interrupt_rep() try: name = raw.decode("utf-8", errors="strict") except UnicodeDecodeError: @@ -6536,7 +6545,7 @@ def _handle_osc_99(self, data: bytes, out_term: bytes) -> None: ) def _handle_osc(self, code: int, data: bytes, terminator: bytes) -> None: - self._last_printed = "" + self._interrupt_rep() out_term = b"\x07" if terminator == b"\x07" else b"\x1b\\" if code == 0: @@ -6929,7 +6938,7 @@ def _handle_dcs( final: int, data: bytes, ) -> None: - self._last_printed = "" + self._interrupt_rep() # DECRQSS: DCS $ q ST if prefix == 0 and intermediates == b"$" and final == ord("q"): if self._profile.queries.decrqss_enabled: @@ -7044,7 +7053,7 @@ def _handle_dcs( self._unhandled("dcs", f"DCS {inter_s}{chr(final)}") def _handle_apc(self, data: bytes) -> None: - self._last_printed = "" + self._interrupt_rep() if data.startswith(b"G"): scr = self._screens.active orig_row, orig_col = scr._cur_row, scr._cur_col diff --git a/tests/test_terminal.py b/tests/test_terminal.py index 5e00cbf..0438929 100644 --- a/tests/test_terminal.py +++ b/tests/test_terminal.py @@ -1173,3 +1173,51 @@ def test_committed_appearance_sync_hold_set_theme_2031_and_decsnm() -> None: term.feed(b"\x1bc") assert term.modes.is_set(Mode.DECSCNM) is False assert term.appearance.scheme == ColorScheme.LIGHT + + +@pytest.mark.parametrize( + "control", + [ + b"\x1b[2;2H", + b"\x1b[31m\x1b[2;2H", + b"\r\x1b[2;2H", + b"\x1b]2;title\x07\x1b[2;2H", + b"\x1bPq\x1b\\\x1b[2;2H", + ], +) +@pytest.mark.parametrize( + ("text", "repeated"), [("abc", "cc"), ("界", "界界"), ("e\u0301", "ee")] +) +def test_rep_keeps_the_last_ghostty_graphic( + control: bytes, text: str, repeated: str +) -> None: + """Ghostty repeats its last spacing character at the new cursor.""" + for profile in (GHOSTTY_PROFILE, XTERM_PROFILE): + for split in (False, True): + terminal = libvt.Terminal(rows=3, cols=8, profile=profile) + before = terminal.committed + data = text.encode() + control + b"\x1b[2b" + for part in [bytes((b,)) for b in data] if split else [data]: + terminal.feed(part) + snap = terminal.committed + ghostty = profile == GHOSTTY_PROFILE + assert snap.lines == (text, " " + repeated if ghostty else "", "") + width = (4 if text == "界" else 2) if ghostty else 0 + assert (snap.cursor.row, snap.cursor.col, snap[1].wrapped) == ( + 1, + 1 + width, + False, + ) + if ghostty and b"31m" in control: + assert [snap[1, col].style.fg for col in range(1, 1 + width)] == [ + 1 + ] * width + assert before.lines == ("", "", "") + + +def test_rep_ghostty_soft_and_hard_reset() -> None: + """A hard reset clears retained REP history. A soft reset keeps it.""" + for reset, expected in ((b"\x1b[!p", "abbb"), (b"\x1bc", "")): + terminal = libvt.Terminal(rows=2, cols=8, profile=GHOSTTY_PROFILE) + terminal.feed(b"ab" + reset + b"\x1b[2b") + assert terminal.lines[0] == expected From 524a396b956d5dffaf95dd6be1a71e298590dacd Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:51:44 -0600 Subject: [PATCH 2/2] CHANGES(docs): Record cursor motion rep why: Readers need the behavior and scope of this change at release. what: - Add the unreleased entry for PR #185 - Preserve previously published notes and the parent branch's entries --- CHANGES | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGES b/CHANGES index ca1c339..c87b1d4 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,13 @@ _Notes on the upcoming release will go here._ +### Fixes + +#### Repeat the last printed character on the Ghostty profile (#185) + +After a cursor movement or style change, `GHOSTTY_PROFILE` repeats the +last spacing character at the new cursor position. A hard reset clears it. + ## libvt 0.0.1a5 (2026-11-30) `libvt` 0.0.1a5 adds rectangular grid blitting, display-column text writes, and