Beispiel #1
0
def test_split_multiline_prompt():
    # Test 1: no newlines:
    tokens = [("class:testclass", "ab")]
    has_before_tokens, before, first_input_line = _split_multiline_prompt(
        lambda: tokens
    )
    assert has_before_tokens() is False
    assert before() == []
    assert first_input_line() == [
        ("class:testclass", "a"),
        ("class:testclass", "b"),
    ]

    # Test 1: multiple lines.
    tokens = [("class:testclass", "ab\ncd\nef")]
    has_before_tokens, before, first_input_line = _split_multiline_prompt(
        lambda: tokens
    )
    assert has_before_tokens() is True
    assert before() == [
        ("class:testclass", "a"),
        ("class:testclass", "b"),
        ("class:testclass", "\n"),
        ("class:testclass", "c"),
        ("class:testclass", "d"),
    ]
    assert first_input_line() == [
        ("class:testclass", "e"),
        ("class:testclass", "f"),
    ]

    # Edge case 1: starting with a newline.
    tokens = [("class:testclass", "\nab")]
    has_before_tokens, before, first_input_line = _split_multiline_prompt(
        lambda: tokens
    )
    assert has_before_tokens() is True
    assert before() == []
    assert first_input_line() == [("class:testclass", "a"), ("class:testclass", "b")]

    # Edge case 2: starting with two newlines.
    tokens = [("class:testclass", "\n\nab")]
    has_before_tokens, before, first_input_line = _split_multiline_prompt(
        lambda: tokens
    )
    assert has_before_tokens() is True
    assert before() == [("class:testclass", "\n")]
    assert first_input_line() == [("class:testclass", "a"), ("class:testclass", "b")]
Beispiel #2
0
    def _create_layout(self):
        has_before_fragments, get_prompt_text_1, get_prompt_text_2 = \
            _split_multiline_prompt(self._get_prompt)

        default_buffer = self.default_buffer

        default_buffer_control = BufferControl(
            buffer=default_buffer,
            search_buffer_control=None,
            input_processors=[],
            include_default_input_processors=False)

        prompt_window = Window(FormattedTextControl(get_prompt_text_1),
                               dont_extend_height=True)
        default_buffer_window = Window(
            default_buffer_control,
            dont_extend_height=True,
            get_line_prefix=partial(self._get_line_prefix,
                                    get_prompt_text_2=get_prompt_text_2))
        divider = Window(char='_', height=1, style='fg:gray bg:black')
        search_window = Window(content=SearchControl(), style='')
        bottom_toolbar = ConditionalContainer(
            Window(FormattedTextControl(lambda: self.bottom_toolbar),
                   dont_extend_height=True,
                   height=Dimension(min=1)),
            filter=~is_done & renderer_height_is_known
            & Condition(lambda: self.bottom_toolbar is not None))

        layout = HSplit([
            prompt_window, default_buffer_window, divider, search_window,
            bottom_toolbar
        ])

        return Layout(layout, default_buffer_window)
def test_split_multiline_prompt():
    # Test 1: no newlines:
    tokens = [('class:testclass', 'ab')]
    has_before_tokens, before, first_input_line = _split_multiline_prompt(
        lambda: tokens)
    assert has_before_tokens() is False
    assert before() == []
    assert first_input_line() == [
        ('class:testclass', 'a'),
        ('class:testclass', 'b'),
    ]

    # Test 1: multiple lines.
    tokens = [('class:testclass', 'ab\ncd\nef')]
    has_before_tokens, before, first_input_line = _split_multiline_prompt(
        lambda: tokens)
    assert has_before_tokens() is True
    assert before() == [
        ('class:testclass', 'a'),
        ('class:testclass', 'b'),
        ('class:testclass', '\n'),
        ('class:testclass', 'c'),
        ('class:testclass', 'd'),
    ]
    assert first_input_line() == [
        ('class:testclass', 'e'),
        ('class:testclass', 'f'),
    ]

    # Edge case 1: starting with a newline.
    tokens = [('class:testclass', '\nab')]
    has_before_tokens, before, first_input_line = _split_multiline_prompt(
        lambda: tokens)
    assert has_before_tokens() is True
    assert before() == []
    assert first_input_line() == [('class:testclass', 'a'),
                                  ('class:testclass', 'b')]

    # Edge case 2: starting with two newlines.
    tokens = [('class:testclass', '\n\nab')]
    has_before_tokens, before, first_input_line = _split_multiline_prompt(
        lambda: tokens)
    assert has_before_tokens() is True
    assert before() == [('class:testclass', '\n')]
    assert first_input_line() == [('class:testclass', 'a'),
                                  ('class:testclass', 'b')]
