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: 6 additions & 6 deletions src/prompt_toolkit/layout/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2349,25 +2349,25 @@ def get_line_height(lineno: int) -> int:
# scroll to this line.
line_height = get_line_height(ui_content.cursor_position.y)
if line_height > height - scroll_offsets_top:
# Calculate the height of the text before the cursor (including
# line prefixes).
text_before_height = ui_content.get_height_for_line(
# Include the character under the cursor, which can wrap onto
# the next line even when all preceding text fits.
cursor_height = ui_content.get_height_for_line(
ui_content.cursor_position.y,
width,
self.get_line_prefix,
slice_stop=ui_content.cursor_position.x,
slice_stop=ui_content.cursor_position.x + 1,
)

# Adjust scroll offset.
self.vertical_scroll = ui_content.cursor_position.y
self.vertical_scroll_2 = min(
text_before_height - 1, # Keep the cursor visible.
cursor_height - 1, # Keep the cursor visible.
line_height
- height, # Avoid blank lines at the bottom when scrolling up again.
self.vertical_scroll_2,
)
self.vertical_scroll_2 = max(
0, text_before_height - height, self.vertical_scroll_2
0, cursor_height - height, self.vertical_scroll_2
)
return
else:
Expand Down
75 changes: 75 additions & 0 deletions tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import pytest

from prompt_toolkit import Application
from prompt_toolkit.data_structures import Point, Size
from prompt_toolkit.input.defaults import create_pipe_input
from prompt_toolkit.layout import InvalidLayoutError, Layout
from prompt_toolkit.layout.containers import HSplit, VSplit, Window
from prompt_toolkit.layout.controls import BufferControl
from prompt_toolkit.output import DummyOutput
from prompt_toolkit.utils import get_cwidth
from prompt_toolkit.widgets import TextArea


def test_layout_class():
Expand Down Expand Up @@ -51,3 +57,72 @@ def test_layout_class():
def test_create_invalid_layout():
with pytest.raises(InvalidLayoutError):
Layout(HSplit([]))


@pytest.mark.parametrize("height", [1, 2])
@pytest.mark.parametrize(
"prompt,prefix,character",
[
("", "", " "),
(">>> ", "", " "),
("界> ", "", " "),
("", ".. ", " "),
(">>> ", ".. ", " "),
(">>> ", "界 ", " "),
(">>> ", "", "界"),
(">>> ", ".. ", "界"),
],
)
def test_wrapped_text_area_cursor(height, prompt, prefix, character):
class Output(DummyOutput):
def get_size(self):
return Size(rows=height, columns=20)

prefix_width = get_cwidth(prefix)
text = "x" * (20 - get_cwidth(prompt) - prefix_width - 1)
text += "x" * ((height - 1) * (20 - prefix_width))
area = TextArea(
multiline=height > 1,
height=height,
wrap_lines=True,
prompt=prompt,
get_line_prefix=(lambda lineno, wrap_count: prefix) if prefix else None,
)
# Fill the window, type at the wrap boundary, then move back and insert.
keys = iter([text, "x", character, "\x1b[D", "X"])
positions = []
last_document = None

with create_pipe_input() as inp:

def after_render(app):
nonlocal last_document
if app.is_done or area.document == last_document:
return
last_document = area.document
info = area.window.render_info
positions.append((info.cursor_position, area.window.vertical_scroll_2))
key = next(keys, None)
if key is None:
app.exit()
else:
inp.send_text(key)

app = Application(
layout=Layout(area),
input=inp,
output=Output(),
full_screen=True,
after_render=after_render,
)
app.run()

assert positions[1:] == [
(Point(x=19, y=height - 1), 0),
(Point(x=prefix_width, y=height - 1), 1),
(Point(x=prefix_width + get_cwidth(character), y=height - 1), 1),
(Point(x=prefix_width, y=height - 1), 1),
(Point(x=prefix_width + 1, y=height - 1), 1),
]
assert area.text == text + "xX" + character
assert area.buffer.cursor_position == len(text) + 2
Loading