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
12 changes: 12 additions & 0 deletions Lib/_pyrepl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ def do(self) -> None:
r.debug("start of buffer")
return

# Skip prompt-only rows, they hold no buffer positions
row = r.layout.rows[new_y]
while new_y > 0 and not row.char_widths and not row.buffer_advance:
new_y -= 1
row = r.layout.rows[new_y]

if (
x
> (
Expand Down Expand Up @@ -297,6 +303,12 @@ def do(self) -> None:
r.debug("end of buffer")
return

# Skip prompt-only rows, they hold no buffer positions
row = r.layout.rows[new_y]
while new_y < r.layout.max_row() and not row.char_widths and not row.buffer_advance:
new_y += 1
row = r.layout.rows[new_y]

if (
x
> (
Expand Down
62 changes: 57 additions & 5 deletions Lib/_pyrepl/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Self

from .content import ContentFragment, ContentLine
from .utils import ANSI_ESCAPE_SEQUENCE, wlen

lazy from .types import CursorXY, ScreenInfoRow

Expand Down Expand Up @@ -157,6 +158,35 @@ def wrapped_row_end(
return end if end > start else start + 1


def split_prompt(text: str, width: int) -> list[tuple[str, int]]:
"""Split *text* into rows of at most *width* visible columns.

Every row but the last is a full terminal row; the last one holds the
rest of the prompt and may be empty.
"""
rows: list[tuple[str, int]] = []
row_start = 0
column = 0
index = 0
while index < len(text):
if match := ANSI_ESCAPE_SEQUENCE.match(text, index):
index = match.end()
continue
char_width = wlen(text[index])
if column + char_width > width:
rows.append((text[row_start:index], column))
row_start = index
column = 0
column += char_width
index += 1
if column == width:
rows.append((text[row_start:index], column))
row_start = index
column = 0
rows.append((text[row_start:], column))
return rows


def layout_content_lines(
lines: tuple[ContentLine, ...],
width: int,
Expand Down Expand Up @@ -192,6 +222,17 @@ def layout_content_lines(

prompt_text = line.prompt.text
prompt_width = line.prompt.width
if prompt_width >= width:
*prompt_rows, (prompt_text, prompt_width) = split_prompt(
prompt_text, width
)
for row_text, row_width in prompt_rows:
line_end_offsets.append(offset)
wrapped_rows.append(
WrappedRow(prompt_text=row_text, prompt_width=row_width)
)
layout_rows.append(LayoutRow(row_width, ()))

body = tuple(line.body)
body_widths = tuple(fragment.width for fragment in body)

Expand Down Expand Up @@ -223,12 +264,23 @@ def layout_content_lines(
start = 0
total = len(body)
while True:
available_width = width - current_prompt_width - 1
if current_prompt_width and body_widths[start] > available_width:
# No room left after the prompt, start the body on a new row.
line_end_offsets.append(offset)
wrapped_rows.append(
WrappedRow(
prompt_text=current_prompt,
prompt_width=current_prompt_width,
)
)
layout_rows.append(LayoutRow(current_prompt_width, ()))
current_prompt = ""
current_prompt_width = 0
continue

# Find how many characters fit on this row.
end = wrapped_row_end(
body_widths,
start,
width - current_prompt_width - 1,
)
end = wrapped_row_end(body_widths, start, available_width)
index_to_wrap_before = end - start

at_line_end = (start + index_to_wrap_before) >= total
Expand Down
41 changes: 37 additions & 4 deletions Lib/test/test_pyrepl/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,50 @@ def test_line_wraps(self):

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

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

def test_prompt_wider_than_terminal(self):
line = _content_line("Hello", prompt=_prompt("a" * 65, 65))
result = layout_content_lines((line,), 20, 0)

rows = result.wrapped_rows
self.assertEqual(
[row.prompt_width for row in rows], [20, 20, 20, 5]
)
self.assertEqual([row.suffix for row in rows], ["", "", "", ""])
self.assertEqual(
[row.buffer_advance for row in rows], [0, 0, 0, 6]
)
self.assertTrue(all(row.width <= 20 for row in result.layout_map.rows))
self.assertEqual(result.layout_map.pos_to_xy(0), (5, 3))
self.assertEqual(result.layout_map.pos_to_xy(5), (10, 3))

def test_prompt_wider_than_terminal_empty_body(self):
line = _content_line("", prompt=_prompt("a" * 40, 40))
result = layout_content_lines((line,), 20, 0)

self.assertEqual(
[row.prompt_width for row in result.wrapped_rows], [20, 20, 0]
)
self.assertEqual(result.layout_map.pos_to_xy(0), (0, 2))

def test_prompt_filling_last_row(self):
line = _content_line("Hello", prompt=_prompt("a" * 39, 39))
result = layout_content_lines((line,), 20, 0)

rows = result.wrapped_rows
self.assertEqual([row.prompt_width for row in rows], [20, 19, 0])
self.assertEqual([row.suffix for row in rows], ["", "", ""])
self.assertEqual(result.layout_map.pos_to_xy(0), (0, 2))

def test_layout_map_matches_wrapped_rows(self):
line = _content_line("abc")
result = layout_content_lines((line,), 80, 0)
Expand Down
36 changes: 36 additions & 0 deletions Lib/test/test_pyrepl/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,42 @@ def test_up_arrow_after_ctrl_r(self):
reader, _ = handle_all_events(events)
self.assertIn(reader.screen, ([], [""]))

def test_up_arrow_with_prompt_wider_than_terminal(self):
events = itertools.chain(
code_to_events("x\nabc"),
[Event(evt="key", data="up", raw=bytearray(b"\x1bOA"))],
)
reader, _ = handle_all_events(
events,
prepare_console=functools.partial(prepare_console, width=20),
prepare_reader=functools.partial(
prepare_reader, get_prompt=lambda lineno, cursor_on_line: "a" * 30
),
)
# the prompt rows above "abc" are skipped, landing on "x"
self.assertEqual(reader.pos, 1)
self.assertEqual(reader.cxy, (11, 1))

def test_down_arrow_with_prompt_wider_than_terminal(self):
events = itertools.chain(
code_to_events("xy\nabc"),
[
Event(evt="key", data="up", raw=bytearray(b"\x1bOA")),
Event(evt="key", data="left", raw=bytearray(b"\x1bOD")),
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")),
],
)
reader, _ = handle_all_events(
events,
prepare_console=functools.partial(prepare_console, width=20),
prepare_reader=functools.partial(
prepare_reader, get_prompt=lambda lineno, cursor_on_line: "a" * 30
),
)
# the prompt rows above "abc" are skipped, keeping the column
self.assertEqual(reader.pos, 4)
self.assertEqual(reader.cxy, (11, 3))

def test_newline_within_block_trailing_whitespace(self):
# fmt: off
code = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix the :term:`REPL` mangling typed input when :data:`sys.ps1` is wider than
the terminal.
Loading