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
1 change: 1 addition & 0 deletions src/prompt_toolkit/shortcuts/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ def multi_column_complete_style() -> bool:
ConditionalContainer(
Window(
FormattedTextControl(get_prompt_text_1),
wrap_lines=dyncond("wrap_lines"),
dont_extend_height=True,
),
Condition(has_before_fragments),
Expand Down
92 changes: 91 additions & 1 deletion tests/test_shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from __future__ import annotations

from prompt_toolkit.shortcuts import print_container
import pytest

from prompt_toolkit.data_structures import Size
from prompt_toolkit.filters import Condition
from prompt_toolkit.input import create_pipe_input
from prompt_toolkit.output import DummyOutput
from prompt_toolkit.shortcuts import PromptSession, print_container
from prompt_toolkit.shortcuts.prompt import _split_multiline_prompt
from prompt_toolkit.widgets import Frame, TextArea

Expand Down Expand Up @@ -66,3 +72,87 @@ def test_print_container(tmpdir):
text = fd.read()
assert "Hello world" in text
assert "Title" in text


@pytest.mark.parametrize(
"message, wrap_lines, expected",
[
("abcdefghijklmnopqrstuv\n> ", True, ["abcdefghij", "klmnopqrst", "uv", "> "]),
("abcdefghijklmnopqrstuv\n> ", False, ["abcdefghij", "> "]),
("short\n> ", True, ["short", "> "]),
("abcdefghij\n> ", True, ["abcdefghij", "> "]),
(
"abcdefghijkl\nmnopqrstuvwx\n> ",
True,
["abcdefghij", "kl", "mnopqrstuv", "wx", "> "],
),
],
)
def test_multiline_prompt_wraps_before_input(message, wrap_lines, expected):
class SmallOutput(DummyOutput):
def get_size(self):
return Size(rows=24, columns=10)

rendered_lines = []
with create_pipe_input() as inp:
session = PromptSession(
message,
wrap_lines=wrap_lines,
input=inp,
output=SmallOutput(),
reserve_space_for_menu=0,
)

def after_render(app):
screen = app.renderer._last_screen
if screen is not None:
rendered_lines.extend(
"".join(screen.data_buffer[y][x].char for x in range(10)).rstrip()
for y in range(len(expected))
)
app.exit(result="")

session.app.after_render += after_render
session.prompt()

assert rendered_lines == [line.rstrip() for line in expected]


def test_multiline_prompt_wrap_filter_changes_between_calls():
class SmallOutput(DummyOutput):
def get_size(self):
return Size(rows=24, columns=10)

wrap = False
rendered_lines = []
with create_pipe_input() as inp:
session = PromptSession(
"abcdefghijkl\n> ",
wrap_lines=Condition(lambda: wrap),
input=inp,
output=SmallOutput(),
reserve_space_for_menu=0,
)

def after_render(app):
screen = app.renderer._last_screen
if screen is not None:
rendered_lines.append(
[
"".join(
screen.data_buffer[y][x].char for x in range(10)
).rstrip()
for y in range(3)
]
)
app.exit(result="")

session.app.after_render += after_render
session.prompt()
wrap = True
session.prompt()

assert rendered_lines == [
["abcdefghij", ">", ""],
["abcdefghij", "kl", ">"],
]
Loading