@@ -369,7 +369,7 @@ def __init__(self, completekey: str='tab', stdin=None, stdout=None, persistent_h
369369 except AttributeError :
370370 pass
371371
372- # initialize plugin system
372+ # initialize plugin system
373373 # needs to be done before we call __init__(0)
374374 self ._initialize_plugin_system ()
375375
@@ -482,11 +482,11 @@ def __init__(self, completekey: str='tab', stdin=None, stdout=None, persistent_h
482482 # in reset_completion_defaults() and it is up to completer functions to set them before returning results.
483483 ############################################################################################################
484484
485- # If true and a single match is returned to complete(), then a space will be appended
485+ # If True and a single match is returned to complete(), then a space will be appended
486486 # if the match appears at the end of the line
487487 self .allow_appended_space = True
488488
489- # If true and a single match is returned to complete(), then a closing quote
489+ # If True and a single match is returned to complete(), then a closing quote
490490 # will be added if there is an unmatched opening quote
491491 self .allow_closing_quote = True
492492
@@ -504,6 +504,10 @@ def __init__(self, completekey: str='tab', stdin=None, stdout=None, persistent_h
504504 # quote matches that are completed in a delimited fashion
505505 self .matches_delimited = False
506506
507+ # Set to True before returning matches to complete() in cases where matches are sorted with custom ordering.
508+ # If False, then complete() will sort the matches alphabetically before they are displayed.
509+ self .matches_sorted = False
510+
507511 # Set the pager(s) for use with the ppaged() method for displaying output using a pager
508512 if sys .platform .startswith ('win' ):
509513 self .pager = self .pager_chop = 'more'
@@ -678,6 +682,7 @@ def reset_completion_defaults(self) -> None:
678682 self .completion_header = ''
679683 self .display_matches = []
680684 self .matches_delimited = False
685+ self .matches_sorted = False
681686
682687 if rl_type == RlType .GNU :
683688 readline .set_completion_display_matches_hook (self ._display_matches_gnu_readline )
@@ -994,12 +999,15 @@ def complete_users():
994999 users = []
9951000
9961001 # Windows lacks the pwd module so we can't get a list of users.
997- # Instead we will add a slash once the user enters text that
1002+ # Instead we will return a result once the user enters text that
9981003 # resolves to an existing home directory.
9991004 if sys .platform .startswith ('win' ):
10001005 expanded_path = os .path .expanduser (text )
10011006 if os .path .isdir (expanded_path ):
1002- users .append (text + os .path .sep )
1007+ user = text
1008+ if add_trailing_sep_if_dir :
1009+ user += os .path .sep
1010+ users .append (user )
10031011 else :
10041012 import pwd
10051013
@@ -1083,6 +1091,10 @@ def complete_users():
10831091 self .allow_appended_space = False
10841092 self .allow_closing_quote = False
10851093
1094+ # Sort the matches before any trailing slashes are added
1095+ matches = utils .alphabetical_sort (matches )
1096+ self .matches_sorted = True
1097+
10861098 # Build display_matches and add a slash to directories
10871099 for index , cur_match in enumerate (matches ):
10881100
@@ -1446,11 +1458,8 @@ def complete(self, text: str, state: int) -> Optional[str]:
14461458 if self .completion_matches :
14471459
14481460 # Eliminate duplicates
1449- matches_set = set (self .completion_matches )
1450- self .completion_matches = list (matches_set )
1451-
1452- display_matches_set = set (self .display_matches )
1453- self .display_matches = list (display_matches_set )
1461+ self .completion_matches = utils .remove_duplicates (self .completion_matches )
1462+ self .display_matches = utils .remove_duplicates (self .display_matches )
14541463
14551464 if not self .display_matches :
14561465 # Since self.display_matches is empty, set it to self.completion_matches
@@ -1521,10 +1530,11 @@ def complete(self, text: str, state: int) -> Optional[str]:
15211530
15221531 self .completion_matches [0 ] += str_to_append
15231532
1524- # Otherwise sort matches
1525- elif self .completion_matches :
1526- self .completion_matches .sort ()
1527- self .display_matches .sort ()
1533+ # Sort matches alphabetically if they haven't already been sorted
1534+ if not self .matches_sorted :
1535+ self .completion_matches = utils .alphabetical_sort (self .completion_matches )
1536+ self .display_matches = utils .alphabetical_sort (self .display_matches )
1537+ self .matches_sorted = True
15281538
15291539 try :
15301540 return self .completion_matches [state ]
@@ -2270,7 +2280,7 @@ def do_unalias(self, arglist: List[str]) -> None:
22702280
22712281 else :
22722282 # Get rid of duplicates
2273- arglist = list ( set ( arglist ) )
2283+ arglist = utils . remove_duplicates ( arglist )
22742284
22752285 for cur_arg in arglist :
22762286 if cur_arg in self .aliases :
@@ -2315,12 +2325,10 @@ def _help_menu(self, verbose: bool=False) -> None:
23152325 """Show a list of commands which help can be displayed for.
23162326 """
23172327 # Get a sorted list of help topics
2318- help_topics = self .get_help_topics ()
2319- help_topics .sort ()
2328+ help_topics = utils .alphabetical_sort (self .get_help_topics ())
23202329
23212330 # Get a sorted list of visible command names
2322- visible_commands = self .get_visible_commands ()
2323- visible_commands .sort ()
2331+ visible_commands = utils .alphabetical_sort (self .get_visible_commands ())
23242332
23252333 cmds_doc = []
23262334 cmds_undoc = []
0 commit comments