Skip to content

Commit adb62d8

Browse files
committed
Added ability to print a header above tab-completion suggestions
1 parent be9ffc5 commit adb62d8

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

cmd2/cmd2.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,9 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, persistent_histor
495495
# will be added if there is an unmatched opening quote
496496
self.allow_closing_quote = True
497497

498+
# An optional header that prints above the tab-completion suggestions
499+
self.completion_header = ''
500+
498501
# If the tab-completion suggestions should be displayed in a way that is different than the actual match values,
499502
# then place those results in this list. The full matches still must be returned from your completer function.
500503
# For an example, look at path_complete() which uses this to show only the basename of paths as the
@@ -661,6 +664,7 @@ def reset_completion_defaults(self):
661664
"""
662665
self.allow_appended_space = True
663666
self.allow_closing_quote = True
667+
self.completion_header = ''
664668
self.display_matches = []
665669
self.matches_delimited = False
666670

@@ -1254,6 +1258,10 @@ def _display_matches_gnu_readline(self, substitution, matches, longest_match_len
12541258
strings_array[1:-1] = encoded_matches
12551259
strings_array[-1] = None
12561260

1261+
# Print the header if one exists
1262+
if self.completion_header:
1263+
self.stdout.write('\n' + self.completion_header)
1264+
12571265
# Call readline's display function
12581266
# rl_display_match_list(strings_array, number of completion matches, longest match length)
12591267
readline_lib.rl_display_match_list(strings_array, len(encoded_matches), longest_match_length)
@@ -1279,6 +1287,10 @@ def _display_matches_pyreadline(self, matches): # pragma: no cover
12791287
# Add padding for visual appeal
12801288
matches_to_display, _ = self._pad_matches_to_display(matches_to_display)
12811289

1290+
# Print the header if one exists
1291+
if self.completion_header:
1292+
readline.rl.mode.console.write('\n' + self.completion_header)
1293+
12821294
# Display matches using actual display function. This also redraws the prompt and line.
12831295
orig_pyreadline_display(matches_to_display)
12841296

0 commit comments

Comments
 (0)