Skip to content

Commit 5e44213

Browse files
committed
Fix fish shell completions
1 parent 88fcf83 commit 5e44213

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

tests/test_completion/test_completion_complete.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_completion_complete_subcommand_fish():
7979
},
8080
)
8181
assert (
82-
"delete Delete a user with USERNAME.\ndelete-all Delete ALL users in the database."
82+
"delete\tDelete a user with USERNAME.\ndelete-all\tDelete ALL users in the database."
8383
in result.stdout
8484
)
8585

typer/_completion_classes.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import importlib
2+
import importlib.util
13
import os
24
import re
35
import sys
@@ -21,6 +23,15 @@
2123
shellingham = None
2224

2325

26+
def _sanitize_help_text(text: str) -> str:
27+
"""Sanitizes the help text by removing rich tags"""
28+
if not importlib.util.find_spec("rich"):
29+
return text
30+
from . import rich_utils
31+
32+
return rich_utils.rich_render_text(text)
33+
34+
2435
class BashComplete(click.shell_completion.BashComplete):
2536
name = Shells.bash.value
2637
source_template = COMPLETION_SCRIPT_BASH
@@ -93,7 +104,7 @@ def escape(s: str) -> str:
93104
# the difference with and without escape
94105
# return f"{item.type}\n{item.value}\n{item.help if item.help else '_'}"
95106
if item.help:
96-
return f'"{escape(item.value)}":"{escape(item.help)}"'
107+
return f'"{escape(item.value)}":"{_sanitize_help_text(escape(item.help))}"'
97108
else:
98109
return f'"{escape(item.value)}"'
99110

@@ -139,7 +150,7 @@ def format_completion(self, item: click.shell_completion.CompletionItem) -> str:
139150
# return f"{item.type},{item.value}
140151
if item.help:
141152
formatted_help = re.sub(r"\s", " ", item.help)
142-
return f"{item.value}\t{formatted_help}"
153+
return f"{item.value}\t{_sanitize_help_text(formatted_help)}"
143154
else:
144155
return f"{item.value}"
145156

@@ -180,7 +191,7 @@ def get_completion_args(self) -> Tuple[List[str], str]:
180191
return args, incomplete
181192

182193
def format_completion(self, item: click.shell_completion.CompletionItem) -> str:
183-
return f"{item.value}:::{item.help or ' '}"
194+
return f"{item.value}:::{_sanitize_help_text(item.help) if item.help else ' '}"
184195

185196

186197
def completion_init() -> None:

typer/completion.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515
except ImportError: # pragma: no cover
1616
shellingham = None
1717

18-
try:
19-
import rich
20-
21-
except ImportError: # pragma: no cover
22-
rich = None # type: ignore
23-
2418

2519
_click_patched = False
2620

@@ -147,13 +141,7 @@ def shell_complete(
147141

148142
# Typer override to print the completion help msg with Rich
149143
if instruction == "complete":
150-
if not rich: # pragma: no cover
151-
click.echo(comp.complete())
152-
else:
153-
from . import rich_utils
154-
155-
rich_utils.print_with_rich(comp.complete())
156-
144+
click.echo(comp.complete())
157145
return 0
158146
# Typer override end
159147

typer/rich_utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -712,12 +712,6 @@ def rich_abort_error() -> None:
712712
console.print(ABORTED_TEXT, style=STYLE_ABORTED)
713713

714714

715-
def print_with_rich(text: str) -> None:
716-
"""Print richly formatted message."""
717-
console = _get_rich_console()
718-
console.print(text)
719-
720-
721715
def rich_to_html(input_text: str) -> str:
722716
"""Print the HTML version of a rich-formatted input string.
723717
@@ -729,3 +723,9 @@ def rich_to_html(input_text: str) -> str:
729723
console.print(input_text, overflow="ignore", crop=False)
730724

731725
return console.export_html(inline_styles=True, code_format="{code}").strip()
726+
727+
728+
def rich_render_text(text: str) -> str:
729+
"""Remove rich tags and render a pure text representation"""
730+
console = _get_rich_console()
731+
return "".join(segment.text for segment in console.render(text)).rstrip("\n")

0 commit comments

Comments
 (0)