Exemplo n.º 1
0
    def write_dl(self, rows, col_max=30, col_spacing=2):
        rows = list(rows)
        widths = measure_table(rows)
        if len(widths) != 2:
            raise TypeError("Expected two columns for definition list")

        first_col = min(widths[0], col_max) + col_spacing

        for first, second in iter_rows(rows, len(widths)):
            self.write("%*s%s" %
                       (self.current_indent, "", context.io.cyan(first)))
            if not second:
                self.write("\n")
                continue
            if term_len(first) <= first_col - col_spacing:
                self.write(" " * (first_col - term_len(first)))
            else:
                self.write("\n")
                self.write(" " * (first_col + self.current_indent))

            text_width = max(self.width - first_col - 2, 10)
            lines = iter(wrap_text(second, text_width).splitlines())
            if lines:
                self.write(next(lines) + "\n")
                for line in lines:
                    self.write("%*s%s\n" %
                               (first_col + self.current_indent, "", line))
            else:
                self.write("\n")
Exemplo n.º 2
0
    def write_dl(self, rows, col_max=30, col_spacing=2):  # noqa: D102
        rows = list(rows)
        widths = measure_table(rows)
        if len(widths) != 2:
            raise TypeError("Expected two columns for definition list")

        first_col = min(widths[0], col_max) + col_spacing

        for first, second in iter_rows(rows, len(widths)):
            self.write(f"{'':>{self.current_indent}}{first}")
            if not second:
                self.write("\n")
                continue
            if term_len(first) <= first_col - col_spacing:
                self.write(" " * (first_col - term_len(first)))
            else:
                self.write("\n")
                self.write(" " * (first_col + self.current_indent))

            text_width = max(self.width - first_col - 2, 10)
            wrapped_text = wrap_text(second,
                                     text_width,
                                     preserve_paragraphs=True)
            lines = wrapped_text.splitlines()

            if lines:
                self.write(f"{lines[0]}\n")

                for line in lines[1:]:
                    self.write(
                        f"{'':>{first_col + self.current_indent}}{line}\n")
            else:
                self.write("\n")
Exemplo n.º 3
0
    def format_options(self, ctx, formatter):
        def keyfunc(o):
            value = getattr(o, 'option_group', None)
            return value if value is not None else ''

        grouped_options = groupby(
            sorted(
                self.get_params(ctx),
                key=keyfunc,
            ),
            key=keyfunc,
        )

        options = {}
        for option_group, params in grouped_options:
            for param in params:
                rv = param.get_help_record(ctx)
                if rv is not None:
                    options.setdefault(option_group, []).append(rv)

        if options:
            widths_a, widths_b = list(
                zip(*[measure_table(group_options) for group_options in options.values()]),
            )
            widths = (max(widths_a), max(widths_b))

            for option_group, group_options in options.items():
                with formatter.section(option_group if option_group else 'Options'):
                    formatter.write_dl(group_options, widths=widths)
Exemplo n.º 4
0
Arquivo: cli.py Projeto: onyb/raiden
    def format_options(self, ctx, formatter):
        def keyfunc(o):
            value = getattr(o, 'option_group', None)
            return value if value is not None else ''

        grouped_options = groupby(
            sorted(
                self.get_params(ctx),
                key=keyfunc,
            ),
            key=keyfunc,
        )

        options = {}
        for option_group, params in grouped_options:
            for param in params:
                rv = param.get_help_record(ctx)
                if rv is not None:
                    options.setdefault(option_group, []).append(rv)

        if options:
            widths_a, widths_b = list(
                zip(*[
                    measure_table(group_options)
                    for group_options in options.values()
                ]), )
            widths = (max(widths_a), max(widths_b))

            for option_group, group_options in options.items():
                with formatter.section(
                        option_group if option_group else 'Options'):
                    formatter.write_dl(group_options, widths=widths)
