diff --git a/Lib/_pyrepl/unix_console.py b/Lib/_pyrepl/unix_console.py index d556e6dd7c85f6..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 @@ -946,6 +949,16 @@ def __apply_line_update( self.__write(text) self.posxy = update.start_x + update.char_width, update.y + # 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, 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. self.move_cursor(0, update.y) @@ -997,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 05c716f4a75a9c..cbce14774d0f63 100644 --- a/Lib/test/test_pyrepl/test_unix_console.py +++ b/Lib/test/test_pyrepl/test_unix_console.py @@ -259,6 +259,48 @@ def test_cursor_back_write(self, _os_write): _os_write.assert_any_call(ANY, b"2") con.restore() + def test_write_to_last_column_records_pending_wrap_position(self, _os_write): + # gh-145448: after writing into the last column the terminal is in + # "pending wrap" state and the cursor stays at width - 1. + from _pyrepl.render import LineUpdate, RenderCell + con = unix_console(iter([]), width=5) + cells = tuple(RenderCell(ch, 1) for ch in "abcde") + update = LineUpdate( + kind="replace_span", y=0, start_cell=0, start_x=0, + cells=cells, char_width=5, + ) + con._UnixConsole__apply_line_update(update) + 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( 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.