diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-04-03 12:48:07 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-04-03 12:48:07 -0400 |
commit | 93eafd4e45bb3762f1aea59651a93474bed4742a (patch) | |
tree | 251df7bddf31aa17f0314813b42b47a3d7a4abbc /cmd2.py | |
parent | 81a70e0137231fe3895535b8e963300f08d1a20d (diff) | |
download | cmd2-git-93eafd4e45bb3762f1aea59651a93474bed4742a.tar.gz |
Added padding to display matches for visual appeal
Diffstat (limited to 'cmd2.py')
-rwxr-xr-x | cmd2.py | 32 |
1 files changed, 31 insertions, 1 deletions
@@ -1758,6 +1758,29 @@ class Cmd(cmd.Cmd): # Call the command's completer function return compfunc(text, line, begidx, endidx) + @staticmethod + def _pad_matches_to_display(matches_to_display): + """ + Adds padding to the matches being displayed as tab completion suggestions. + The default padding of readline/pyreadine is small and not visually appealing + especially if matches have spaces. It appears very squished together. + :param matches_to_display: the matches being padded + :return: the padded matches and length of padding + """ + if rl_type == RlType.GNU: + # Add 2 to the padding of 2 that readline uses for a total of 4. + padding = 2 * ' ' + return [cur_match + padding for cur_match in matches_to_display], len(padding) + + elif rl_type == RlType.PYREADLINE: + # Add 3 to the padding of 1 that pyreadline uses for a total of 4. + padding = 3 * ' ' + return [cur_match + padding for cur_match in matches_to_display], len(padding) + + else: + # This function is meaningless without readline + return matches_to_display, 0 + def _display_matches_gnu_readline(self, substitution, matches, longest_match_length): """ Prints a match list using GNU readline's rl_display_match_list() @@ -1783,6 +1806,10 @@ class Cmd(cmd.Cmd): else: matches_to_display = matches + # Add padding for visual appeal + matches_to_display, padding_length = self._pad_matches_to_display(matches_to_display) + longest_match_length += padding_length + # We will use readline's display function (rl_display_match_list()), so we # need to encode our string as bytes to place in a C array. if six.PY3: @@ -1829,6 +1856,9 @@ class Cmd(cmd.Cmd): else: matches_to_display = matches + # Add padding for visual appeal + matches_to_display, _ = self._pad_matches_to_display(matches_to_display) + # Display the matches orig_pyreadline_display(matches_to_display) @@ -2088,7 +2118,7 @@ class Cmd(cmd.Cmd): # before we alter them. That way the suggestions will reflect how we parsed # the token being completed and not how readline did. if len(self.display_matches) == 0: - self.display_matches = self.completion_matches + self.display_matches = copy.copy(self.completion_matches) # Check if we need to remove text from the beginning of tab completions if text_to_remove: |