Пример #1
0
    def __init__(self,
                 prompt: AnyFormattedText = 'Shell command: ',
                 enable_global_bindings: FilterOrBool = True) -> None:

        self.prompt = prompt
        self.enable_global_bindings = to_filter(enable_global_bindings)

        self.system_buffer = Buffer(name=SYSTEM_BUFFER)

        self._bindings = self._build_key_bindings()

        self.buffer_control = BufferControl(
            buffer=self.system_buffer,
            lexer=SimpleLexer(style='class:system-toolbar.text'),
            input_processors=[
                BeforeInput(lambda: self.prompt, style='class:system-toolbar')
            ],
            key_bindings=self._bindings)

        self.window = Window(self.buffer_control,
                             height=1,
                             style='class:system-toolbar')

        self.container = ConditionalContainer(content=self.window,
                                              filter=has_focus(
                                                  self.system_buffer))
Пример #2
0
    def __init__(self) -> None:
        def get_formatted_text() -> StyleAndTextTuples:
            arg = get_app().key_processor.arg or ''
            if arg == '-':
                arg = '-1'

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

        self.window = Window(FormattedTextControl(get_formatted_text),
                             height=1)

        self.container = ConditionalContainer(content=self.window,
                                              filter=has_arg)
Пример #3
0
    def __init__(
            self,
            search_buffer: Optional[Buffer] = None,
            vi_mode: bool = False,
            text_if_not_searching: AnyFormattedText = '',
            forward_search_prompt: AnyFormattedText = 'I-search: ',
            backward_search_prompt: AnyFormattedText = 'I-search backward: ',
            ignore_case: FilterOrBool = False) -> None:

        if search_buffer is None:
            search_buffer = Buffer()

        @Condition
        def is_searching() -> bool:
            return self.control in get_app().layout.search_links

        def get_before_input() -> AnyFormattedText:
            if not is_searching():
                return text_if_not_searching
            elif self.control.searcher_search_state.direction == SearchDirection.BACKWARD:
                return ('?' if vi_mode else backward_search_prompt)
            else:
                return ('/' if vi_mode else forward_search_prompt)

        self.search_buffer = search_buffer

        self.control = SearchBufferControl(
            buffer=search_buffer,
            input_processors=[
                BeforeInput(get_before_input,
                            style='class:search-toolbar.prompt')
            ],
            lexer=SimpleLexer(style='class:search-toolbar.text'),
            ignore_case=ignore_case)

        self.container = ConditionalContainer(content=Window(
            self.control, height=1, style='class:search-toolbar'),
                                              filter=is_searching)
Пример #4
0
    def __init__(self, show_position: bool = False) -> None:
        def get_formatted_text() -> StyleAndTextTuples:
            buff = get_app().current_buffer

            if buff.validation_error:
                row, column = buff.document.translate_index_to_position(
                    buff.validation_error.cursor_position)

                if show_position:
                    text = '%s (line=%s column=%s)' % (
                        buff.validation_error.message, row + 1, column + 1)
                else:
                    text = buff.validation_error.message

                return [('class:validation-toolbar', text)]
            else:
                return []

        self.control = FormattedTextControl(get_formatted_text)

        self.container = ConditionalContainer(content=Window(self.control,
                                                             height=1),
                                              filter=has_validation_error)
Пример #5
0
    def __init__(self,
                 body: AnyContainer,
                 title: AnyFormattedText = '',
                 style: str = '',
                 width: AnyDimension = None,
                 height: AnyDimension = None,
                 key_bindings: Optional[KeyBindings] = None,
                 modal: bool = False) -> None:

        self.title = title
        self.body = body

        fill = partial(Window, style='class:frame.border')
        style = 'class:frame ' + style

        top_row_with_title = VSplit(
            [
                fill(width=1, height=1, char=Border.TOP_LEFT),
                fill(char=Border.HORIZONTAL),
                fill(width=1, height=1, char='|'),
                # Notice: we use `Template` here, because `self.title` can be an
                # `HTML` object for instance.
                Label(lambda: Template(' {} ').format(self.title),
                      style='class:frame.label',
                      dont_extend_width=True),
                fill(width=1, height=1, char='|'),
                fill(char=Border.HORIZONTAL),
                fill(width=1, height=1, char=Border.TOP_RIGHT),
            ],
            height=1)

        top_row_without_title = VSplit([
            fill(width=1, height=1, char=Border.TOP_LEFT),
            fill(char=Border.HORIZONTAL),
            fill(width=1, height=1, char=Border.TOP_RIGHT),
        ],
                                       height=1)

        @Condition
        def has_title() -> bool:
            return bool(self.title)

        self.container = HSplit(
            [
                ConditionalContainer(content=top_row_with_title,
                                     filter=has_title),
                ConditionalContainer(content=top_row_without_title,
                                     filter=~has_title),
                VSplit(
                    [
                        fill(width=1, char=Border.VERTICAL),
                        DynamicContainer(lambda: self.body),
                        fill(width=1, char=Border.VERTICAL),
                        # Padding is required to make sure that if the content is
                        # too small, the right frame border is still aligned.
                    ],
                    padding=0),
                VSplit([
                    fill(width=1, height=1, char=Border.BOTTOM_LEFT),
                    fill(char=Border.HORIZONTAL),
                    fill(width=1, height=1, char=Border.BOTTOM_RIGHT),
                ]),
            ],
            width=width,
            height=height,
            style=style,
            key_bindings=key_bindings,
            modal=modal)
Пример #6
0
 def __init__(self) -> None:
     self.container = ConditionalContainer(content=Window(
         _CompletionsToolbarControl(),
         height=1,
         style='class:completion-toolbar'),
                                           filter=has_completions)
