summaryrefslogtreecommitdiff
path: root/src/click/formatting.py
diff options
context:
space:
mode:
authorDavid Lord <davidism@gmail.com>2020-03-06 13:50:04 -0800
committerDavid Lord <davidism@gmail.com>2020-03-06 13:50:04 -0800
commit93ba3ba112d2f8ba7bdd8b231e510f74dd0b037e (patch)
tree409b93ee7ec2209b9e52256ed77b0292f4e49720 /src/click/formatting.py
parent488739dfe0d51f415c7b20466648cc519962ecbb (diff)
downloadclick-93ba3ba112d2f8ba7bdd8b231e510f74dd0b037e.tar.gz
apply black
Diffstat (limited to 'src/click/formatting.py')
-rw-r--r--src/click/formatting.py103
1 files changed, 60 insertions, 43 deletions
diff --git a/src/click/formatting.py b/src/click/formatting.py
index 4426d9c..de90d6b 100644
--- a/src/click/formatting.py
+++ b/src/click/formatting.py
@@ -19,11 +19,12 @@ def measure_table(rows):
def iter_rows(rows, col_count):
for row in rows:
row = tuple(row)
- yield row + ('',) * (col_count - len(row))
+ yield row + ("",) * (col_count - len(row))
-def wrap_text(text, width=78, initial_indent='', subsequent_indent='',
- preserve_paragraphs=False):
+def wrap_text(
+ text, width=78, initial_indent="", subsequent_indent="", preserve_paragraphs=False
+):
"""A helper function that intelligently wraps text. By default, it
assumes that it operates on a single paragraph of text but if the
`preserve_paragraphs` parameter is provided it will intelligently
@@ -43,10 +44,14 @@ def wrap_text(text, width=78, initial_indent='', subsequent_indent='',
intelligently handle paragraphs.
"""
from ._textwrap import TextWrapper
+
text = text.expandtabs()
- wrapper = TextWrapper(width, initial_indent=initial_indent,
- subsequent_indent=subsequent_indent,
- replace_whitespace=False)
+ wrapper = TextWrapper(
+ width,
+ initial_indent=initial_indent,
+ subsequent_indent=subsequent_indent,
+ replace_whitespace=False,
+ )
if not preserve_paragraphs:
return wrapper.fill(text)
@@ -57,10 +62,10 @@ def wrap_text(text, width=78, initial_indent='', subsequent_indent='',
def _flush_par():
if not buf:
return
- if buf[0].strip() == '\b':
- p.append((indent or 0, True, '\n'.join(buf[1:])))
+ if buf[0].strip() == "\b":
+ p.append((indent or 0, True, "\n".join(buf[1:])))
else:
- p.append((indent or 0, False, ' '.join(buf)))
+ p.append((indent or 0, False, " ".join(buf)))
del buf[:]
for line in text.splitlines():
@@ -77,13 +82,13 @@ def wrap_text(text, width=78, initial_indent='', subsequent_indent='',
rv = []
for indent, raw, text in p:
- with wrapper.extra_indent(' ' * indent):
+ with wrapper.extra_indent(" " * indent):
if raw:
rv.append(wrapper.indent_only(text))
else:
rv.append(wrapper.fill(text))
- return '\n\n'.join(rv)
+ return "\n\n".join(rv)
class HelpFormatter(object):
@@ -122,53 +127,65 @@ class HelpFormatter(object):
"""Decreases the indentation."""
self.current_indent -= self.indent_increment
- def write_usage(self, prog, args='', prefix='Usage: '):
+ 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 = '%*s%s ' % (self.current_indent, prefix, prog)
+ usage_prefix = "%*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))
+ 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))
+ 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
+ )
+ )
- self.write('\n')
+ self.write("\n")
def write_heading(self, heading):
"""Writes a heading into the buffer."""
- self.write('%*s%s:\n' % (self.current_indent, '', heading))
+ self.write("%*s%s:\n" % (self.current_indent, "", heading))
def write_paragraph(self):
"""Writes a paragraph into the buffer."""
if self.buffer:
- self.write('\n')
+ self.write("\n")
def write_text(self, text):
"""Writes re-indented text into the buffer. This rewraps and
preserves paragraphs.
"""
text_width = max(self.width - self.current_indent, 11)
- indent = ' ' * self.current_indent
- self.write(wrap_text(text, text_width,
- initial_indent=indent,
- subsequent_indent=indent,
- preserve_paragraphs=True))
- self.write('\n')
+ indent = " " * self.current_indent
+ self.write(
+ wrap_text(
+ text,
+ text_width,
+ initial_indent=indent,
+ subsequent_indent=indent,
+ preserve_paragraphs=True,
+ )
+ )
+ self.write("\n")
def write_dl(self, rows, col_max=30, col_spacing=2):
"""Writes a definition list into the buffer. This is how options
@@ -182,36 +199,36 @@ class HelpFormatter(object):
rows = list(rows)
widths = measure_table(rows)
if len(widths) != 2:
- raise TypeError('Expected two columns for definition list')
+ 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))
+ self.write("%*s%s" % (self.current_indent, "", first))
if not second:
- self.write('\n')
+ self.write("\n")
continue
if term_len(first) <= first_col - col_spacing:
- self.write(' ' * (first_col - term_len(first)))
+ self.write(" " * (first_col - term_len(first)))
else:
- self.write('\n')
- self.write(' ' * (first_col + self.current_indent))
+ 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(lines[0] + '\n')
+ self.write(lines[0] + "\n")
for line in lines[1:]:
- self.write('%*s%s\n' % (first_col + self.current_indent, '', line))
+ self.write("%*s%s\n" % (first_col + self.current_indent, "", line))
if len(lines) > 1:
# separate long help from next option
self.write("\n")
else:
- self.write('\n')
+ self.write("\n")
@contextmanager
def section(self, name):
@@ -239,7 +256,7 @@ class HelpFormatter(object):
def getvalue(self):
"""Returns the buffer contents."""
- return ''.join(self.buffer)
+ return "".join(self.buffer)
def join_options(options):
@@ -252,11 +269,11 @@ def join_options(options):
any_prefix_is_slash = False
for opt in options:
prefix = split_opt(opt)[0]
- if prefix == '/':
+ if prefix == "/":
any_prefix_is_slash = True
rv.append((len(prefix), opt))
rv.sort(key=lambda x: x[0])
- rv = ', '.join(x[1] for x in rv)
+ rv = ", ".join(x[1] for x in rv)
return rv, any_prefix_is_slash