Skip to content
43 changes: 43 additions & 0 deletions tests/test_rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,46 @@ def main(bar: str):
result = runner.invoke(app, ["--help"])
assert "Usage" in result.stdout
assert "BAR" in result.stdout


def test_make_rich_text_with_ansi_escape_sequences():
from typer.rich_utils import Text, _make_rich_text

ansi_text = "This is \x1b[4munderlined\x1b[0m text"
result = _make_rich_text(text=ansi_text, markup_mode=None)

assert isinstance(result, Text)
assert "\x1b[" not in result.plain
assert "underlined" in result.plain

mixed_text = "Start \x1b[31mred\x1b[0m middle \x1b[32mgreen\x1b[0m end"
result = _make_rich_text(text=mixed_text, markup_mode=None)
assert isinstance(result, Text)
assert "\x1b[" not in result.plain
assert "red" in result.plain
assert "green" in result.plain

fake_ansi = "This contains \x1b[ but not a complete sequence"
result = _make_rich_text(text=fake_ansi, markup_mode=None)
assert isinstance(result, Text)
assert "\x1b[" not in result.plain
assert "This contains " in result.plain


def test_make_rich_text_with_typer_style_in_help():
app = typer.Typer()

@app.command()
def example(
a: str = typer.Option(help="This is A"),
b: str = typer.Option(help=f"This is {typer.style('B', underline=True)}"),
):
"""Example command with styled help text."""
pass # pragma: no cover

result = runner.invoke(app, ["--help"])

assert result.exit_code == 0
assert "This is A" in result.stdout
assert "This is B" in result.stdout
assert "\x1b[" not in result.stdout
6 changes: 5 additions & 1 deletion typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ def _make_rich_text(
if markup_mode == MARKUP_MODE_RICH:
return highlighter(Text.from_markup(text, style=style))
else:
return highlighter(Text(text, style=style))
ANSI_ESCAPE_SEQUENCE = "\x1b["
if ANSI_ESCAPE_SEQUENCE in text:
return highlighter(Text.from_ansi(text, style=style))
else:
return highlighter(Text(text, style=style))


@group()
Expand Down
Loading