From 603f39d2bfbe283b6138ff87188022d5535ae772 Mon Sep 17 00:00:00 2001 From: Sofie Van Landeghem Date: Mon, 18 Nov 2024 23:14:33 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Remove=20Rich=20tags=20when=20showi?= =?UTF-8?q?ng=20completion=20text=20(#877)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sebastián Ramírez --- .../test_completion/test_completion_complete.py | 2 +- typer/completion.py | 16 +++++++++++++++- typer/rich_utils.py | 6 ++++++ 3 files changed, 22 insertions(+), 2 deletions(-) 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.