Skip to content

Commit 26471c2

Browse files
committed
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.
1 parent 7774f23 commit 26471c2

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎Lib/_pyrepl/unix_console.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ class UnixConsole(Console):
186186
__gone_tall: bool
187187
__move: _MoveFunc
188188
__offset: int
189+
__pending_wrap: bool
189190

190191
def __init__(
191192
self,
@@ -496,6 +497,7 @@ def prepare(self) -> None:
496497
self.height, self.width = self.getheightwidth()
497498

498499
self.posxy = 0, 0
500+
self.__pending_wrap = False
499501
self.__gone_tall = False
500502
self.__move = self.__move_short
501503
self.__offset = 0
@@ -720,6 +722,7 @@ def clear(self):
720722
self.__gone_tall = True
721723
self.__move = self.__move_tall
722724
self.posxy = 0, 0
725+
self.__pending_wrap = False
723726
self.sync_rendered_screen(RenderedScreen.empty(), self.posxy)
724727

725728
@property
@@ -949,10 +952,12 @@ def __apply_line_update(
949952
# When a write reaches the last column, the terminal enters the
950953
# "pending wrap" state and the cursor physically stays at width - 1.
951954
# Record that position so later relative moves (CUB) don't overshoot
952-
# by one cell (gh-145448).
955+
# by one cell, and remember the pending wrap so the next move clears
956+
# it before anything else is written (gh-145448).
953957
x, y = self.posxy
954958
if x >= self.width:
955959
self.posxy = self.width - 1, y
960+
self.__pending_wrap = True
956961

957962
if update.reset_to_margin:
958963
# Non-SGR terminal controls can affect the cursor position.
@@ -1005,12 +1010,21 @@ def __move_x_cub_cuf(self, x: int) -> None:
10051010
self.__write_code(self._cub, -dx)
10061011

10071012
def __move_short(self, x, y):
1013+
if self.__pending_wrap:
1014+
# A relative move may emit nothing when the target is the
1015+
# current cell, and the next write would then wrap to the
1016+
# following row. A carriage return always clears the pending
1017+
# wrap state, so start the move from column 0.
1018+
self.__write("\r")
1019+
self.posxy = 0, self.posxy[1]
1020+
self.__pending_wrap = False
10081021
self.__move_x(x)
10091022
self.__move_y(y)
10101023

10111024
def __move_tall(self, x, y):
10121025
assert 0 <= y - self.__offset < self.height, y - self.__offset
10131026
self.__write_code(self._cup, y - self.__offset, x)
1027+
self.__pending_wrap = False
10141028

10151029
def __sigwinch(self, signum, frame):
10161030
self.event_queue.insert(Event("resize", ""))

‎Lib/test/test_pyrepl/test_unix_console.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,34 @@ def test_write_to_last_column_records_pending_wrap_position(self, _os_write):
273273
self.assertEqual(con.posxy, (4, 0))
274274
con.restore()
275275

276+
def test_write_after_pending_wrap_stays_on_row(self, _os_write):
277+
# gh-145448: updating "abcde" to "abcdf" in a five-column terminal
278+
# rewrites the last column while the terminal is in pending wrap
279+
# state. The cursor must be moved explicitly first, otherwise the
280+
# "f" would be written on the next row.
281+
from _pyrepl.render import LineUpdate, RenderCell
282+
con = unix_console(iter([]), width=5)
283+
cells = tuple(RenderCell(ch, 1) for ch in "abcde")
284+
con._UnixConsole__apply_line_update(LineUpdate(
285+
kind="replace_span", y=0, start_cell=0, start_x=0,
286+
cells=cells, char_width=5,
287+
))
288+
buffer = con._UnixConsole__buffer
289+
start = len(buffer)
290+
con._UnixConsole__apply_line_update(LineUpdate(
291+
kind="replace_char", y=0, start_cell=4, start_x=4,
292+
cells=(RenderCell("f", 1),), char_width=1,
293+
))
294+
written = b"".join(
295+
text if is_code else text.encode()
296+
for text, is_code in buffer[start:]
297+
)
298+
self.assertEqual(
299+
written, b"\r" + TERM_CAPABILITIES["cuf"] + b":4" + b"f"
300+
)
301+
self.assertEqual(con.posxy, (4, 0))
302+
con.restore()
303+
276304
def test_insert_char_only_for_midline_insert(self, _os_write):
277305
insert_char = b"<insert-char>"
278306
events = itertools.chain(

0 commit comments

Comments
 (0)