Exemplo n.º 1
0
    def create_content(self, width, height):
        # Report dimensions to the process.
        self.process.set_size(width, height)

        # The first time that this user control is rendered. Keep track of the
        # 'app' object and start the process.
        if not self._running:
            self.process.start()
            self._running = True

        if not self.process.screen:
            return UIContent()

        pt_screen = self.process.screen.pt_screen
        data_buffer = pt_screen.data_buffer
        cursor_y = pt_screen.cursor_position.y

        # Prompt_toolkit needs the amount of characters before the cursor in a
        # UIControl.  This doesn't correspond with the xpos in case of double
        # width characters. That's why we compute the wcwidth.
        cursor_row = data_buffer[pt_screen.cursor_position.y]
        text_before_cursor = ''.join(
            cursor_row[x].char for x in range(0, pt_screen.cursor_position.x))
        cursor_x = len(text_before_cursor)

        def get_line(number):
            row = data_buffer[number]
            empty = True
            if row:
                max_column = max(row)
                empty = False
            else:
                max_column = 0

            if number == cursor_y:
                max_column = max(max_column, cursor_x)
                empty = False

            if empty:
                return [('', ' ')]
            else:
                cells = [row[i] for i in range(max_column + 1)]
                return [(cell.style, cell.char) for cell in cells]

        if data_buffer:
            line_count = max(
                data_buffer
            ) + 1  # TODO: substract all empty lines from the beginning. (If we need to. Not sure.)
        else:
            line_count = 1

        return UIContent(get_line,
                         line_count=line_count,
                         show_cursor=pt_screen.show_cursor,
                         cursor_position=Point(x=cursor_x, y=cursor_y))
Exemplo n.º 2
0
    def create_content(self, width: int, height: int) -> UIContent:
        def get_line(i: int) -> StyleAndTextTuples:
            return self._items[i]

        return UIContent(get_line=get_line,
                         line_count=len(self._items),
                         show_cursor=False)
Exemplo n.º 3
0
    def create_content(self, width: int, height: int) -> UIContent:
        def _get_line_tokens(line_number):
            choice = self._get_available_choices()[line_number]
            tokens = []

            selected = (choice == self.get_selection())

            if selected:
                tokens.append(('class:set-cursor-position', ' \u276f '))
            else:
                # For alignment
                tokens.append(('', '   '))

            if choice.is_disabled:
                token_text = choice.display_text
                if choice.disabled_reason:
                    token_text += f' ({choice.disabled_reason})'
                tokens.append(
                    ('class:selected' if selected else 'class:disabled',
                     token_text))
            else:
                tokens.append(('class:selected' if selected else '',
                               str(choice.display_text)))

            return tokens

        return UIContent(
            get_line=_get_line_tokens,
            line_count=self.choice_count,
        )
    def create_content(self, width, height):
        complete_state = get_app().current_buffer.complete_state
        if complete_state:
            completions = complete_state.completions
            index = complete_state.complete_index  # Can be None!

            # Width of the completions without the left/right arrows in the margins.
            content_width = width - 6

            # Booleans indicating whether we stripped from the left/right
            cut_left = False
            cut_right = False

            # Create Menu content.
            fragments = []

            for i, c in enumerate(completions):
                # When there is no more place for the next completion
                if fragment_list_len(fragments) + len(
                        c.display_text) >= content_width:
                    # If the current one was not yet displayed, page to the next sequence.
                    if i <= (index or 0):
                        fragments = []
                        cut_left = True
                    # If the current one is visible, stop here.
                    else:
                        cut_right = True
                        break

                fragments.extend(
                    to_formatted_text(
                        c.display_text,
                        style=('class:completion-toolbar.completion.current'
                               if i == index else
                               'class:completion-toolbar.completion')))
                fragments.append(('', ' '))

            # Extend/strip until the content width.
            fragments.append(
                ('', ' ' * (content_width - fragment_list_len(fragments))))
            fragments = fragments[:content_width]

            # Return fragments
            all_fragments = [
                ('', ' '),
                ('class:completion-toolbar.arrow', '<' if cut_left else ' '),
                ('', ' '),
            ] + fragments + [
                ('', ' '),
                ('class:completion-toolbar.arrow', '>' if cut_right else ' '),
                ('', ' '),
            ]
        else:
            all_fragments = []

        def get_line(i):
            return all_fragments

        return UIContent(get_line=get_line, line_count=1)
