Skip to content

Commit

Permalink
Strip ANSI escape codes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTereshenkov authored and stephenfin committed Mar 19, 2021
1 parent de245b5 commit 483fcca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
9 changes: 5 additions & 4 deletions sphinx_click/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def _format_option(opt):
if opt[1]:
yield ''
for line in statemachine.string2lines(
opt[1], tab_width=4, convert_whitespace=True
ANSI_ESC_SEQ_RE.sub('', opt[1]), tab_width=4, convert_whitespace=True
):
yield _indent(line)

Expand Down Expand Up @@ -241,12 +241,13 @@ def _format_epilog(ctx):
We parse this as reStructuredText, allowing users to embed rich
information in their help messages if they so choose.
"""
epilog_string = ctx.command.epilog
if not epilog_string:
if not ctx.command.epilog:
return

for line in statemachine.string2lines(
epilog_string, tab_width=4, convert_whitespace=True
ANSI_ESC_SEQ_RE.sub('', ctx.command.epilog),
tab_width=4,
convert_whitespace=True,
):
yield line
yield ''
Expand Down
36 changes: 35 additions & 1 deletion tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,23 @@ def hello(name):
def test_ansi_escape_sequences(self):
"""Validate that ANSI escape sequences are stripped."""

@click.command()
@click.command(epilog='\033[31mA sample epilog.\033[0m')
@click.option(
'--name',
help='Name to say \033[94mhello\033[0m to.',
required=True,
type=str,
)
@click.option(
'--choice',
help='A sample option with choices',
type=click.Choice(['\033[94mOption1\033[0m', '\033[94mOption2\033[0m']),
)
@click.option(
'--param',
default=lambda: None,
show_default='Something computed at \033[94mruntime\033[0m',
)
def foobar():
"""A sample command with **sparkles**.
Expand All @@ -293,6 +309,24 @@ def foobar():
.. code-block:: shell
foobar [OPTIONS]
.. rubric:: Options
.. option:: --name <name>
**Required** Name to say hello to.
.. option:: --choice <choice>
A sample option with choices
:options: Option1 | Option2
.. option:: --param <param>
:default: Something computed at runtime
A sample epilog.
"""
).lstrip(),
'\n'.join(output),
Expand Down

0 comments on commit 483fcca

Please sign in to comment.