diff --git a/src/prompt_toolkit/shortcuts/prompt.py b/src/prompt_toolkit/shortcuts/prompt.py index f14ecd80c..3dffc2bc5 100644 --- a/src/prompt_toolkit/shortcuts/prompt.py +++ b/src/prompt_toolkit/shortcuts/prompt.py @@ -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), diff --git a/tests/test_shortcuts.py b/tests/test_shortcuts.py index 287c6d33a..63a3942e8 100644 --- a/tests/test_shortcuts.py +++ b/tests/test_shortcuts.py @@ -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 @@ -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", ">"], + ]