Exemplo n.º 5
0
def display_rows(gandi, rows, has_header=True):
    col_len = measure_table(rows)
    formatting = ' | '.join(['%-' + str(l) + 's' for l in col_len])

    if has_header:
        header = rows.pop(0)
        gandi.echo(formatting % tuple(header))
        gandi.echo('-+-'.join(['-' * l for l in col_len]))

    for row in rows:
        gandi.echo(formatting % tuple(row))
Exemplo n.º 6
0
def display_rows(gandi, rows, has_header=True):
    col_len = measure_table(rows)
    formatting = ' | '.join(['%-' + str(l) + 's' for l in col_len])

    if has_header:
        header = rows.pop(0)
        gandi.echo(formatting % tuple(header))
        gandi.echo('-+-'.join(['-' * l for l in col_len]))

    for row in rows:
        gandi.echo(formatting % tuple(row))
Exemplo n.º 7
0
def display_rows(gandi, rows, has_header=True):
    col_len = measure_table(rows)
    formatting = " | ".join(["%-" + str(l) + "s" for l in col_len])

    if has_header:
        header = rows.pop(0)
        gandi.echo(formatting % tuple(header))
        gandi.echo("-+-".join(["-" * l for l in col_len]))

    for row in rows:
        gandi.echo(formatting % tuple(row))
Exemplo n.º 8
0
    def write_dl(self, rows, col_max=30, col_spacing=2, widths=None):
        """Writes a definition list into the buffer.  This is how options
        and commands are usually formatted.

        :param rows: a list of two item tuples for the terms and values.
        :param col_max: the maximum width of the first column.
        :param col_spacing: the number of spaces between the first and
                            second column.
        :param widths: optional pre-calculated line widths
        """
        rows = list(rows)
        if widths is None:
            widths = measure_table(rows)
        if len(widths) != 2:
            raise TypeError('Expected two columns for definition list')

        first_col = min(widths[0], col_max) + col_spacing

        for first, second in iter_rows(rows, len(widths)):
            self.write('%*s%s' % (self.current_indent, '', first))
            if not second:
                self.write('\n')
                continue
            if term_len(first) <= first_col - col_spacing:
                self.write(' ' * (first_col - term_len(first)))
            else:
                self.write('\n')
                self.write(' ' * (first_col + self.current_indent))

            text_width = max(self.width - first_col - 2, 10)
            lines = iter(wrap_text(second, text_width).splitlines())
            if lines:
                self.write(next(lines) + '\n')
                for line in lines:
                    self.write('%*s%s\n' % (
                        first_col + self.current_indent,
                        '',
                        line,
                    ))
            else:
                self.write('\n')
Exemplo n.º 9
0
    def write_dl(self, rows, col_max=30, col_spacing=2, widths=None):
        """Writes a definition list into the buffer.  This is how options
        and commands are usually formatted.

        :param rows: a list of two item tuples for the terms and values.
        :param col_max: the maximum width of the first column.
        :param col_spacing: the number of spaces between the first and
                            second column.
        :param widths: optional pre-calculated line widths
        """
        rows = list(rows)
        if widths is None:
            widths = measure_table(rows)
        if len(widths) != 2:
            raise TypeError('Expected two columns for definition list')

        first_col = min(widths[0], col_max) + col_spacing

        for first, second in iter_rows(rows, len(widths)):
            self.write('%*s%s' % (self.current_indent, '', first))
            if not second:
                self.write('\n')
                continue
            if term_len(first) <= first_col - col_spacing:
                self.write(' ' * (first_col - term_len(first)))
            else:
                self.write('\n')
                self.write(' ' * (first_col + self.current_indent))

            text_width = max(self.width - first_col - 2, 10)
            lines = iter(wrap_text(second, text_width).splitlines())
            if lines:
                self.write(next(lines) + '\n')
                for line in lines:
                    self.write('%*s%s\n' % (
                        first_col + self.current_indent, '', line))
            else:
                self.write('\n')