Skip to content

Commit b480959

Browse files
committed
Split wide prompts into terminal rows
1 parent 93e3c4f commit b480959

4 files changed

Lines changed: 114 additions & 16 deletions

File tree

‎Lib/_pyrepl/commands.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,12 @@ def do(self) -> None:
303303
r.debug("end of buffer")
304304
return
305305

306+
# Skip prompt-only rows, they hold no buffer positions
307+
row = r.layout.rows[new_y]
308+
while new_y < r.layout.max_row() and not row.char_widths and not row.buffer_advance:
309+
new_y += 1
310+
row = r.layout.rows[new_y]
311+
306312
if (
307313
x
308314
> (

‎Lib/_pyrepl/layout.py‎

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Self
55

66
from .content import ContentFragment, ContentLine
7+
from .utils import ANSI_ESCAPE_SEQUENCE, wlen
78

89
lazy from .types import CursorXY, ScreenInfoRow
910

@@ -157,6 +158,35 @@ def wrapped_row_end(
157158
return end if end > start else start + 1
158159

159160

161+
def split_prompt(text: str, width: int) -> list[tuple[str, int]]:
162+
"""Split *text* into rows of at most *width* visible columns.
163+
164+
Every row but the last is a full terminal row; the last one holds the
165+
rest of the prompt and may be empty.
166+
"""
167+
rows: list[tuple[str, int]] = []
168+
row_start = 0
169+
column = 0
170+
index = 0
171+
while index < len(text):
172+
if match := ANSI_ESCAPE_SEQUENCE.match(text, index):
173+
index = match.end()
174+
continue
175+
char_width = wlen(text[index])
176+
if column + char_width > width:
177+
rows.append((text[row_start:index], column))
178+
row_start = index
179+
column = 0
180+
column += char_width
181+
index += 1
182+
if column == width:
183+
rows.append((text[row_start:index], column))
184+
row_start = index
185+
column = 0
186+
rows.append((text[row_start:], column))
187+
return rows
188+
189+
160190
def layout_content_lines(
161191
lines: tuple[ContentLine, ...],
162192
width: int,
@@ -192,6 +222,17 @@ def layout_content_lines(
192222

193223
prompt_text = line.prompt.text
194224
prompt_width = line.prompt.width
225+
if prompt_width >= width:
226+
*prompt_rows, (prompt_text, prompt_width) = split_prompt(
227+
prompt_text, width
228+
)
229+
for row_text, row_width in prompt_rows:
230+
line_end_offsets.append(offset)
231+
wrapped_rows.append(
232+
WrappedRow(prompt_text=row_text, prompt_width=row_width)
233+
)
234+
layout_rows.append(LayoutRow(row_width, ()))
235+
195236
body = tuple(line.body)
196237
body_widths = tuple(fragment.width for fragment in body)
197238

@@ -223,12 +264,23 @@ def layout_content_lines(
223264
start = 0
224265
total = len(body)
225266
while True:
267+
available_width = width - current_prompt_width - 1
268+
if current_prompt_width and body_widths[start] > available_width:
269+
# No room left after the prompt, start the body on a new row.
270+
line_end_offsets.append(offset)
271+
wrapped_rows.append(
272+
WrappedRow(
273+
prompt_text=current_prompt,
274+
prompt_width=current_prompt_width,
275+
)
276+
)
277+
layout_rows.append(LayoutRow(current_prompt_width, ()))
278+
current_prompt = ""
279+
current_prompt_width = 0
280+
continue
281+
226282
# Find how many characters fit on this row.
227-
end = wrapped_row_end(
228-
body_widths,
229-
start,
230-
width - current_prompt_width - 1,
231-
)
283+
end = wrapped_row_end(body_widths, start, available_width)
232284
index_to_wrap_before = end - start
233285

234286
at_line_end = (start + index_to_wrap_before) >= total

‎Lib/test/test_pyrepl/test_layout.py‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,35 @@ def test_prompt_wider_than_terminal(self):
243243
line = _content_line("Hello", prompt=_prompt("a" * 65, 65))
244244
result = layout_content_lines((line,), 20, 0)
245245

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))
246+
rows = result.wrapped_rows
247+
self.assertEqual(
248+
[row.prompt_width for row in rows], [20, 20, 20, 5]
249+
)
250+
self.assertEqual([row.suffix for row in rows], ["", "", "", ""])
251+
self.assertEqual(
252+
[row.buffer_advance for row in rows], [0, 0, 0, 6]
253+
)
254+
self.assertTrue(all(row.width <= 20 for row in result.layout_map.rows))
255+
self.assertEqual(result.layout_map.pos_to_xy(0), (5, 3))
256+
self.assertEqual(result.layout_map.pos_to_xy(5), (10, 3))
257+
258+
def test_prompt_wider_than_terminal_empty_body(self):
259+
line = _content_line("", prompt=_prompt("a" * 40, 40))
260+
result = layout_content_lines((line,), 20, 0)
261+
262+
self.assertEqual(
263+
[row.prompt_width for row in result.wrapped_rows], [20, 20, 0]
264+
)
265+
self.assertEqual(result.layout_map.pos_to_xy(0), (0, 2))
266+
267+
def test_prompt_filling_last_row(self):
268+
line = _content_line("Hello", prompt=_prompt("a" * 39, 39))
269+
result = layout_content_lines((line,), 20, 0)
270+
271+
rows = result.wrapped_rows
272+
self.assertEqual([row.prompt_width for row in rows], [20, 19, 0])
273+
self.assertEqual([row.suffix for row in rows], ["", "", ""])
274+
self.assertEqual(result.layout_map.pos_to_xy(0), (0, 2))
255275

256276
def test_layout_map_matches_wrapped_rows(self):
257277
line = _content_line("abc")

‎Lib/test/test_pyrepl/test_reader.py‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,29 @@ def test_up_arrow_with_prompt_wider_than_terminal(self):
274274
prepare_reader, get_prompt=lambda lineno, cursor_on_line: "a" * 30
275275
),
276276
)
277-
# the prompt-only row above "abc" is skipped, landing on "x"
277+
# the prompt rows above "abc" are skipped, landing on "x"
278278
self.assertEqual(reader.pos, 1)
279-
self.assertEqual(reader.cxy, (1, 1))
279+
self.assertEqual(reader.cxy, (11, 1))
280+
281+
def test_down_arrow_with_prompt_wider_than_terminal(self):
282+
events = itertools.chain(
283+
code_to_events("xy\nabc"),
284+
[
285+
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
286+
Event(evt="key", data="left", raw=bytearray(b"\x1bOD")),
287+
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")),
288+
],
289+
)
290+
reader, _ = handle_all_events(
291+
events,
292+
prepare_console=functools.partial(prepare_console, width=20),
293+
prepare_reader=functools.partial(
294+
prepare_reader, get_prompt=lambda lineno, cursor_on_line: "a" * 30
295+
),
296+
)
297+
# the prompt rows above "abc" are skipped, keeping the column
298+
self.assertEqual(reader.pos, 4)
299+
self.assertEqual(reader.cxy, (11, 3))
280300

281301
def test_newline_within_block_trailing_whitespace(self):
282302
# fmt: off

0 commit comments

Comments
 (0)