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")
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")
def write_usage(self, prog, args='', prefix='Usage: '): """Writes a usage line into the buffer. :param prog: the program name. :param args: whitespace separated list of arguments. :param prefix: the prefix for the first line. """ usage_prefix = message_cyan('%*s%s ' % (self.current_indent, prefix, prog)) text_width = self.width - self.current_indent if text_width >= (term_len(usage_prefix) + 20): # The arguments will fit to the right of the prefix. indent = ' ' * term_len(usage_prefix) self.write( wrap_text(args, text_width, initial_indent=usage_prefix, subsequent_indent=indent)) else: # The prefix is too long, put the arguments on the next line. self.write(usage_prefix) self.write('\n') indent = ' ' * (max(self.current_indent, term_len(prefix)) + 4) self.write( wrap_text(args, text_width, initial_indent=indent, subsequent_indent=indent))
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')
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')