Пример #7
0
    def __init__(self, body: AnyContainer, menu_items: List['MenuItem'],
                 floats: Optional[List[Float]] = None,
                 key_bindings: Optional[KeyBindingsBase] = None) -> None:

        self.body = body
        self.menu_items = menu_items
        self.selected_menu = [0]

        # Key bindings.
        kb = KeyBindings()

        @Condition
        def in_main_menu() -> bool:
            return len(self.selected_menu) == 1

        @Condition
        def in_sub_menu() -> bool:
            return len(self.selected_menu) > 1

        # Navigation through the main menu.

        @kb.add('left', filter=in_main_menu)
        def _(event: E) -> None:
            self.selected_menu[0] = max(0, self.selected_menu[0] - 1)

        @kb.add('right', filter=in_main_menu)
        def _(event: E) -> None:
            self.selected_menu[0] = min(
                len(self.menu_items) - 1, self.selected_menu[0] + 1)

        @kb.add('down', filter=in_main_menu)
        def _(event: E) -> None:
            self.selected_menu.append(0)

        @kb.add('c-c', filter=in_main_menu)
        @kb.add('c-g', filter=in_main_menu)
        def _(event: E) -> None:
            " Leave menu. "
            event.app.layout.focus_last()

        # Sub menu navigation.

        @kb.add('left', filter=in_sub_menu)
        @kb.add('c-g', filter=in_sub_menu)
        @kb.add('c-c', filter=in_sub_menu)
        def _(event: E) -> None:
            " Go back to parent menu. "
            if len(self.selected_menu) > 1:
                self.selected_menu.pop()

        @kb.add('right', filter=in_sub_menu)
        def _(event: E) -> None:
            " go into sub menu. "
            if self._get_menu(len(self.selected_menu) - 1).children:
                self.selected_menu.append(0)

            # If This item does not have a sub menu. Go up in the parent menu.
            elif len(self.selected_menu) == 2 and self.selected_menu[0] < len(self.menu_items) - 1:
                self.selected_menu = [min(
                    len(self.menu_items) - 1, self.selected_menu[0] + 1)]
                if self.menu_items[self.selected_menu[0]].children:
                    self.selected_menu.append(0)

        @kb.add('up', filter=in_sub_menu)
        def _(event: E) -> None:
            " Select previous (enabled) menu item or return to main menu. "
            # Look for previous enabled items in this sub menu.
            menu = self._get_menu(len(self.selected_menu) - 2)
            index = self.selected_menu[-1]

            previous_indexes = [i for i, item in enumerate(menu.children)
                            if i < index and not item.disabled]

            if previous_indexes:
                self.selected_menu[-1] = previous_indexes[-1]
            elif len(self.selected_menu) == 2:
                # Return to main menu.
                self.selected_menu.pop()

        @kb.add('down', filter=in_sub_menu)
        def _(event: E) -> None:
            " Select next (enabled) menu item. "
            menu = self._get_menu(len(self.selected_menu) - 2)
            index = self.selected_menu[-1]

            next_indexes = [i for i, item in enumerate(menu.children)
                            if i > index and not item.disabled]

            if next_indexes:
                self.selected_menu[-1] = next_indexes[0]

        @kb.add('enter')
        def _(event: E) -> None:
            " Click the selected menu item. "
            item = self._get_menu(len(self.selected_menu) - 1)
            if item.handler:
                event.app.layout.focus_last()
                item.handler()

        # Controls.
        self.control = FormattedTextControl(
            self._get_menu_fragments,
            key_bindings=kb,
            focusable=True,
            show_cursor=False)

        self.window = Window(
            height=1,
            content=self.control,
            style='class:menu-bar')

        submenu = self._submenu(0)
        submenu2 = self._submenu(1)
        submenu3 = self._submenu(2)

        @Condition
        def has_focus() -> bool:
            return get_app().layout.current_window == self.window

        self.container = FloatContainer(
            content=HSplit([
                # The titlebar.
                self.window,

                # The 'body', like defined above.
                body,
            ]),
            floats=[
                Float(xcursor=True, ycursor=True,
                      content=ConditionalContainer(
                          content=Shadow(body=submenu),
                          filter=has_focus)),
                Float(attach_to_window=submenu,
                      xcursor=True, ycursor=True,
                      allow_cover_cursor=True,
                      content=ConditionalContainer(
                          content=Shadow(body=submenu2),
                          filter=has_focus & Condition(lambda: len(self.selected_menu) >= 1))),
                Float(attach_to_window=submenu2,
                      xcursor=True, ycursor=True,
                      allow_cover_cursor=True,
                      content=ConditionalContainer(
                          content=Shadow(body=submenu3),
                          filter=has_focus & Condition(lambda: len(self.selected_menu) >= 2))),

                # --
            ] + (floats or []),
            key_bindings=key_bindings,
        )
Пример #8
0
                             width=D(preferred=80),
                             modal=True)

    def __pt_container__(self):
        return self.dialog


body = HSplit([
    text_field,
    search_toolbar,
    ConditionalContainer(
        content=VSplit([
            Window(FormattedTextControl(get_statusbar_text),
                   style='class:status'),
            Window(FormattedTextControl(get_statusbar_right_text),
                   style='class:status.right',
                   width=9,
                   align=WindowAlign.RIGHT),
        ],
                       height=1),
        filter=Condition(lambda: ApplicationState.show_status_bar)),
])

# Global key bindings.
bindings = KeyBindings()


@bindings.add('c-c')
def _(event):
    " Focus menu. "
    event.app.layout.focus(root_container.window)