Skip to content

Commit 844fd2d

Browse files
authored
Merge pull request #335 from python-cmd2/completion_tweaks
Added padding to display matches for visual appeal
2 parents 81a70e0 + 93eafd4 commit 844fd2d

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

cmd2.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,29 @@ def _redirect_complete(self, text, line, begidx, endidx, compfunc):
17581758
# Call the command's completer function
17591759
return compfunc(text, line, begidx, endidx)
17601760

1761+
@staticmethod
1762+
def _pad_matches_to_display(matches_to_display):
1763+
"""
1764+
Adds padding to the matches being displayed as tab completion suggestions.
1765+
The default padding of readline/pyreadine is small and not visually appealing
1766+
especially if matches have spaces. It appears very squished together.
1767+
:param matches_to_display: the matches being padded
1768+
:return: the padded matches and length of padding
1769+
"""
1770+
if rl_type == RlType.GNU:
1771+
# Add 2 to the padding of 2 that readline uses for a total of 4.
1772+
padding = 2 * ' '
1773+
return [cur_match + padding for cur_match in matches_to_display], len(padding)
1774+
1775+
elif rl_type == RlType.PYREADLINE:
1776+
# Add 3 to the padding of 1 that pyreadline uses for a total of 4.
1777+
padding = 3 * ' '
1778+
return [cur_match + padding for cur_match in matches_to_display], len(padding)
1779+
1780+
else:
1781+
# This function is meaningless without readline
1782+
return matches_to_display, 0
1783+
17611784
def _display_matches_gnu_readline(self, substitution, matches, longest_match_length):
17621785
"""
17631786
Prints a match list using GNU readline's rl_display_match_list()
@@ -1783,6 +1806,10 @@ def _display_matches_gnu_readline(self, substitution, matches, longest_match_len
17831806
else:
17841807
matches_to_display = matches
17851808

1809+
# Add padding for visual appeal
1810+
matches_to_display, padding_length = self._pad_matches_to_display(matches_to_display)
1811+
longest_match_length += padding_length
1812+
17861813
# We will use readline's display function (rl_display_match_list()), so we
17871814
# need to encode our string as bytes to place in a C array.
17881815
if six.PY3:
@@ -1829,6 +1856,9 @@ def _display_matches_pyreadline(self, matches):
18291856
else:
18301857
matches_to_display = matches
18311858

1859+
# Add padding for visual appeal
1860+
matches_to_display, _ = self._pad_matches_to_display(matches_to_display)
1861+
18321862
# Display the matches
18331863
orig_pyreadline_display(matches_to_display)
18341864

@@ -2088,7 +2118,7 @@ def complete(self, text, state):
20882118
# before we alter them. That way the suggestions will reflect how we parsed
20892119
# the token being completed and not how readline did.
20902120
if len(self.display_matches) == 0:
2091-
self.display_matches = self.completion_matches
2121+
self.display_matches = copy.copy(self.completion_matches)
20922122

20932123
# Check if we need to remove text from the beginning of tab completions
20942124
if text_to_remove:

0 commit comments

Comments
 (0)