Exemplo n.º 5
0
    def create_content(self, width, height):
        menu_width = self.preferred_width(width)

        def get_line(i):
            item = self._get_items()[i]
            is_selected = (i == self._selection)
            return self._menu_item_fragment(item, is_selected, menu_width)

        return UIContent(get_line=get_line,
                         cursor_position=Point(x=0, y=self._selection or 0),
                         line_count=len(self._get_items()))
Exemplo n.º 6
0
    def create_content(self, width, height):
        items = []
        for pr in self.progress_bar.counters:
            items.append(
                to_formatted_text(
                    self.progress_bar.formatter(self.progress_bar, pr, width)))

        def get_line(i):
            return items[i]

        return UIContent(get_line=get_line,
                         line_count=len(items),
                         show_cursor=False)
Exemplo n.º 7
0
    def create_content(self, app, width, height):
        self.process.set_size(width, height)

        if not self.process.screen:
            return UIContent()

        pt_screen = self.process.screen.pt_screen
        data_buffer = pt_screen.data_buffer
        cursor_y = pt_screen.cursor_position.y
        cursor_x = pt_screen.cursor_position.x

        def get_line(number):
            row = data_buffer[number]
            empty = True
            if row:
                max_column = max(row)
                empty = False
            else:
                max_column = 0

            if number == cursor_y:
                max_column = max(max_column, cursor_x)
                empty = False

            if empty:
                return [(Token, ' ')]
            else:
                cells = [row[i] for i in range(max_column + 1)]
                return [(cell.token, cell.char) for cell in cells]

        if data_buffer:
            line_count = max(data_buffer) + 1
        else:
            line_count = 1

        return UIContent(get_line,
                         line_count=line_count,
                         cursor_position=Point(x=pt_screen.cursor_position.x,
                                               y=pt_screen.cursor_position.y))
Exemplo n.º 8
0
    def create_content(self, width: int, height: int) -> UIContent:
        items: List[StyleAndTextTuples] = []

        for pr in self.progress_bar.counters:
            try:
                text = self.formatter.format(self.progress_bar, pr, width)
            except BaseException:
                traceback.print_exc()
                text = "ERROR"

            items.append(to_formatted_text(text))

        def get_line(i: int) -> StyleAndTextTuples:
            return items[i]

        return UIContent(get_line=get_line, line_count=len(items), show_cursor=False)
Exemplo n.º 9
0
    def create_content(self, width, height):
        items = []
        for pr in self.progress_bar.counters:
            try:
                text = self.formatter.format(self.progress_bar, pr, width)
            except BaseException:
                traceback.print_exc()
                text = 'ERROR'

            items.append(to_formatted_text(text))

        def get_line(i):
            return items[i]

        return UIContent(get_line=get_line,
                         line_count=len(items),
                         show_cursor=False)
Exemplo n.º 10
0
 def __init__(self, m):
     self.m = m
     self.content = UIContent(get_line=self.get_line,
                              line_count=m.line_count(),
                              show_cursor=True)
Exemplo n.º 11
0
editing = False
insert_mode = Condition(lambda cli: editing)

wrap = False
wrapping = Condition(lambda cli: wrap)


def get_line(i):
    return [(Token.LineNumber, "%04d" % i),
            (Token.Title, 50 * '(Hello, World) ')]


content = UIContent(
    get_line=get_line,
    show_cursor=True,
    # cursor_position=Point(150, 2),
    line_count=1000000)


class MyControl(UIControl):
    def create_content(self, cli, width, height):
        return content

    def move_cursor(self, y):
        content.cursor_position = Point(y, 0)


def get_status_tokens(cli):
    return [
        (Token.Title, ' Status '), (Token.Title, str(editing)),
Exemplo n.º 12
0
 def get_content():
     return UIContent(get_line=lambda i: fragment_lines[i],
                      line_count=len(fragment_lines),
                      cursor_position=cursor_position,
                      show_cursor=self.show_cursor)