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
Provenance
- Commit
97b505c- Tree
ddfc54c- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
373d92d
4 files changed+75 added−20 removed
modified:click/__init__.py+6 added
30 unmodified lines31 32 # Exceptions33 from .exceptions import UsageError, Abort34 35+# Formatting36+from .formatting import HelpFormatter, wrap_text37 38+39 __all__ = [40 # Core classes41 'Context', 'Command', 'MultiCommand', 'Group', 'CommandCollection',42 'Parameter', 'Option', 'Argument',10 unmodified lines53 'prompt', 'confirm', 'get_terminal_size', 'echo',54 55 # Exceptions56 'UsageError', 'Abort',57+58+ # Formatting59+ 'HelpFormatter', 'wrap_text',60 ]modified:click/formatting.py+60 added-19 removed
94 unmodified lines95 return '\n\n'.join(rv)96 97 98 class 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.102 103+ At present it always writes into memory.104+105+ :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+ """109+110 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 = []117 118+ def write(self, string):119+ """Writes a unicode string into the internal buffer."""120+ self.buffer.append(string)121+122 def indent(self):123+ """Increases the indentation."""124 self.current_indent += self.indent_increment125 126 def dedent(self):127+ """Decreases the indentation."""128 self.current_indent -= self.indent_increment129 130 def write_usage(self, prog, args='', prefix='Usage: '):131+ """Writes a usage line into the buffer.132+133+ :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)139 140 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))145 125- self.buffer.append('\n')146+ self.write('\n')147 148 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))151 152 def write_paragraph(self):153+ """Writes a paragraph into the buffer."""154 if self.buffer:132- self.buffer.append('\n')155+ self.write('\n')156 157 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')168 169 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.172+173+ :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')182 183 first_col = min(widths[0], col_max) + col_spacing184 185 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')197 198 @contextmanager199 def section(self, name):200+ """Helpful context manager that writes a paragraph, a heading201+ and the indents.202+203+ :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 lines211 self.dedent()212 213 @contextmanager214 def indentation(self):215+ """A context manager that increases the indentation."""216 self.indent()217 try:218 yield219 finally:220 self.dedent()221 222 def getvalue(self):223+ """Returns the buffer contents."""224 return ''.join(self.buffer)modified:docs/api.rst+8 added
94 unmodified lines95 96 .. autoexception:: Abort97 98 .. autoexception:: UsageError99+100+Formatting101+----------102+103+.. autoclass:: HelpFormatter104+ :members:105+106+.. autofunction:: wrap_textmodified:LICENSE+1 added-1 removed
1 Copyright (c) 2014 by Armin Ronacher.2 3 Click uses parts of optparse written by Gregory P. Ward and maintained by the4-Python software foundation. This is limited to code in the _optparse.py4+Python software foundation. This is limited to code in the parser.py5 module:6 7 Copyright (c) 2001-2006 Gregory P. Ward. All rights reserved.8 Copyright (c) 2002-2006 Python Software Foundation. All rights reserved.30 unmodified lines