def test_split_multiline_prompt():
    # Test 1: no newlines:
    tokens = [('class:testclass', 'ab')]
    has_before_tokens, before, first_input_line = _split_multiline_prompt(lambda: tokens)
    assert has_before_tokens() is False
    assert before() == []
    assert first_input_line() == [
        ('class:testclass', 'a'),
        ('class:testclass', 'b'),
    ]

    # Test 1: multiple lines.
    tokens = [('class:testclass', 'ab\ncd\nef')]
    has_before_tokens, before, first_input_line = _split_multiline_prompt(lambda: tokens)
    assert has_before_tokens() is True
    assert before() == [
        ('class:testclass', 'a'),
        ('class:testclass', 'b'),
        ('class:testclass', '\n'),
        ('class:testclass', 'c'),
        ('class:testclass', 'd'),
    ]
    assert first_input_line() == [
        ('class:testclass', 'e'),
        ('class:testclass', 'f'),
    ]

    # Edge case 1: starting with a newline.
    tokens = [('class:testclass', '\nab')]
    has_before_tokens, before, first_input_line = _split_multiline_prompt(lambda: tokens)
    assert has_before_tokens() is True
    assert before() == []
    assert first_input_line() == [
        ('class:testclass', 'a'),
        ('class:testclass', 'b')
    ]

    # Edge case 2: starting with two newlines.
    tokens = [('class:testclass', '\n\nab')]
    has_before_tokens, before, first_input_line = _split_multiline_prompt(lambda: tokens)
    assert has_before_tokens() is True
    assert before() == [('class:testclass', '\n')]
    assert first_input_line() == [
        ('class:testclass', 'a'),
        ('class:testclass', 'b')
    ]
Beispiel #5
0
    def create_layout(self):
        # Create functions that will dynamically split the prompt. (If we have
        # a multiline prompt.)
        has_before_fragments, get_prompt_text_1, get_prompt_text_2 = \
            _split_multiline_prompt(self.formatted_message)

        default_buffer = ModalBuffer(
            name=DEFAULT_BUFFER,
            complete_while_typing=Condition(
                lambda: self.complete_while_typing),
            completer=DynamicCompleter(lambda: self.completer),
            history=self.history,
            get_tempfile_suffix=lambda: self.tempfile_suffix)

        search_buffer = Buffer(name=SEARCH_BUFFER)

        search_toolbar = SearchToolbar(search_buffer)

        input_processor = merge_processors([
            ConditionalProcessor(HighlightSearchProcessor(preview_search=True),
                                 has_focus(search_buffer)),
            HighlightSelectionProcessor(),
            HighlightMatchingBracketProcessor(),
            DisplayMultipleCursors()
        ])

        default_buffer_control = BufferControl(
            buffer=default_buffer,
            search_buffer_control=search_toolbar.control,
            input_processor=input_processor,
            lexer=DynamicLexer(lambda: self.lexer),
            preview_search=True)

        def get_default_buffer_control_height():
            # If there is an autocompletion menu to be shown, make sure that our
            # layout has at least a minimal height in order to display it.
            space = self.reserve_space_for_menu

            if space and not get_app().is_done:
                buff = default_buffer
                if buff.complete_while_typing(
                ) or buff.complete_state is not None:
                    return Dimension(min=space)
            return Dimension()

        def get_continuation(width):
            prompt_continuation = self.prompt_continuation

            if callable(prompt_continuation):
                prompt_continuation = prompt_continuation(width)

            return to_formatted_text(prompt_continuation,
                                     style='class:prompt-continuation')

        default_buffer_window = Window(
            default_buffer_control,
            height=get_default_buffer_control_height,
            left_margins=[PromptMargin(get_prompt_text_2, get_continuation)],
            wrap_lines=True)

        def get_arg_text():
            arg = self.app.key_processor.arg
            if arg == '-':
                arg = '-1'

            return [('class:arg-toolbar', 'Repeat: '),
                    ('class:arg-toolbar.text', arg)]

        # Build the layout.
        layout = HSplit([
            # The main input, with completion menus floating on top of it.
            FloatContainer(
                HSplit([
                    ConditionalContainer(
                        Window(FormattedTextControl(get_prompt_text_1),
                               dont_extend_height=True),
                        Condition(has_before_fragments)),
                    default_buffer_window,
                ]),
                [
                    # Completion menus.
                    Float(xcursor=True,
                          ycursor=True,
                          content=MultiColumnCompletionsMenu(
                              show_meta=True,
                              extra_filter=has_focus(default_buffer)))
                ]),
            ConditionalContainer(
                Window(FormattedTextControl(get_arg_text), height=1), has_arg),
            search_toolbar
        ])

        return Layout(layout, default_buffer_window)