Skip to content

Commit 31bc36d

Browse files
committed
gh-159658: Fix the REPL mangling input when the prompt is wider than the terminal
1 parent 2188f9b commit 31bc36d

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

‎Lib/_pyrepl/layout.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def layout_content_lines(
215215
index_to_wrap_before += 1
216216
column += char_width
217217

218-
if index_to_wrap_before == 0 and start < total:
218+
if index_to_wrap_before == 0 and not current_prompt_width:
219219
index_to_wrap_before = 1 # force progress
220220

221221
at_line_end = (start + index_to_wrap_before) >= total

‎Lib/test/test_pyrepl/test_layout.py‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,30 @@ def test_line_wraps(self):
229229

230230
def test_wrapping_forces_progress(self):
231231
# When a single character is wider than available space, force 1 char
232-
prompt = _prompt("P", 1)
233-
body = (ContentFragment("W", 1),)
232+
prompt = _prompt("", 0)
233+
body = (ContentFragment("W", 2),)
234234
source = _source("W", has_newline=False)
235235
line = ContentLine(source, prompt, body)
236-
# width=2 means prompt(1) + char(1) = 2, which fits (< width would be
237-
# false for width=2 since 1+1 >= 2), so it wraps but forces progress
236+
# width=2 means char(2) >= width, so it wraps but forces progress
238237
result = layout_content_lines((line,), 2, 0)
239238

240239
self.assertEqual(len(result.wrapped_rows), 1)
241240
self.assertEqual(result.wrapped_rows[0].buffer_advance, 1)
242241

242+
def test_prompt_wider_than_terminal(self):
243+
line = _content_line("Hello", prompt=_prompt("a" * 65, 65))
244+
result = layout_content_lines((line,), 20, 0)
245+
246+
self.assertEqual(len(result.wrapped_rows), 2)
247+
first, second = result.wrapped_rows
248+
self.assertEqual(first.prompt_width, 65)
249+
self.assertEqual(first.fragments, ())
250+
self.assertEqual(first.suffix, "\\")
251+
self.assertEqual(first.buffer_advance, 0)
252+
self.assertEqual(second.prompt_width, 0)
253+
self.assertEqual(second.buffer_advance, 6)
254+
self.assertEqual(result.layout_map.pos_to_xy(0), (0, 1))
255+
243256
def test_layout_map_matches_wrapped_rows(self):
244257
line = _content_line("abc")
245258
result = layout_content_lines((line,), 80, 0)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix the :term:`REPL` mangling typed input when :data:`sys.ps1` is wider than
2+
the terminal.

0 commit comments

Comments
 (0)