Skip to content

Commit 3ec6cea

Browse files
committed
display_matches is no longer restricted to delimited strings
1 parent 91a69cb commit 3ec6cea

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## 0.8.8 (TBD, 2018)
22
* Bug Fixes
33
* Prevent crashes that could occur attempting to open a file in non-existent directory or with very long filename
4+
* Enhancements
5+
* ``display_matches`` is no longer restricted to delimited strings
46

57
## 0.8.7 (May 28, 2018)
68
* Bug Fixes

cmd2.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,7 @@ def complete_submenu(_self, text, line, begidx, endidx):
939939
submenu.allow_appended_space = True
940940
submenu.allow_closing_quote = True
941941
submenu.display_matches = []
942+
submenu.matches_delimited = False
942943

943944
return _complete_from_cmd(submenu, text, line, begidx, endidx)
944945
finally:
@@ -949,6 +950,7 @@ def complete_submenu(_self, text, line, begidx, endidx):
949950
_self.allow_appended_space = submenu.allow_appended_space
950951
_self.allow_closing_quote = submenu.allow_closing_quote
951952
_self.display_matches = copy.copy(submenu.display_matches)
953+
_self.matches_delimited = submenu.matches_delimited
952954

953955
original_do_help = cmd_obj.do_help
954956
original_complete_help = cmd_obj.complete_help
@@ -1174,13 +1176,16 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, persistent_histor
11741176
# will be added if there is an unmatched opening quote
11751177
self.allow_closing_quote = True
11761178

1177-
# Use this list if you are completing strings that contain a common delimiter and you only want to
1178-
# display the final portion of the matches as the tab-completion suggestions. The full matches
1179-
# still must be returned from your completer function. For an example, look at path_complete()
1180-
# which uses this to show only the basename of paths as the suggestions. delimiter_complete() also
1181-
# populates this list.
1179+
# If the tab-completion suggestions should be displayed in a way that is different than the actual match values,
1180+
# then place those results in this list. The full matches still must be returned from your completer function.
1181+
# For an example, look at path_complete() which uses this to show only the basename of paths as the
1182+
# suggestions. delimiter_complete() also populates this list.
11821183
self.display_matches = []
11831184

1185+
# Used by functions like path_complete() and delimiter_complete() to properly
1186+
# quote matches that are completed in a delimited fashion
1187+
self.matches_delimited = False
1188+
11841189
# ----- Methods related to presenting output to the user -----
11851190

11861191
@property
@@ -1380,6 +1385,7 @@ def reset_completion_defaults(self):
13801385
self.allow_appended_space = True
13811386
self.allow_closing_quote = True
13821387
self.display_matches = []
1388+
self.matches_delimited = False
13831389

13841390
if rl_type == RlType.GNU:
13851391
readline.set_completion_display_matches_hook(self._display_matches_gnu_readline)
@@ -1557,6 +1563,8 @@ def delimiter_complete(self, text, line, begidx, endidx, match_against, delimite
15571563

15581564
# Display only the portion of the match that's being completed based on delimiter
15591565
if matches:
1566+
# Set this to True for proper quoting of matches with spaces
1567+
self.matches_delimited = True
15601568

15611569
# Get the common beginning for the matches
15621570
common_prefix = os.path.commonprefix(matches)
@@ -1758,6 +1766,9 @@ def complete_users():
17581766
search_str = os.path.join(os.getcwd(), search_str)
17591767
cwd_added = True
17601768

1769+
# Set this to True for proper quoting of paths with spaces
1770+
self.matches_delimited = True
1771+
17611772
# Find all matching path completions
17621773
matches = glob.glob(search_str)
17631774

@@ -2147,13 +2158,7 @@ def complete(self, text, state):
21472158
display_matches_set = set(self.display_matches)
21482159
self.display_matches = list(display_matches_set)
21492160

2150-
# Check if display_matches has been used. If so, then matches
2151-
# on delimited strings like paths was done.
2152-
if self.display_matches:
2153-
matches_delimited = True
2154-
else:
2155-
matches_delimited = False
2156-
2161+
if not self.display_matches:
21572162
# Since self.display_matches is empty, set it to self.completion_matches
21582163
# before we alter them. That way the suggestions will reflect how we parsed
21592164
# the token being completed and not how readline did.
@@ -2167,7 +2172,7 @@ def complete(self, text, state):
21672172
# This is the tab completion text that will appear on the command line.
21682173
common_prefix = os.path.commonprefix(self.completion_matches)
21692174

2170-
if matches_delimited:
2175+
if self.matches_delimited:
21712176
# Check if any portion of the display matches appears in the tab completion
21722177
display_prefix = os.path.commonprefix(self.display_matches)
21732178

0 commit comments

Comments
 (0)