Skip to content

Commit

Permalink
display_matches is no longer restricted to delimited strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kmvanbrunt committed Jun 8, 2018
1 parent 91a69cb commit 3ec6cea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 0.8.8 (TBD, 2018)
* Bug Fixes
* Prevent crashes that could occur attempting to open a file in non-existent directory or with very long filename
* Enhancements
* ``display_matches`` is no longer restricted to delimited strings

## 0.8.7 (May 28, 2018)
* Bug Fixes
Expand Down
31 changes: 18 additions & 13 deletions cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,7 @@ def complete_submenu(_self, text, line, begidx, endidx):
submenu.allow_appended_space = True
submenu.allow_closing_quote = True
submenu.display_matches = []
submenu.matches_delimited = False

return _complete_from_cmd(submenu, text, line, begidx, endidx)
finally:
Expand All @@ -949,6 +950,7 @@ def complete_submenu(_self, text, line, begidx, endidx):
_self.allow_appended_space = submenu.allow_appended_space
_self.allow_closing_quote = submenu.allow_closing_quote
_self.display_matches = copy.copy(submenu.display_matches)
_self.matches_delimited = submenu.matches_delimited

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

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

# Used by functions like path_complete() and delimiter_complete() to properly
# quote matches that are completed in a delimited fashion
self.matches_delimited = False

# ----- Methods related to presenting output to the user -----

@property
Expand Down Expand Up @@ -1380,6 +1385,7 @@ def reset_completion_defaults(self):
self.allow_appended_space = True
self.allow_closing_quote = True
self.display_matches = []
self.matches_delimited = False

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

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

# Get the common beginning for the matches
common_prefix = os.path.commonprefix(matches)
Expand Down Expand Up @@ -1758,6 +1766,9 @@ def complete_users():
search_str = os.path.join(os.getcwd(), search_str)
cwd_added = True

# Set this to True for proper quoting of paths with spaces
self.matches_delimited = True

# Find all matching path completions
matches = glob.glob(search_str)

Expand Down Expand Up @@ -2147,13 +2158,7 @@ def complete(self, text, state):
display_matches_set = set(self.display_matches)
self.display_matches = list(display_matches_set)

# Check if display_matches has been used. If so, then matches
# on delimited strings like paths was done.
if self.display_matches:
matches_delimited = True
else:
matches_delimited = False

if not self.display_matches:
# Since self.display_matches is empty, set it to self.completion_matches
# before we alter them. That way the suggestions will reflect how we parsed
# the token being completed and not how readline did.
Expand All @@ -2167,7 +2172,7 @@ def complete(self, text, state):
# This is the tab completion text that will appear on the command line.
common_prefix = os.path.commonprefix(self.completion_matches)

if matches_delimited:
if self.matches_delimited:
# Check if any portion of the display matches appears in the tab completion
display_prefix = os.path.commonprefix(self.display_matches)

Expand Down

0 comments on commit 3ec6cea

Please sign in to comment.