Merge pull request #359 from markbt/help-formatting
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Improve help text formatting.
authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents d65f32f, c904e5d · Browse files · 3 files·+59 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
3 files+59 added−21 removed
- tests:+38 added−1 removed
- other:+21 added−20 removed
Provenance
- Commit
8056132- Tree
481d08f- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
d65f32f- Parent 2
c904e5d
3 files changed+59 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+ usage_prefix = '%*s%s ' % (self.current_indent, prefix, prog)127+ text_width = self.width - self.current_indent128 129+ if text_width >= (term_len(usage_prefix) + 20):130+ # The arguments will fit to the right of the prefix.131+ indent = ' ' * term_len(usage_prefix)132+ self.write(wrap_text(args, text_width,133+ initial_indent=usage_prefix,134+ subsequent_indent=indent))135+ else:136+ # The prefix is too long, put the arguments on the next line.137+ self.write(usage_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 = ' ' * (max(self.current_indent, term_len(prefix)) + 4)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+38 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+129 def test_formatting_empty_help_lines(runner):130 @click.command()131 def cli():132 """Top level command15 unmodified linesmodified:CHANGES+2 added
8 unmodified lines9 (codename to be decided, release date to be determined)10 11 - Removed various deprecated functionality.12 - Atomic files now only accept the `w` mode.13+- Change the usage part of help output for very long commands to wrap14+ their arguments onto the next line, indented by 4 spaces.15 16 Version 4.117 -----------18 219 unmodified lines