Skip to content

Commit

Permalink
Strip ANSI escape codes
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Finucane <stephen@that.guru>
Fixes: #77
  • Loading branch information
stephenfin committed Mar 18, 2021
1 parent c4e6d71 commit de245b5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions sphinx_click/ext.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import traceback
import warnings

Expand All @@ -16,6 +17,8 @@
NESTED_SHORT = 'short'
NESTED_NONE = 'none'

ANSI_ESC_SEQ_RE = re.compile(r'\x1B\[\d+(;\d+){0,2}m', flags=re.MULTILINE)


def _indent(text, level=1):
prefix = ' ' * (4 * level)
Expand Down Expand Up @@ -109,6 +112,8 @@ def _format_description(ctx):
if not help_string:
return

help_string = ANSI_ESC_SEQ_RE.sub('', help_string)

bar_enabled = False
for line in statemachine.string2lines(
help_string, tab_width=4, convert_whitespace=True
Expand Down
32 changes: 32 additions & 0 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,38 @@ def hello(name):
'\n'.join(output),
)

def test_ansi_escape_sequences(self):
"""Validate that ANSI escape sequences are stripped."""

@click.command()
def foobar():
"""A sample command with **sparkles**.
We've got \033[31mred text\033[0m, \033[104mblue backgrounds\033[0m, a
dash of \033[1mbold\033[0m and even some \033[4munderlined words\033[0m.
"""
pass

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

self.assertEqual(
textwrap.dedent(
"""
A sample command with **sparkles**.
We've got red text, blue backgrounds, a
dash of bold and even some underlined words.
.. program:: foobar
.. code-block:: shell
foobar [OPTIONS]
"""
).lstrip(),
'\n'.join(output),
)


class GroupTestCase(unittest.TestCase):
"""Validate basic ``click.Group`` instances."""
Expand Down

0 comments on commit de245b5

Please sign in to comment.