Beispiel #1
0
def _trim_text(text, max_width):
    """
    Trim the text to `max_width`, append dots when the text is too long.
    Returns (text, width) tuple.
    """
    width = get_cwidth(text)

    # When the text is too wide, trim it.
    if width > max_width:
        # When there are no double width characters, just use slice operation.
        if len(text) == width:
            trimmed_text = (text[:max(1, max_width-3)] + '...')[:max_width]
            return trimmed_text, len(trimmed_text)

        # Otherwise, loop until we have the desired width. (Rather
        # inefficient, but ok for now.)
        else:
            trimmed_text = ''
            for c in text:
                if get_cwidth(trimmed_text + c) <= max_width - 3:
                    trimmed_text += c
            trimmed_text += '...'

            return (trimmed_text, get_cwidth(trimmed_text))
    else:
        return text, width
Beispiel #2
0
 def preferred_width(self, cli, max_available_width):
     """
     Return the preferred width for this control.
     That is the width of the longest line.
     """
     text = token_list_to_text(self._get_tokens_cached(cli))
     line_lengths = [get_cwidth(l) for l in text.split('\n')]
     return max(line_lengths)
Beispiel #3
0
 def _get_menu_meta_width(self, max_width, complete_state):
     """
     Return the width of the meta column.
     """
     if self._show_meta(complete_state):
         return min(max_width, max(get_cwidth(c.display_meta)
                    for c in complete_state.current_completions) + 2)
     else:
         return 0
Beispiel #4
0
    def __init__(self, char=' ', token=Token):
        # If this character has to be displayed otherwise, take that one.
        char = self.display_mappings.get(char, char)

        self.char = char
        self.token = token

        # Calculate width. (We always need this, so better to store it directly
        # as a member for performance.)
        self.width = get_cwidth(char)
Beispiel #5
0
def token_list_width(tokenlist):
    """
    Return the character width of this token list.
    (Take double width characters into account.)

    :param tokenlist: List of (token, text) or (token, text, mouse_handler)
                      tuples.
    """
    ZeroWidthEscape = Token.ZeroWidthEscape
    return sum(
        get_cwidth(c) for item in tokenlist for c in item[1]
        if item[0] != ZeroWidthEscape)
Beispiel #6
0
    def preferred_width(self, cli, max_available_width):
        """
        Report the width of the longest meta text as the preferred width of this control.

        It could be that we use less width, but this way, we're sure that the
        layout doesn't change when we select another completion (E.g. that
        completions are suddenly shown in more or fewer columns.)
        """
        if cli.current_buffer.complete_state:
            state = cli.current_buffer.complete_state
            return 2 + max(get_cwidth(c.display_meta) for c in state.current_completions)
        else:
            return 0
Beispiel #7
0
    def get_height_for_text(text, width):
        # Get text width for this line.
        line_width = get_cwidth(text)

        # Calculate height.
        try:
            quotient, remainder = divmod(line_width, width)
        except ZeroDivisionError:
            # Return something very big.
            # (This can happen, when the Window gets very small.)
            return 10**10
        else:
            if remainder:
                quotient += 1  # Like math.ceil.
            return max(1, quotient)
Beispiel #8
0
 def _get_menu_width(self, max_width, complete_state):
     """
     Return the width of the main column.
     """
     return min(max_width, max(self.MIN_WIDTH, max(get_cwidth(c.display)
                for c in complete_state.current_completions) + 2))
Beispiel #9
0
 def _get_column_width(self, complete_state):
     """
     Return the width of each column.
     """
     return max(get_cwidth(c.display) for c in complete_state.current_completions) + 1
Beispiel #10
0
 def get_width(self, cli, ui_content):
     " Width to report to the `Window`. "
     # Take the width from the first line.
     text = token_list_to_text(self.get_prompt_tokens(cli))
     return get_cwidth(text)
Beispiel #11
0
def _display_completions_like_readline(cli, completions):
    """
    Display the list of completions in columns above the prompt.
    This will ask for a confirmation if there are too many completions to fit
    on a single page and provide a paginator to walk through them.
    """
    from rp.prompt_toolkit.shortcuts import create_confirm_application
    assert isinstance(completions, list)

    # Get terminal dimensions.
    term_size = cli.output.get_size()
    term_width = term_size.columns
    term_height = term_size.rows

    # Calculate amount of required columns/rows for displaying the
    # completions. (Keep in mind that completions are displayed
    # alphabetically column-wise.)
    max_compl_width = min(term_width,
                          max(get_cwidth(c.text) for c in completions) + 1)
    column_count = max(1, term_width // max_compl_width)
    completions_per_page = column_count * (term_height - 1)
    page_count = int(math.ceil(len(completions) / float(completions_per_page)))

    # Note: math.ceil can return float on Python2.

    def display(page):
        # Display completions.
        page_completions = completions[page * completions_per_page:(page + 1) *
                                       completions_per_page]

        page_row_count = int(
            math.ceil(len(page_completions) / float(column_count)))
        page_columns = [
            page_completions[i * page_row_count:(i + 1) * page_row_count]
            for i in range(column_count)
        ]

        result = []
        for r in range(page_row_count):
            for c in range(column_count):
                try:
                    result.append(
                        page_columns[c][r].text.ljust(max_compl_width))
                except IndexError:
                    pass
            result.append('\n')
        cli.output.write(''.join(result))
        cli.output.flush()

    # User interaction through an application generator function.
    def run():
        if len(completions) > completions_per_page:
            # Ask confirmation if it doesn't fit on the screen.
            message = 'Display all {} possibilities? (y on n) '.format(
                len(completions))
            confirm = yield create_confirm_application(message)

            if confirm:
                # Display pages.
                for page in range(page_count):
                    display(page)

                    if page != page_count - 1:
                        # Display --MORE-- and go to the next page.
                        show_more = yield _create_more_application()
                        if not show_more:
                            return
            else:
                cli.output.write('\n')
                cli.output.flush()
        else:
            # Display all completions.
            display(0)

    cli.run_application_generator(run, render_cli_done=True)