Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Remove Rich tags when showing completion text #877

Merged
merged 6 commits into from
Nov 18, 2024
Merged
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
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
Loading