Exemplo n.º 1
0
    def __init__(self):
        self._percentage = 60

        self.label = Label('60%')
        self.container = FloatContainer(
            content=Window(height=1),
            floats=[
                # We first draw the label, than the actual progress bar.  Right
                # now, this is the only way to have the colors of the progress
                # bar appear on to of the label. The problem is that our label
                # can't be part of any `Window` below.
                Float(content=self.label, top=0, bottom=0),
                Float(left=0,
                      top=0,
                      right=0,
                      bottom=0,
                      content=VSplit([
                          Window(
                              style='class:progress-bar.used',
                              width=lambda: D(weight=int(self._percentage))),
                          Window(style='class:progress-bar',
                                 width=lambda: D(weight=int(100 - self.
                                                            _percentage))),
                      ])),
            ])
Exemplo n.º 2
0
    def __init__(self,
                 text,
                 style='',
                 width=None,
                 dont_extend_height=True,
                 dont_extend_width=False):
        assert is_formatted_text(text)
        self.text = text

        def get_width():
            if width is None:
                text_fragments = to_formatted_text(self.text)
                text = fragment_list_to_text(text_fragments)
                if text:
                    longest_line = max(
                        get_cwidth(line) for line in text.splitlines())
                else:
                    return D(preferred=0)
                return D(preferred=longest_line)
            else:
                return width

        self.formatted_text_control = FormattedTextControl(
            text=lambda: self.text)

        self.window = Window(content=self.formatted_text_control,
                             width=get_width,
                             style='class:label ' + style,
                             dont_extend_height=dont_extend_height,
                             dont_extend_width=dont_extend_width)
Exemplo n.º 3
0
    def __init__(self, text, handler=None, width=12):
        assert isinstance(text, six.text_type)
        assert handler is None or callable(handler)
        assert isinstance(width, int)

        self.text = text
        self.handler = handler
        self.width = width
        self.control = FormattedTextControl(
            self._get_text_fragments,
            key_bindings=self._get_key_bindings(),
            focusable=True)

        def get_style():
            if get_app().layout.has_focus(self):
                return 'class:button.focused'
            else:
                return 'class:button'

        self.window = Window(self.control,
                             align=WindowAlign.CENTER,
                             height=1,
                             width=width,
                             style=get_style,
                             dont_extend_width=True,
                             dont_extend_height=True)
Exemplo n.º 4
0
    def __init__(self,
                 body,
                 padding=None,
                 padding_left=None,
                 padding_right=None,
                 padding_top=None,
                 padding_bottom=None,
                 width=None,
                 height=None,
                 style='',
                 char=None,
                 modal=False,
                 key_bindings=None):
        assert is_container(body)

        if padding is None:
            padding = D(preferred=0)

        def get(value):
            if value is None:
                value = padding
            return to_dimension(value)

        self.padding_left = get(padding_left)
        self.padding_right = get(padding_right)
        self.padding_top = get(padding_top)
        self.padding_bottom = get(padding_bottom)
        self.body = body

        self.container = HSplit([
            Window(height=self.padding_top, char=char),
            VSplit([
                Window(width=self.padding_left, char=char),
                body,
                Window(width=self.padding_right, char=char),
            ]),
            Window(height=self.padding_bottom, char=char),
        ],
                                width=width,
                                height=height,
                                style=style,
                                modal=modal,
                                key_bindings=None)
Exemplo n.º 5
0
    def __init__(self, body):
        assert is_container(body)

        self.container = FloatContainer(
            content=body,
            floats=[
                Float(bottom=-1,
                      height=1,
                      left=1,
                      right=-1,
                      transparent=True,
                      content=Window(style='class:shadow')),
                Float(bottom=-1,
                      top=1,
                      width=1,
                      right=-1,
                      transparent=True,
                      content=Window(style='class:shadow')),
            ])
Exemplo n.º 6
0
    def __init__(self):
        def get_formatted_text():
            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)
