Skip to content
Myr

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

2 files+52 added−3 removed

modified:click/formatting.py+13 added-3 removed
16 unmodified lines
17 row = tuple(row)18 yield row + ('',) * (col_count - len(row))192021def 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)252627def 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 the
14 unmodified lines
45 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)5657 p = []58 buf = []59 indent = None
22 unmodified lines
82 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))9192 return '\n\n'.join(rv)9394
157 unmodified lines
modified:tests/test_formatting.py+39 added
46 unmodified lines
47 '',48 'Options:',49 ' --help Show this message and exit.',50 ]515253def test_wrapping_long_options_strings(runner):54 @click.group()55 def cli():56 """Top level command57 """5859 @cli.group()60 def a_very_long():61 """Second level62 """6364 @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 """7475 # 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 ]