From fd546ec1a4e0927cfc09b1b778456f26054e629f Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Sat, 31 Mar 2018 02:58:30 -0400 Subject: Sorting matches earlier --- cmd2.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'cmd2.py') diff --git a/cmd2.py b/cmd2.py index 093abc01..d2111dff 100755 --- a/cmd2.py +++ b/cmd2.py @@ -2065,6 +2065,13 @@ class Cmd(cmd.Cmd): if len(self.completion_matches) > 0: + # Eliminate duplicates + matches_set = set(self.completion_matches) + self.completion_matches = list(matches_set) + + display_matches_set = set(self.display_matches) + self.display_matches = list(display_matches_set) + # Get the token being completed as it appears on the command line raw_completion_token = raw_tokens[-1] @@ -2103,15 +2110,6 @@ class Cmd(cmd.Cmd): strs_to_match = alias_names + visible_commands self.completion_matches = self.basic_complete(text, line, begidx, endidx, strs_to_match) - # Eliminate duplicates and sort - matches_set = set(self.completion_matches) - self.completion_matches = list(matches_set) - self.completion_matches.sort() - - display_matches_set = set(self.display_matches) - self.display_matches = list(display_matches_set) - self.display_matches.sort() - # Handle single result if len(self.completion_matches) == 1: str_to_append = '' @@ -2126,6 +2124,11 @@ class Cmd(cmd.Cmd): self.completion_matches[0] += str_to_append + # Otherwise sort matches + elif len(self.completion_matches) > 0: + self.completion_matches.sort() + self.display_matches.sort() + try: return self.completion_matches[state] except IndexError: -- cgit v1.2.1