Exemplo n.º 7
0
    def __init__(self, text=''):
        assert is_formatted_text(text)

        self.checked = True

        kb = KeyBindings()

        @kb.add(' ')
        @kb.add('enter')
        def _(event):
            self.checked = not self.checked

        self.control = FormattedTextControl(self._get_text_fragments,
                                            key_bindings=kb,
                                            focusable=True)

        self.window = Window(width=3, content=self.control, height=1)

        self.container = VSplit(
            [self.window,
             Label(text=Template(' {}').format(text))],
            style='class:checkbox')
Exemplo n.º 8
0
    def __init__(self,
                 search_buffer=None,
                 vi_mode=False,
                 text_if_not_searching='',
                 forward_search_prompt='I-search: ',
                 backward_search_prompt='I-search backward: ',
                 ignore_case=False):
        assert search_buffer is None or isinstance(search_buffer, Buffer)

        if search_buffer is None:
            search_buffer = Buffer()

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

        def get_before_input():
            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)
Exemplo n.º 9
0
    def __init__(self, prompt='Shell command: ', enable_global_bindings=True):
        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))
Exemplo n.º 10
0
    def __init__(self, show_position=False):
        def get_formatted_text():
            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)
Exemplo n.º 11
0
    def __init__(self, values):
        assert isinstance(values, list)
        assert len(values) > 0
        assert all(isinstance(i, tuple) and len(i) == 2 for i in values)

        self.values = values
        self.current_value = values[0][0]
        self._selected_index = 0

        # Key bindings.
        kb = KeyBindings()

        @kb.add('up')
        def _(event):
            self._selected_index = max(0, self._selected_index - 1)

        @kb.add('down')
        def _(event):
            self._selected_index = min(
                len(self.values) - 1, self._selected_index + 1)

        @kb.add('enter')
        @kb.add(' ')
        def _(event):
            self.current_value = self.values[self._selected_index][0]

        # Control and window.
        self.control = FormattedTextControl(self._get_text_fragments,
                                            key_bindings=kb,
                                            focusable=True)

        self.window = Window(content=self.control,
                             style='class:radio-list',
                             right_margins=[
                                 ScrollbarMargin(display_arrows=True),
                             ],
                             dont_extend_height=True)
Exemplo n.º 12
0
 def __init__(self):
     self.container = ConditionalContainer(content=Window(
         _CompletionsToolbarControl(),
         height=1,
         style='class:completion-toolbar'),
                                           filter=has_completions)
Exemplo n.º 13
0
    def __init__(self, body, menu_items=None, floats=None, key_bindings=None):
        assert isinstance(menu_items, list) and \
            all(isinstance(i, MenuItem) for i in menu_items)
        assert floats is None or all(isinstance(f, Float) for f in floats)

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

        # Key bindings.
        kb = KeyBindings()

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

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

        # Navigation through the main menu.

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

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

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

        @kb.add('c-c', filter=in_main_menu)
        @kb.add('c-g', filter=in_main_menu)
        def _(event):
            " 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):
            " Go back to parent menu. "
            if len(self.selected_menu) > 1:
                self.selected_menu.pop()

        @kb.add('right', filter=in_sub_menu)
        def _(event):
            " 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):
            " 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):
            " 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):
            " 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():
            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=self.window,
                      ycursor=self.window,
                      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,
        )
