Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions tests/test_rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,45 @@ def test_metavar_highlighter(input_text: str):
assert str(closing_bracket_style) == STYLE_TYPES_SEPARATOR


@needs_rich
@pytest.mark.parametrize(
("text", "expected"),
[
# Metavars keep their highlighting.
("<int>", ["<int>"]),
("<TEXT>", ["<TEXT>"]),
("x <int> y", ["<int>"]),
("[default: <none>]", ["<none>"]),
("<a b>", ["<a b>"]),
# Comparison operators in help text are not metavars.
("Use only if x < 5 and y > 2", []),
("compare a < b or c > d", []),
("if x <= 5 then y >= 2", []),
# Nothing to highlight without any content between the brackets.
("<>", []),
("< >", []),
("< int>", []),
("<int >", []),
],
)
def test_option_highlighter_types_ignores_comparison_operators(
text: str, expected: list
):
"""
`<...>` is only a metavar when it has no padding inside the brackets, so help
text such as `x < 5 and y > 2` is left alone instead of being highlighted.
"""
from typer.rich_utils import Text, highlighter

highlighted = highlighter(Text(text))
matched = [
text[span.start : span.end]
for span in highlighted.spans
if span.style == "types"
]
assert matched == expected


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

Expand Down
2 changes: 1 addition & 1 deletion typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class OptionHighlighter(RegexHighlighter):
highlights = [
r"(^|\W)(?P<switch>\-\w+)(?![a-zA-Z0-9])",
r"(^|\W)(?P<option>\-\-[\w\-]+)(?![a-zA-Z0-9])",
r"(?P<types>\<[^\>]+\>)",
r"(?P<types>\<\S(?:[^\>]*\S)?\>)",
r"(?P<usage>Usage: )",
]

Expand Down
Loading