Skip to content

Commit 53c41ea

Browse files
committed
Updated the examples to illustrate sorting CompletionItems
1 parent 60d6e4c commit 53c41ea

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
## 0.9.11 (TBD, 2019)
22
* Enhancements
3-
* Simplified examples that illustrate ``argparse`` tab completion via ``AutoCompleter``
43
* Added ``matches_sort_key`` to override the default way tab completion matches are sorted
54
* Deprecations
65
* Deprecated support for bash completion since this feature had slow performance. Also it relied on

cmd2/argparse_completer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def my_completer(text: str, line: str, begidx: int, endidx:int, extra_param: str
7070
from typing import List, Dict, Tuple, Callable, Union
7171

7272
from colorama import Fore
73-
from wcwidth import wcswidth
7473

7574
from .rl_utils import rl_force_redisplay
75+
from .utils import ansi_safe_wcswidth
7676

7777
# attribute that can optionally added to an argparse argument (called an Action) to
7878
# define the completion choices for the argument. You may provide a Collection or a Function.
@@ -587,16 +587,16 @@ def process_action_nargs(action: argparse.Action, arg_state: AutoCompleter._Argu
587587
def _format_completions(self, action, completions: List[Union[str, CompletionItem]]) -> List[str]:
588588
if completions and len(completions) > 1 and isinstance(completions[0], CompletionItem):
589589

590-
# If the user has not already sorted the CompletionItems, then do that now
590+
# If the user has not already sorted the CompletionItems, then sort them before appending the descriptions
591591
if not self._cmd2_app.matches_sorted:
592592
completions.sort(key=self._cmd2_app.matches_sort_key)
593593
self._cmd2_app.matches_sorted = True
594594

595-
token_width = wcswidth(action.dest)
595+
token_width = ansi_safe_wcswidth(action.dest)
596596
completions_with_desc = []
597597

598598
for item in completions:
599-
item_width = wcswidth(item)
599+
item_width = ansi_safe_wcswidth(item)
600600
if item_width > token_width:
601601
token_width = item_width
602602

examples/tab_autocomp_dynamic.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import List
1010

1111
import cmd2
12-
from cmd2 import argparse_completer
12+
from cmd2 import argparse_completer, utils
1313

1414
actors = ['Mark Hamill', 'Harrison Ford', 'Carrie Fisher', 'Alec Guinness', 'Peter Mayhew',
1515
'Anthony Daniels', 'Adam Driver', 'Daisy Ridley', 'John Boyega', 'Oscar Isaac',
@@ -175,11 +175,14 @@ def instance_query_movie_ids(self) -> List[str]:
175175
"""Demonstrates showing tabular hinting of tab completion information"""
176176
completions_with_desc = []
177177

178-
for movie_id in self.MOVIE_DATABASE_IDS:
178+
# Sort the movie id strings with a natural sort since they contain numbers
179+
for movie_id in utils.natural_sort(self.MOVIE_DATABASE_IDS):
179180
if movie_id in self.MOVIE_DATABASE:
180181
movie_entry = self.MOVIE_DATABASE[movie_id]
181182
completions_with_desc.append(argparse_completer.CompletionItem(movie_id, movie_entry['title']))
182183

184+
# Mark that we already sorted the matches
185+
self.matches_sorted = True
183186
return completions_with_desc
184187

185188
###################################################################################

examples/tab_autocompletion.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from typing import List
1212

1313
import cmd2
14-
from cmd2 import argparse_completer
14+
from cmd2 import argparse_completer, utils
1515

1616
actors = ['Mark Hamill', 'Harrison Ford', 'Carrie Fisher', 'Alec Guinness', 'Peter Mayhew',
1717
'Anthony Daniels', 'Adam Driver', 'Daisy Ridley', 'John Boyega', 'Oscar Isaac',
@@ -113,11 +113,14 @@ def instance_query_movie_ids(self) -> List[str]:
113113
"""Demonstrates showing tabular hinting of tab completion information"""
114114
completions_with_desc = []
115115

116-
for movie_id in self.MOVIE_DATABASE_IDS:
116+
# Sort the movie id strings with a natural sort since they contain numbers
117+
for movie_id in utils.natural_sort(self.MOVIE_DATABASE_IDS):
117118
if movie_id in self.MOVIE_DATABASE:
118119
movie_entry = self.MOVIE_DATABASE[movie_id]
119120
completions_with_desc.append(argparse_completer.CompletionItem(movie_id, movie_entry['title']))
120121

122+
# Mark that we already sorted the matches
123+
self.matches_sorted = True
121124
return completions_with_desc
122125

123126
# This demonstrates a number of customizations of the AutoCompleter version of ArgumentParser

0 commit comments

Comments
 (0)