Merge pull request #240 from sjagoe/fix-help-wrapping
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix usage wrapping on narrow terminals
authored by Armin Ronacher · committed by Armin Ronacher · parents 438430d, e2c3b9b · Browse files · 2 files·+52 added−3 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+52 added−3 removed
- tests:+39 added−0 removed
- other:+13 added−3 removed
Provenance
- Commit
c2c2bac- Tree
cab2444- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent 1
438430d- Parent 2
e2c3b9b
2 files changed+52 added−3 removed
modified:click/formatting.py+13 added-3 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[0]] + [subsequent_indent + line for line in lines[1:]]24+ return '\n'.join(lines)25+26+27 def wrap_text(text, width=78, initial_indent='', subsequent_indent='',28 preserve_paragraphs=False):29 """A helper function that intelligently wraps text. By default, it30 assumes that it operates on a single paragraph of text but if the14 unmodified lines45 intelligently handle paragraphs.46 """47 from ._textwrap import TextWrapper48 text = text.expandtabs()49+ post_wrap_indent = subsequent_indent[:-1]50+ subsequent_indent = subsequent_indent[-1:]51 wrapper = TextWrapper(width, initial_indent=initial_indent,52 subsequent_indent=subsequent_indent,53 replace_whitespace=False)54 if not preserve_paragraphs:47- return wrapper.fill(text)55+ return add_subsequent_indent(wrapper.fill(text), post_wrap_indent)56 57 p = []58 buf = []59 indent = None22 unmodified lines82 rv = []83 for indent, raw, text in p:84 with wrapper.extra_indent(' ' * indent):85 if raw:78- rv.append(wrapper.indent_only(text))86+ rv.append(add_subsequent_indent(wrapper.indent_only(text),87+ post_wrap_indent))88 else:80- rv.append(wrapper.fill(text))89+ rv.append(add_subsequent_indent(wrapper.fill(text),90+ post_wrap_indent))91 92 return '\n\n'.join(rv)93 94 157 unmodified linesmodified:tests/test_formatting.py+39 added
46 unmodified lines47 '',48 'Options:',49 ' --help Show this message and exit.',50 ]51+52+53+def test_wrapping_long_options_strings(runner):54+ @click.group()55+ def cli():56+ """Top level command57+ """58+59+ @cli.group()60+ def a_very_long():61+ """Second level62+ """63+64+ @a_very_long.command()65+ @click.argument('first')66+ @click.argument('second')67+ @click.argument('third')68+ @click.argument('fourth')69+ @click.argument('fifth')70+ @click.argument('sixth')71+ def command():72+ """A command.73+ """74+75+ # 54 is chosen as a lenthg 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.exception80+ assert result.output.splitlines() == [81+ 'Usage: cli a_very_long command [OPTIONS] FIRST SECOND',82+ ' THIRD FOURTH FIFTH',83+ ' SIXTH',84+ '',85+ ' A command.',86+ '',87+ 'Options:',88+ ' --help Show this message and exit.',89+ ]