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
34 changes: 34 additions & 0 deletions tests/test_rich_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import sys

import pytest
import typer
import typer.completion
from typer.testing import CliRunner

from tests.utils import needs_rich

runner = CliRunner()


Expand Down Expand Up @@ -99,3 +102,34 @@ def main(bar: str):
result = runner.invoke(app, ["--help"])
assert "Usage" in result.stdout
assert "BAR" in result.stdout


@needs_rich
@pytest.mark.parametrize("input_text", ["[ARGS]", "[ARGS]..."])
def test_metavar_highlighter(input_text: str):
Comment on lines +108 to +109
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test succeeds on master with the normal [ARGS] but fails with [ARGS].... Succeeds with this PR.

"""
Test that the MetavarHighlighter works correctly.
cf PR 1508
"""
from typer.rich_utils import (
STYLE_METAVAR_SEPARATOR,
Text,
_get_rich_console,
metavar_highlighter,
)

console = _get_rich_console()

text = Text(input_text)
highlighted = metavar_highlighter(text)
console.print(highlighted)

# Get the style for each bracket
opening_bracket_style = highlighted.get_style_at_offset(console, 0)
closing_bracket_style = highlighted.get_style_at_offset(console, 5)

# The opening bracket should have metavar_sep style
assert str(opening_bracket_style) == STYLE_METAVAR_SEPARATOR

# The closing bracket should have metavar_sep style (fails before PR 1508 when there are 3 dots)
assert str(closing_bracket_style) == STYLE_METAVAR_SEPARATOR
3 changes: 3 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest
from typer._completion_shared import _get_shell_name
from typer.core import HAS_RICH

needs_py310 = pytest.mark.skipif(
sys.version_info < (3, 10), reason="requires python3.10+"
Expand All @@ -13,6 +14,8 @@
not sys.platform.startswith("linux"), reason="Test requires Linux"
)

needs_rich = pytest.mark.skipif(not HAS_RICH, reason="Test requires Rich")

shell = _get_shell_name()
needs_bash = pytest.mark.skipif(
shell is None or "bash" not in shell, reason="Test requires Bash"
Expand Down
20 changes: 10 additions & 10 deletions typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,18 @@ class NegativeOptionHighlighter(RegexHighlighter):
]


# Highlighter to make [ | ] and <> dim
class MetavarHighlighter(RegexHighlighter):
highlights = [
r"^(?P<metavar_sep>(\[|<))",
r"(?P<metavar_sep>\|)",
r"(?P<metavar_sep>(\]|>))(\.\.\.)?$",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiangolo: notice how this code was moved, but also changed to add the optional 3 dots at the end of the regex.

]


highlighter = OptionHighlighter()
negative_highlighter = NegativeOptionHighlighter()
metavar_highlighter = MetavarHighlighter()


def _get_rich_console(stderr: bool = False) -> Console:
Expand Down Expand Up @@ -393,16 +403,6 @@ def _print_options_panel(
if param.required:
required = Text(REQUIRED_SHORT_STRING, style=STYLE_REQUIRED_SHORT)

# Highlighter to make [ | ] and <> dim
class MetavarHighlighter(RegexHighlighter):
highlights = [
r"^(?P<metavar_sep>(\[|<))",
r"(?P<metavar_sep>\|)",
r"(?P<metavar_sep>(\]|>)$)",
]

metavar_highlighter = MetavarHighlighter()

required_rows.append(required)
options_rows.append(
[
Expand Down