Skip to content

Commit

Permalink
Merge pull request #1872 from janLuke/fix/formatter_write_text
Browse files Browse the repository at this point in the history
Fix HelpFormatter.write_text() to use the full line width
  • Loading branch information
davidism authored May 11, 2021
2 parents 5215fc1 + dcd991d commit ac655f8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ Unreleased
- When defining a parameter, ``default`` is validated with
``multiple`` and ``nargs``. More validation is done for values being
processed as well. :issue:`1806`
- ``HelpFormatter.write_text`` uses the full line width when wrapping
text. :issue:`1871`


Version 7.1.2
Expand Down
3 changes: 1 addition & 2 deletions src/click/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,11 @@ def write_text(self, text: str) -> None:
"""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,
self.width,
initial_indent=indent,
subsequent_indent=indent,
preserve_paragraphs=True,
Expand Down
10 changes: 10 additions & 0 deletions tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,13 @@ def test_formatting_with_options_metavar_empty(runner):
cli = click.Command("cli", options_metavar="", params=[click.Argument(["var"])])
result = runner.invoke(cli, ["--help"])
assert "Usage: cli VAR\n" in result.output


def test_help_formatter_write_text():
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
formatter = click.HelpFormatter(width=len(" Lorem ipsum dolor sit amet,"))
formatter.current_indent = 2
formatter.write_text(text)
actual = formatter.getvalue()
expected = " Lorem ipsum dolor sit amet,\n consectetur adipiscing elit\n"
assert actual == expected

0 comments on commit ac655f8

Please sign in to comment.