Exemplo n.º 14
0
    def _submenu(self, level=0):
        def get_text_fragments():
            result = []
            if level < len(self.selected_menu):
                menu = self._get_menu(level)
                if menu.children:
                    result.append(('class:menu', Border.TOP_LEFT))
                    result.append(
                        ('class:menu', Border.HORIZONTAL * (menu.width + 4)))
                    result.append(('class:menu', Border.TOP_RIGHT))
                    result.append(('', '\n'))
                    try:
                        selected_item = self.selected_menu[level + 1]
                    except IndexError:
                        selected_item = -1

                    def one_item(i, item):
                        def mouse_handler(mouse_event):
                            if mouse_event.event_type == MouseEventType.MOUSE_UP:
                                app = get_app()
                                if item.handler:
                                    app.layout.focus_last()
                                    item.handler()
                                else:
                                    self.selected_menu = self.selected_menu[:level
                                                                            +
                                                                            1] + [
                                                                                i
                                                                            ]

                        if i == selected_item:
                            yield ('[SetCursorPosition]', '')
                            style = 'class:menu-bar.selected-item'
                        else:
                            style = ''

                        yield ('class:menu', Border.VERTICAL)
                        if item.text == '-':
                            yield (style + 'class:menu-border', '{}'.format(
                                Border.HORIZONTAL * (menu.width + 3)),
                                   mouse_handler)
                        else:
                            yield (style, ' {}'.format(
                                item.text).ljust(menu.width + 3),
                                   mouse_handler)

                        if item.children:
                            yield (style, '>', mouse_handler)
                        else:
                            yield (style, ' ', mouse_handler)

                        if i == selected_item:
                            yield ('[SetMenuPosition]', '')
                        yield ('class:menu', Border.VERTICAL)

                        yield ('', '\n')

                    for i, item in enumerate(menu.children):
                        result.extend(one_item(i, item))

                    result.append(('class:menu', Border.BOTTOM_LEFT))
                    result.append(
                        ('class:menu', Border.HORIZONTAL * (menu.width + 4)))
                    result.append(('class:menu', Border.BOTTOM_RIGHT))
            return result

        return Window(FormattedTextControl(get_text_fragments),
                      style='class:menu')
Exemplo n.º 15
0
    def __init__(self,
                 text='',
                 multiline=True,
                 password=False,
                 lexer=None,
                 completer=None,
                 accept_handler=None,
                 focusable=True,
                 wrap_lines=True,
                 read_only=False,
                 width=None,
                 height=None,
                 dont_extend_height=False,
                 dont_extend_width=False,
                 line_numbers=False,
                 scrollbar=False,
                 style='',
                 search_field=None,
                 preview_search=True,
                 prompt=''):
        assert isinstance(text, six.text_type)
        assert search_field is None or isinstance(search_field, SearchToolbar)

        if search_field is None:
            search_control = None
        elif isinstance(search_field, SearchToolbar):
            search_control = search_field.control

        self.buffer = Buffer(
            document=Document(text, 0),
            multiline=multiline,
            read_only=read_only,
            completer=completer,
            complete_while_typing=True,
            accept_handler=lambda buff: accept_handler and accept_handler())

        self.control = BufferControl(
            buffer=self.buffer,
            lexer=lexer,
            input_processors=[
                ConditionalProcessor(processor=PasswordProcessor(),
                                     filter=to_filter(password)),
                BeforeInput(prompt, style='class:text-area.prompt'),
            ],
            search_buffer_control=search_control,
            preview_search=preview_search,
            focusable=focusable)

        if multiline:
            if scrollbar:
                right_margins = [ScrollbarMargin(display_arrows=True)]
            else:
                right_margins = []
            if line_numbers:
                left_margins = [NumberedMargin()]
            else:
                left_margins = []
        else:
            wrap_lines = False  # Never wrap for single line input.
            height = D.exact(1)
            left_margins = []
            right_margins = []

        style = 'class:text-area ' + style

        self.window = Window(height=height,
                             width=width,
                             dont_extend_height=dont_extend_height,
                             dont_extend_width=dont_extend_width,
                             content=self.control,
                             style=style,
                             wrap_lines=wrap_lines,
                             left_margins=left_margins,
                             right_margins=right_margins)
Exemplo n.º 16
0
 def __init__(self):
     self.window = Window(char=Border.HORIZONTAL,
                          style='class:line,horizontal-line',
                          height=1)
Exemplo n.º 17
0
 def __init__(self):
     self.window = Window(char=Border.VERTICAL,
                          style='class:line,vertical-line',
                          width=1)