Improve help text formatting.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Clean up the code (no need for separate function to add subsequent indents). When the command is too long for the arguments to fit on the right of the command help text, wrap them to the next line at a reduced indent.
authored by Mark Thomas · committed by Mark Thomas · parent f4961a8 · Browse files · 2 files·+56 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+56 added−21 removed
- tests:+37 added−1 removed
- other:+19 added−20 removed
Provenance
- Commit
d8a7cea- Tree
937f6c2- Author
Mark Thomas <markbt@efaref.net>- Committer
Mark Thomas <markbt@efaref.net>- Parent
f4961a8
2 files changed+56 added−21 removed
modified:click/formatting.py+19 added-20 removed
16 unmodified lines17 row = tuple(row)18 yield row + ('',) * (col_count - len(row))19 20 21-def add_subsequent_indent(text, subsequent_indent):22- lines = text.splitlines()23- lines = lines[:1] + [subsequent_indent + line for line in lines[1:]]24- return '\n'.join(lines)25-26-21 def wrap_text(text, width=78, initial_indent='', subsequent_indent='',22 preserve_paragraphs=False):23 """A helper function that intelligently wraps text. By default, it24 assumes that it operates on a single paragraph of text but if the14 unmodified lines39 intelligently handle paragraphs.40 """41 from ._textwrap import TextWrapper42 text = text.expandtabs()49- post_wrap_indent = subsequent_indent[:-1]50- subsequent_indent = subsequent_indent[-1:]43 wrapper = TextWrapper(width, initial_indent=initial_indent,44 subsequent_indent=subsequent_indent,45 replace_whitespace=False)46 if not preserve_paragraphs:55- return add_subsequent_indent(wrapper.fill(text), post_wrap_indent)47+ return wrapper.fill(text)48 49 p = []50 buf = []51 indent = None22 unmodified lines74 rv = []75 for indent, raw, text in p:76 with wrapper.extra_indent(' ' * indent):77 if raw:86- rv.append(add_subsequent_indent(wrapper.indent_only(text),87- post_wrap_indent))78+ rv.append(wrapper.indent_only(text))79 else:89- rv.append(add_subsequent_indent(wrapper.fill(text),90- post_wrap_indent))80+ rv.append(wrapper.fill(text))81 82 return '\n\n'.join(rv)83 84 37 unmodified lines122 :param prog: the program name.123 :param args: whitespace separated list of arguments.124 :param prefix: the prefix for the first line.125 """136- prefix = '%*s%s' % (self.current_indent, prefix, prog)137- self.write(prefix)126+ prefix = '%*s%s ' % (self.current_indent, prefix, prog)127+ text_width = self.width - self.current_indent128 129+ if text_width >= (term_len(prefix) + 20):130+ # The arguments will fit to the right of the prefix.131+ indent = ' ' * term_len(prefix)132+ self.write(wrap_text(args, text_width,133+ initial_indent=prefix,134+ subsequent_indent=indent))135+ else:136+ # The prefix is too long, put the arguments on the next line.137+ self.write(prefix)139- text_width = max(self.width - self.current_indent - term_len(prefix), 10)140- indent = ' ' * (term_len(prefix) + 1)141- self.write(wrap_text(args, text_width,142- initial_indent=' ',143- subsequent_indent=indent))138+ self.write('\n')139+ indent = ' ' * (self.current_indent + 12)140+ self.write(wrap_text(args, text_width,141+ initial_indent=indent,142+ subsequent_indent=indent))143 144 self.write('\n')145 146 def write_heading(self, heading):104 unmodified linesmodified:tests/test_formatting.py+37 added-1 removed
70 unmodified lines71 def command():72 """A command.73 """74 75- # 54 is chosen as a lenthg where the second line is one character75+ # 54 is chosen as a length where the second line is one character76 # longer than the maximum length.77 result = runner.invoke(cli, ['a_very_long', 'command', '--help'],78 terminal_width=54)79 assert not result.exception8 unmodified lines88 ' --help Show this message and exit.',89 ]90 91 92+def test_wrapping_long_command_name(runner):93+ @click.group()94+ def cli():95+ """Top level command96+ """97+98+ @cli.group()99+ def a_very_very_very_long():100+ """Second level101+ """102+103+ @a_very_very_very_long.command()104+ @click.argument('first')105+ @click.argument('second')106+ @click.argument('third')107+ @click.argument('fourth')108+ @click.argument('fifth')109+ @click.argument('sixth')110+ def command():111+ """A command.112+ """113+114+ result = runner.invoke(cli, ['a_very_very_very_long', 'command', '--help'],115+ terminal_width=54)116+ assert not result.exception117+ assert result.output.splitlines() == [118+ 'Usage: cli a_very_very_very_long command ',119+ ' [OPTIONS] FIRST SECOND THIRD FOURTH FIFTH',120+ ' SIXTH',121+ '',122+ ' A command.',123+ '',124+ 'Options:',125+ ' --help Show this message and exit.',126+ ]127+128 def test_formatting_empty_help_lines(runner):129 @click.command()130 def cli():131 """Top level command15 unmodified lines