Skip to content

Commit

Permalink
✨ Remove Rich tags when showing completion text (#877)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
  • Loading branch information
svlandeg and tiangolo authored Nov 18, 2024
1 parent 20d4e5c commit 603f39d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tests/test_completion/test_completion_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
16 changes: 15 additions & 1 deletion typer/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 603f39d

Please sign in to comment.