Skip to content
Myr

Exposed formatting

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 373d92d · Browse files · 4 files+75 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+75 added−20 removed

  • other:+75 added−20 removed

4 files+75 added−20 removed

modified:click/__init__.py+6 added
30 unmodified lines
3132# Exceptions33from .exceptions import UsageError, Abort3435# Formatting36from .formatting import HelpFormatter, wrap_text373839__all__ = [40 # Core classes41 'Context', 'Command', 'MultiCommand', 'Group', 'CommandCollection',42 'Parameter', 'Option', 'Argument',
10 unmodified lines
53 'prompt', 'confirm', 'get_terminal_size', 'echo',5455 # Exceptions56 'UsageError', 'Abort',5758 # Formatting59 'HelpFormatter', 'wrap_text',60]
modified:click/formatting.py+60 added-19 removed
94 unmodified lines
95 return '\n\n'.join(rv)969798class HelpFormatter(object):99 """This class helps with formatting text based help pages."""99 """This class helps with formatting text based help pages. It's100 usually just needed for very special internal cases but it's also101 exposed so that developers can write their own fancy outputs.102103 At present it always writes into memory.104105 :param indent_increment: the additional increment for each level.106 :param width: the width for the text. This defaults to the terminal107 width clamped to a maximum of 78.108 """109110 def __init__(self, indent_increment=2, width=None):111 self.indent_increment = indent_increment112 if width is None:113 width = min(get_terminal_size()[0], 80) - 2114 self.width = width115 self.current_indent = 0116 self.buffer = []117118 def write(self, string):119 """Writes a unicode string into the internal buffer."""120 self.buffer.append(string)121122 def indent(self):123 """Increases the indentation."""124 self.current_indent += self.indent_increment125126 def dedent(self):127 """Decreases the indentation."""128 self.current_indent -= self.indent_increment129130 def write_usage(self, prog, args='', prefix='Usage: '):131 """Writes a usage line into the buffer.132133 :param prog: the program name.134 :param args: whitespace separated list of arguments.135 :param prefix: the prefix for the first line.136 """137 prefix = '%*s%s' % (self.current_indent, prefix, prog)117 self.buffer.append(prefix)138 self.write(prefix)139140 text_width = max(self.width - self.current_indent - len(prefix), 10)141 indent = ' ' * (len(prefix) + 1)121 self.buffer.append(wrap_text(args, text_width,122 initial_indent=' ',123 subsequent_indent=indent))142 self.write(wrap_text(args, text_width,143 initial_indent=' ',144 subsequent_indent=indent))145125 self.buffer.append('\n')146 self.write('\n')147148 def write_heading(self, heading):149 """Writes a heading into the buffer."""128 self.buffer.append('%*s%s:\n' % (self.current_indent, '', heading))150 self.write('%*s%s:\n' % (self.current_indent, '', heading))151152 def write_paragraph(self):153 """Writes a paragraph into the buffer."""154 if self.buffer:132 self.buffer.append('\n')155 self.write('\n')156157 def write_text(self, text):158 """Writes re-indented text into the buffer. This rewraps and159 preserves paragraphs.160 """161 text_width = max(self.width - self.current_indent, 11)162 indent = ' ' * self.current_indent137 self.buffer.append(wrap_text(text, text_width,138 initial_indent=indent,139 subsequent_indent=indent,140 preserve_paragraphs=True))141 self.buffer.append('\n')163 self.write(wrap_text(text, text_width,164 initial_indent=indent,165 subsequent_indent=indent,166 preserve_paragraphs=True))167 self.write('\n')168169 def write_dl(self, rows, col_max=20, col_spacing=2):170 """Writes a definition list into the buffer. This is how options171 and commands are usually formatted.172173 :param rows: a list of two item tuples for the terms and values.174 :param col_max: the maximum width of the first column.175 :param col_spacing: the number of spaces between the first and176 second column.177 """178 rows = list(rows)179 widths = measure_table(rows)180 if len(widths) != 2:181 raise TypeError('Expected two columns for definition list')182183 first_col = min(widths[0], col_max) + col_spacing184185 for first, second in iter_rows(rows, len(widths)):152 self.buffer.append('%*s%s' % (self.current_indent, '', first))186 self.write('%*s%s' % (self.current_indent, '', first))187 if len(first) <= first_col:154 self.buffer.append(' ' * (first_col - len(first)))188 self.write(' ' * (first_col - len(first)))189 else:156 self.buffer.append('\n')157 self.buffer.append(' ' * (first_col + self.current_indent))190 self.write('\n')191 self.write(' ' * (first_col + self.current_indent))192 text_width = self.width - first_col - 2159 self.buffer.append(wrap_text(193 self.write(wrap_text(194 second, text_width, initial_indent='',195 subsequent_indent=' ' * (first_col + self.current_indent)))162 self.buffer.append('\n')196 self.write('\n')197198 @contextmanager199 def section(self, name):200 """Helpful context manager that writes a paragraph, a heading201 and the indents.202203 :param name: the section name taht is written as heading.204 """205 self.write_paragraph()206 self.write_heading(name)207 self.indent()208 try:
2 unmodified lines
211 self.dedent()212213 @contextmanager214 def indentation(self):215 """A context manager that increases the indentation."""216 self.indent()217 try:218 yield219 finally:220 self.dedent()221222 def getvalue(self):223 """Returns the buffer contents."""224 return ''.join(self.buffer)
modified:docs/api.rst+8 added
94 unmodified lines
9596.. autoexception:: Abort9798.. autoexception:: UsageError99100Formatting101----------102103.. autoclass:: HelpFormatter104 :members:105106.. autofunction:: wrap_text
modified:LICENSE+1 added-1 removed
1Copyright (c) 2014 by Armin Ronacher.23Click uses parts of optparse written by Gregory P. Ward and maintained by the4Python software foundation. This is limited to code in the _optparse.py4Python software foundation. This is limited to code in the parser.py5module:67Copyright (c) 2001-2006 Gregory P. Ward. All rights reserved.8Copyright (c) 2002-2006 Python Software Foundation. All rights reserved.
30 unmodified lines