Skip to content

Commit 61cd1c0

Browse files
committed
Fixed bug where argument hints weren't displaying in color.
Also added a bottom toolboar for displaying these type hints.
1 parent 165e02e commit 61cd1c0

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

cmd2/cmd2.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,14 @@ def _reset_completion_defaults(self) -> None:
15901590
self.matches_delimited = False
15911591
self.matches_sorted = False
15921592

1593+
def _bottom_toolbar(self) -> Any:
1594+
"""Get the bottom toolbar content."""
1595+
if self.formatted_completions:
1596+
return ANSI(self.formatted_completions.rstrip())
1597+
if self.completion_hint:
1598+
return ANSI(self.completion_hint.rstrip())
1599+
return None
1600+
15931601
def tokens_for_completion(self, line: str, begidx: int, endidx: int) -> tuple[list[str], list[str]]:
15941602
"""Get all tokens through the one being completed, used by tab completion functions.
15951603
@@ -2393,13 +2401,17 @@ def complete(
23932401
# Don't print error and redraw the prompt unless the error has length
23942402
err_str = str(ex)
23952403
if err_str:
2396-
self.print_to(
2397-
sys.stdout,
2398-
Text.assemble(
2399-
"\n",
2400-
(err_str, Cmd2Style.ERROR if ex.apply_style else ""),
2401-
),
2402-
)
2404+
# If apply_style is True, then this is an error message that should be printed
2405+
# above the prompt so it remains in the scrollback.
2406+
if ex.apply_style:
2407+
self.print_to(
2408+
sys.stdout,
2409+
"\n" + err_str,
2410+
style=Cmd2Style.ERROR,
2411+
)
2412+
# Otherwise, this is a hint that should be displayed below the prompt.
2413+
else:
2414+
self.completion_hint = err_str
24032415
return None
24042416
except Exception as ex: # noqa: BLE001
24052417
# Insert a newline so the exception doesn't print in the middle of the command line being tab completed
@@ -3220,23 +3232,25 @@ def get_prompt() -> Any:
32203232
prompt_to_use,
32213233
completer=completer_to_use,
32223234
history=history_to_use,
3235+
bottom_toolbar=self._bottom_toolbar,
32233236
)
32243237

32253238
return self.session.prompt(
32263239
prompt_to_use,
32273240
completer=completer_to_use,
3241+
bottom_toolbar=self._bottom_toolbar,
32283242
)
32293243

32303244
# Otherwise read from self.stdin
32313245
elif self.stdin.isatty():
32323246
# on a tty, print the prompt first, then read the line
3233-
line = pt.prompt(prompt)
3247+
line = pt.prompt(prompt, bottom_toolbar=self._bottom_toolbar)
32343248
if len(line) == 0:
32353249
raise EOFError
32363250
return line.rstrip('\n')
32373251
else:
32383252
# not a tty, just read the line
3239-
line = pt.prompt()
3253+
line = pt.prompt(bottom_toolbar=self._bottom_toolbar)
32403254
if len(line) == 0:
32413255
raise EOFError
32423256
line = line.rstrip('\n')

tests/conftest.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,14 @@ def get_endidx() -> int:
144144
return endidx
145145

146146
# Run the prompt-toolkit tab completion function with mocks in place
147-
return app.complete(text, 0, line, begidx, endidx)
147+
res = app.complete(text, 0, line, begidx, endidx)
148+
149+
# If the completion resulted in a hint being set, then print it now
150+
# so that it can be captured by tests using capsys.
151+
if app.completion_hint:
152+
print(app.completion_hint)
153+
154+
return res
148155

149156

150157
def find_subcommand(action: argparse.ArgumentParser, subcmd_names: list[str]) -> argparse.ArgumentParser:

0 commit comments

Comments
 (0)