Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Lib/_pyrepl/unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class UnixConsole(Console):
__gone_tall: bool
__move: _MoveFunc
__offset: int
__pending_wrap: bool

def __init__(
self,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This fixes the backward move. In a five-column terminal, an update from abcde to abcdf can write the f on the next row. Can we track pending wrap for that write too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, fixed in 26471c2: UnixConsole now tracks pending wrap after a write fills the last column, and the next short move emits \r first so abcde to abcdf rewrites the f on the same row. I added test_write_after_pending_wrap_stays_on_row for that case, and sh -c "./python.exe -m test test_pyrepl" exited 0 on that commit.

self.__pending_wrap = True

if update.reset_to_margin:
# Non-SGR terminal controls can affect the cursor position.
self.move_cursor(0, update.y)
Expand Down Expand Up @@ -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", ""))
Expand Down
42 changes: 42 additions & 0 deletions Lib/test/test_pyrepl/test_unix_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<insert-char>"
events = itertools.chain(
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading