From 539707c1801d33408595e1be3d3eb9b003097777 Mon Sep 17 00:00:00 2001 From: Matt Van Horn Date: Mon, 9 Mar 2026 23:06:26 -0700 Subject: [PATCH 1/2] gh-145448: Fix REPL tab completion cursor position in pending wrap state When __write_changed_line writes to the last column of the terminal, the cursor enters "pending wrap" state where it physically stays at width-1. However, posxy was recording width, causing subsequent CUB (Cursor Backward) calculations to overshoot by one cell. Cap posxy x-coordinate at width-1 to match the physical cursor position in pending wrap state. This fixes the cursor moving backward during tab completion in xterm and Ghostty. Co-Authored-By: Claude Opus 4.6 --- Lib/_pyrepl/unix_console.py | 15 ++++++++++++--- ...03-09-00-00-02.gh-issue-145448.repl-cursor.rst | 3 +++ 2 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-03-09-00-00-02.gh-issue-145448.repl-cursor.rst diff --git a/Lib/_pyrepl/unix_console.py b/Lib/_pyrepl/unix_console.py index 937b5df6ff7d4c..5817075ea7bda6 100644 --- a/Lib/_pyrepl/unix_console.py +++ b/Lib/_pyrepl/unix_console.py @@ -675,7 +675,8 @@ def __write_changed_line(self, y, oldline, newline, px_coord): self.__move(x_coord, y) self.__write_code(self.ich1) self.__write(newline[x_pos]) - self.posxy = x_coord + character_width, y + new_x = x_coord + character_width + self.posxy = min(new_x, self.width - 1), y # if it's a single character change in the middle of the line elif ( @@ -686,7 +687,8 @@ def __write_changed_line(self, y, oldline, newline, px_coord): character_width = wlen(newline[x_pos]) self.__move(x_coord, y) self.__write(newline[x_pos]) - self.posxy = x_coord + character_width, y + new_x = x_coord + character_width + self.posxy = min(new_x, self.width - 1), y # if this is the last character to fit in the line and we edit in the middle of the line elif ( @@ -713,7 +715,14 @@ def __write_changed_line(self, y, oldline, newline, px_coord): if wlen(oldline) > wlen(newline): self.__write_code(self._el) self.__write(newline[x_pos:]) - self.posxy = wlen(newline), y + # When writing reaches the last column, the terminal enters + # "pending wrap" state where the cursor physically stays at + # width-1. Record position accordingly so CUB calculations + # use the correct physical cursor position (gh-145448). + newline_width = wlen(newline) + if newline_width >= self.width: + newline_width = self.width - 1 + self.posxy = newline_width, y if "\x1b" in newline: # ANSI escape characters are present, so we can't assume diff --git a/Misc/NEWS.d/next/Library/2026-03-09-00-00-02.gh-issue-145448.repl-cursor.rst b/Misc/NEWS.d/next/Library/2026-03-09-00-00-02.gh-issue-145448.repl-cursor.rst new file mode 100644 index 00000000000000..2387b886de59c9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-03-09-00-00-02.gh-issue-145448.repl-cursor.rst @@ -0,0 +1,3 @@ +Fixed REPL tab completion cursor moving backward by one cell in terminals +that correctly handle CUB (Cursor Backward) in pending wrap state, such as +xterm and Ghostty. From 26471c2f03e8b01ec1e40e9d9f9c95bb677fbe35 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:37:13 -0800 Subject: [PATCH 2/2] gh-145448: Clear pending wrap before rewriting the last column After a write fills the last column the terminal is in pending wrap state. If the next update targets that same cell, the relative move emits nothing and the write lands on the next row. Track the pending wrap and emit a carriage return before the next relative move. --- Lib/_pyrepl/unix_console.py | 16 ++++++++++++- Lib/test/test_pyrepl/test_unix_console.py | 28 +++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Lib/_pyrepl/unix_console.py b/Lib/_pyrepl/unix_console.py index dd47b793283ac6..841295aad3a7eb 100644 --- a/Lib/_pyrepl/unix_console.py +++ b/Lib/_pyrepl/unix_console.py @@ -186,6 +186,7 @@ class UnixConsole(Console): __gone_tall: bool __move: _MoveFunc __offset: int + __pending_wrap: bool def __init__( self, @@ -496,6 +497,7 @@ def prepare(self) -> None: self.height, self.width = self.getheightwidth() self.posxy = 0, 0 + self.__pending_wrap = False self.__gone_tall = False self.__move = self.__move_short self.__offset = 0 @@ -720,6 +722,7 @@ def clear(self): self.__gone_tall = True self.__move = self.__move_tall self.posxy = 0, 0 + self.__pending_wrap = False self.sync_rendered_screen(RenderedScreen.empty(), self.posxy) @property @@ -949,10 +952,12 @@ def __apply_line_update( # When a write reaches the last column, the terminal enters the # "pending wrap" state and the cursor physically stays at width - 1. # Record that position so later relative moves (CUB) don't overshoot - # by one cell (gh-145448). + # by one cell, and remember the pending wrap so the next move clears + # it before anything else is written (gh-145448). x, y = self.posxy if x >= self.width: self.posxy = self.width - 1, y + self.__pending_wrap = True if update.reset_to_margin: # Non-SGR terminal controls can affect the cursor position. @@ -1005,12 +1010,21 @@ def __move_x_cub_cuf(self, x: int) -> None: self.__write_code(self._cub, -dx) def __move_short(self, x, y): + if self.__pending_wrap: + # A relative move may emit nothing when the target is the + # current cell, and the next write would then wrap to the + # following row. A carriage return always clears the pending + # wrap state, so start the move from column 0. + self.__write("\r") + self.posxy = 0, self.posxy[1] + self.__pending_wrap = False self.__move_x(x) self.__move_y(y) def __move_tall(self, x, y): assert 0 <= y - self.__offset < self.height, y - self.__offset self.__write_code(self._cup, y - self.__offset, x) + self.__pending_wrap = False def __sigwinch(self, signum, frame): self.event_queue.insert(Event("resize", "")) diff --git a/Lib/test/test_pyrepl/test_unix_console.py b/Lib/test/test_pyrepl/test_unix_console.py index ac8906d113f736..cbce14774d0f63 100644 --- a/Lib/test/test_pyrepl/test_unix_console.py +++ b/Lib/test/test_pyrepl/test_unix_console.py @@ -273,6 +273,34 @@ def test_write_to_last_column_records_pending_wrap_position(self, _os_write): self.assertEqual(con.posxy, (4, 0)) con.restore() + def test_write_after_pending_wrap_stays_on_row(self, _os_write): + # gh-145448: updating "abcde" to "abcdf" in a five-column terminal + # rewrites the last column while the terminal is in pending wrap + # state. The cursor must be moved explicitly first, otherwise the + # "f" would be written on the next row. + from _pyrepl.render import LineUpdate, RenderCell + con = unix_console(iter([]), width=5) + cells = tuple(RenderCell(ch, 1) for ch in "abcde") + con._UnixConsole__apply_line_update(LineUpdate( + kind="replace_span", y=0, start_cell=0, start_x=0, + cells=cells, char_width=5, + )) + buffer = con._UnixConsole__buffer + start = len(buffer) + con._UnixConsole__apply_line_update(LineUpdate( + kind="replace_char", y=0, start_cell=4, start_x=4, + cells=(RenderCell("f", 1),), char_width=1, + )) + written = b"".join( + text if is_code else text.encode() + for text, is_code in buffer[start:] + ) + self.assertEqual( + written, b"\r" + TERM_CAPABILITIES["cuf"] + b":4" + b"f" + ) + self.assertEqual(con.posxy, (4, 0)) + con.restore() + def test_insert_char_only_for_midline_insert(self, _os_write): insert_char = b"" events = itertools.chain(