summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md1
-rw-r--r--cmd2/argparse_completer.py8
-rwxr-xr-xexamples/tab_autocomp_dynamic.py7
-rwxr-xr-xexamples/tab_autocompletion.py7
4 files changed, 14 insertions, 9 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a36d9d21..148ccda1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,5 @@
## 0.9.11 (TBD, 2019)
* Enhancements
- * Simplified examples that illustrate ``argparse`` tab completion via ``AutoCompleter``
* Added ``matches_sort_key`` to override the default way tab completion matches are sorted
* Deprecations
* Deprecated support for bash completion since this feature had slow performance. Also it relied on
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py
index 7732c5c5..891622d1 100644
--- a/cmd2/argparse_completer.py
+++ b/cmd2/argparse_completer.py
@@ -70,9 +70,9 @@ from argparse import ZERO_OR_MORE, ONE_OR_MORE, ArgumentError, _, _get_action_na
from typing import List, Dict, Tuple, Callable, Union
from colorama import Fore
-from wcwidth import wcswidth
from .rl_utils import rl_force_redisplay
+from .utils import ansi_safe_wcswidth
# attribute that can optionally added to an argparse argument (called an Action) to
# define the completion choices for the argument. You may provide a Collection or a Function.
@@ -587,16 +587,16 @@ class AutoCompleter(object):
def _format_completions(self, action, completions: List[Union[str, CompletionItem]]) -> List[str]:
if completions and len(completions) > 1 and isinstance(completions[0], CompletionItem):
- # If the user has not already sorted the CompletionItems, then do that now
+ # If the user has not already sorted the CompletionItems, then sort them before appending the descriptions
if not self._cmd2_app.matches_sorted:
completions.sort(key=self._cmd2_app.matches_sort_key)
self._cmd2_app.matches_sorted = True
- token_width = wcswidth(action.dest)
+ token_width = ansi_safe_wcswidth(action.dest)
completions_with_desc = []
for item in completions:
- item_width = wcswidth(item)
+ item_width = ansi_safe_wcswidth(item)
if item_width > token_width:
token_width = item_width
diff --git a/examples/tab_autocomp_dynamic.py b/examples/tab_autocomp_dynamic.py
index ebabf1f9..bedc9d4b 100755
--- a/examples/tab_autocomp_dynamic.py
+++ b/examples/tab_autocomp_dynamic.py
@@ -9,7 +9,7 @@ Released under MIT license, see LICENSE file
from typing import List
import cmd2
-from cmd2 import argparse_completer
+from cmd2 import argparse_completer, utils
actors = ['Mark Hamill', 'Harrison Ford', 'Carrie Fisher', 'Alec Guinness', 'Peter Mayhew',
'Anthony Daniels', 'Adam Driver', 'Daisy Ridley', 'John Boyega', 'Oscar Isaac',
@@ -175,11 +175,14 @@ class TabCompleteExample(cmd2.Cmd):
"""Demonstrates showing tabular hinting of tab completion information"""
completions_with_desc = []
- for movie_id in self.MOVIE_DATABASE_IDS:
+ # Sort the movie id strings with a natural sort since they contain numbers
+ for movie_id in utils.natural_sort(self.MOVIE_DATABASE_IDS):
if movie_id in self.MOVIE_DATABASE:
movie_entry = self.MOVIE_DATABASE[movie_id]
completions_with_desc.append(argparse_completer.CompletionItem(movie_id, movie_entry['title']))
+ # Mark that we already sorted the matches
+ self.matches_sorted = True
return completions_with_desc
###################################################################################
diff --git a/examples/tab_autocompletion.py b/examples/tab_autocompletion.py
index 156c2d45..aa28fc10 100755
--- a/examples/tab_autocompletion.py
+++ b/examples/tab_autocompletion.py
@@ -11,7 +11,7 @@ import itertools
from typing import List
import cmd2
-from cmd2 import argparse_completer
+from cmd2 import argparse_completer, utils
actors = ['Mark Hamill', 'Harrison Ford', 'Carrie Fisher', 'Alec Guinness', 'Peter Mayhew',
'Anthony Daniels', 'Adam Driver', 'Daisy Ridley', 'John Boyega', 'Oscar Isaac',
@@ -113,11 +113,14 @@ class TabCompleteExample(cmd2.Cmd):
"""Demonstrates showing tabular hinting of tab completion information"""
completions_with_desc = []
- for movie_id in self.MOVIE_DATABASE_IDS:
+ # Sort the movie id strings with a natural sort since they contain numbers
+ for movie_id in utils.natural_sort(self.MOVIE_DATABASE_IDS):
if movie_id in self.MOVIE_DATABASE:
movie_entry = self.MOVIE_DATABASE[movie_id]
completions_with_desc.append(argparse_completer.CompletionItem(movie_id, movie_entry['title']))
+ # Mark that we already sorted the matches
+ self.matches_sorted = True
return completions_with_desc
# This demonstrates a number of customizations of the AutoCompleter version of ArgumentParser