Skip to content

Commit eb7a453

Browse files
committed
feat(cmd2): display tab completion hints in bottom toolbar
Updated ArgparseCompleter integration to display required argument hints in the prompt-toolkit bottom toolbar. This ensures hints like 'Hint: name name of this alias' are visible below the prompt when no completion matches are available. Restored printing of hint tables above the prompt for other argument types.
1 parent 87a8894 commit eb7a453

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

cmd2/cmd2.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,8 @@ def _reset_completion_defaults(self) -> None:
16291629

16301630
def _bottom_toolbar(self) -> Any:
16311631
"""Get the bottom toolbar content."""
1632+
if self.completion_hint:
1633+
return self.completion_hint.strip()
16321634
if self.include_bottom_toolbar:
16331635
return sys.argv[0]
16341636
return None
@@ -3240,6 +3242,7 @@ def read_input(
32403242
:param parser: optional argparse parser
32413243
:return: the line read from input
32423244
"""
3245+
self._reset_completion_defaults()
32433246
if self.use_rawinput and self.stdin.isatty():
32443247
# Determine completer
32453248
completer_to_use: Completer
@@ -3288,13 +3291,13 @@ def get_prompt() -> Any:
32883291
return temp_session1.prompt(
32893292
prompt_to_use,
32903293
completer=completer_to_use,
3291-
bottom_toolbar=self._bottom_toolbar if self.include_bottom_toolbar else None,
3294+
bottom_toolbar=self._bottom_toolbar,
32923295
)
32933296

32943297
return self.session.prompt(
32953298
prompt_to_use,
32963299
completer=completer_to_use,
3297-
bottom_toolbar=self._bottom_toolbar if self.include_bottom_toolbar else None,
3300+
bottom_toolbar=self._bottom_toolbar,
32983301
)
32993302

33003303
# Otherwise read from self.stdin
@@ -3308,7 +3311,7 @@ def get_prompt() -> Any:
33083311
)
33093312
line = temp_session2.prompt(
33103313
prompt,
3311-
bottom_toolbar=self._bottom_toolbar if self.include_bottom_toolbar else None,
3314+
bottom_toolbar=self._bottom_toolbar,
33123315
)
33133316
if len(line) == 0:
33143317
raise EOFError
@@ -3322,7 +3325,7 @@ def get_prompt() -> Any:
33223325
complete_while_typing=self.session.complete_while_typing,
33233326
)
33243327
line = temp_session3.prompt(
3325-
bottom_toolbar=self._bottom_toolbar if self.include_bottom_toolbar else None,
3328+
bottom_toolbar=self._bottom_toolbar,
33263329
)
33273330
if len(line) == 0:
33283331
raise EOFError

cmd2/pt_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
TYPE_CHECKING,
77
)
88

9+
from prompt_toolkit import (
10+
print_formatted_text,
11+
)
912
from prompt_toolkit.completion import (
1013
Completer,
1114
Completion,
1215
)
1316
from prompt_toolkit.document import Document
17+
from prompt_toolkit.formatted_text import ANSI
1418
from prompt_toolkit.history import History
1519

1620
from . import (
@@ -67,6 +71,14 @@ def get_completions(self, document: Document, _complete_event: object) -> Iterab
6771
# We pass state=0 to trigger the completion calculation.
6872
self.cmd_app.complete(text, 0, line=line, begidx=begidx, endidx=endidx, custom_settings=self.custom_settings)
6973

74+
# Print formatted completions (tables) above the prompt if present
75+
if self.cmd_app.formatted_completions:
76+
print_formatted_text(ANSI("\n" + self.cmd_app.formatted_completions))
77+
self.cmd_app.formatted_completions = ""
78+
79+
# completion_hint will be displayed in the bottom toolbar by cmd2.py
80+
# and cleared by _reset_completion_defaults() on the next completion attempt.
81+
7082
# Now we iterate over self.cmd_app.completion_matches and self.cmd_app.display_matches
7183
matches = self.cmd_app.completion_matches
7284
display_matches = self.cmd_app.display_matches

tests/test_cmd2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3374,6 +3374,10 @@ def test_bottom_toolbar(base_app, monkeypatch):
33743374
monkeypatch.setattr(sys, 'argv', ['myapp.py'])
33753375
assert base_app._bottom_toolbar() == 'myapp.py'
33763376

3377+
# Test hint priority
3378+
base_app.completion_hint = 'My Hint'
3379+
assert base_app._bottom_toolbar() == 'My Hint'
3380+
33773381

33783382
def test_multiline_complete_statement_keyboard_interrupt(multiline_app, monkeypatch):
33793383
# Mock read_input to raise KeyboardInterrupt

0 commit comments

Comments
 (0)