From bff69222f26b195abb7a7c6bf830462e50b406f4 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Wed, 3 Jul 2024 11:27:00 +0200 Subject: [PATCH 1/3] write completion text with rich (if it's available) --- typer/completion.py | 16 +++++++++++++++- typer/rich_utils.py | 6 ++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/typer/completion.py b/typer/completion.py index 1220a1b545..41acd200aa 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 @@ -141,9 +147,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 rich is None: + 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 cf0538e914..2a2e77ef18 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -709,3 +709,9 @@ def rich_abort_error() -> None: """Print richly formatted abort error.""" console = _get_rich_console(stderr=True) 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) From 9d2ca11d82f83a09ce08bea48ba1077f20a9ec54 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Wed, 3 Jul 2024 12:12:46 +0200 Subject: [PATCH 2/3] fix test --- tests/test_completion/test_completion_complete.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ) From 895a485d660b783feb8c6188e47255da2860938b Mon Sep 17 00:00:00 2001 From: svlandeg Date: Wed, 3 Jul 2024 12:22:37 +0200 Subject: [PATCH 3/3] pragma no cover, as test suite has rich installed --- typer/completion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typer/completion.py b/typer/completion.py index 41acd200aa..cf294c8f59 100644 --- a/typer/completion.py +++ b/typer/completion.py @@ -149,7 +149,7 @@ def shell_complete( # Typer override to print the completion help msg with Rich if instruction == "complete": - if rich is None: + if not rich: # pragma: no cover click.echo(comp.complete()) else: from . import rich_utils