diff --git a/tests/test_completion/test_completion_complete.py b/tests/test_completion/test_completion_complete.py index ea37f68546..e6d18b58a3 100644 --- a/tests/test_completion/test_completion_complete.py +++ b/tests/test_completion/test_completion_complete.py @@ -79,7 +79,7 @@ def test_completion_complete_subcommand_fish(): }, ) assert ( - "delete\tDelete a user with USERNAME.\ndelete-all\tDelete ALL users in the database." + "delete Delete a user with USERNAME.\ndelete-all Delete ALL users in the database." in result.stdout ) diff --git a/typer/completion.py b/typer/completion.py index 5b930bc20c..07ce83190c 100644 --- a/typer/completion.py +++ b/typer/completion.py @@ -15,6 +15,12 @@ except ImportError: # pragma: no cover shellingham = None +try: + import rich + +except ImportError: # pragma: no cover + rich = None # type: ignore + _click_patched = False @@ -139,9 +145,17 @@ def shell_complete( click.echo(comp.source()) return 0 + # Typer override to print the completion help msg with Rich if instruction == "complete": - click.echo(comp.complete()) + if not rich: # pragma: no cover + click.echo(comp.complete()) + else: + from . import rich_utils + + rich_utils.print_with_rich(comp.complete()) + return 0 + # Typer override end click.echo(f'Completion instruction "{instruction}" not supported.', err=True) return 1 diff --git a/typer/rich_utils.py b/typer/rich_utils.py index 12b1cd8aad..7d603da2d7 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -712,6 +712,12 @@ def rich_abort_error() -> None: console.print(ABORTED_TEXT, style=STYLE_ABORTED) +def print_with_rich(text: str) -> None: + """Print richly formatted message.""" + console = _get_rich_console() + console.print(text) + + def rich_to_html(input_text: str) -> str: """Print the HTML version of a rich-formatted input string.