Skip to content

Commit

Permalink
Add support for wrapping suppression using \b in epilogs
Browse files Browse the repository at this point in the history
  • Loading branch information
mthuurne authored and stephenfin committed Jan 27, 2022
1 parent 38b1efc commit 69da77c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
7 changes: 7 additions & 0 deletions sphinx_click/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,18 @@ def _format_epilog(ctx):
if not ctx.command.epilog:
return

bar_enabled = False
for line in statemachine.string2lines(
ANSI_ESC_SEQ_RE.sub('', ctx.command.epilog),
tab_width=4,
convert_whitespace=True,
):
if line == '\b':
bar_enabled = True
continue
if line == '':
bar_enabled = False
line = '| ' + line if bar_enabled else line
yield line
yield ''

Expand Down
46 changes: 46 additions & 0 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,52 @@ def cli():
'\n'.join(output),
)

def test_no_line_wrapping_epilog(self):
r"""Validate behavior of the \b character in an epilog."""

@click.command(
epilog="""
An epilog containing pre-wrapped text.
\b
This is
a paragraph
without rewrapping.
And this is a paragraph
that will be rewrapped again.
"""
)
def foobar():
"""A sample command."""

ctx = click.Context(foobar, info_name='foobar')
output = list(ext._format_command(ctx, nested='short'))

self.assertEqual(
textwrap.dedent(
"""
A sample command.
.. program:: foobar
.. code-block:: shell
foobar [OPTIONS]
An epilog containing pre-wrapped text.
| This is
| a paragraph
| without rewrapping.
And this is a paragraph
that will be rewrapped again.
"""
).lstrip(),
'\n'.join(output),
)


class NestedCommandsTestCase(unittest.TestCase):
"""Validate ``click.Command`` instances inside ``click.Group`` instances."""
Expand Down

0 comments on commit 69da77c

Please sign in to comment.