Skip to content

Commit

Permalink
Escape colon in zsh completions. Fixes #456
Browse files Browse the repository at this point in the history
  • Loading branch information
kislyuk committed Nov 12, 2023
1 parent e800628 commit 5b4397e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
22 changes: 16 additions & 6 deletions argcomplete/finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,19 +525,29 @@ def quote_completions(
special_chars = special_chars.replace('`', '')
else:
escape_char = "\\"
for char in special_chars:
completions = [c.replace(char, escape_char + char) for c in completions]
if os.environ.get("_ARGCOMPLETE_SHELL") == "zsh":
# zsh uses colon as a separator between a completion and its description.
special_chars += ":"

escaped_completions = []
for completion in completions:
escaped_completion = completion
for char in special_chars:
escaped_completion = escaped_completion.replace(char, escape_char + char)
escaped_completions.append(escaped_completion)
if completion in self._display_completions:
self._display_completions[escaped_completion] = self._display_completions[completion]

if self.append_space:
# Similar functionality in bash was previously turned off by supplying the "-o nospace" option to complete.
# Now it is conditionally disabled using "compopt -o nospace" if the match ends in a continuation character.
# This code is retained for environments where this isn't done natively.
continuation_chars = "=/:"
if len(completions) == 1 and completions[0][-1] not in continuation_chars:
if len(escaped_completions) == 1 and escaped_completions[0][-1] not in continuation_chars:
if cword_prequote == "":
completions[0] += " "
escaped_completions[0] += " "

return completions
return escaped_completions

def rl_complete(self, text, state):
"""
Expand Down Expand Up @@ -569,7 +579,7 @@ def rl_complete(self, text, state):

def get_display_completions(self):
"""
This function returns a mapping of option names to their help strings for displaying to the user.
This function returns a mapping of completions to their help strings for displaying to the user.
"""
return self._display_completions

Expand Down
4 changes: 2 additions & 2 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ def make_parser():
for cmd, output in expected_outputs:
self.assertEqual(set(self.run_completer(make_parser(), cmd)), set(output))
zsh_expected_outputs = (
("prog --url ", ["http://url1:foo", "http://url2:bar"]),
('prog --url "', ["http://url1:foo", "http://url2:bar"]),
("prog --url ", ["http\\://url1:foo", "http\\://url2:bar"]),
('prog --url "', ["http\\://url1:foo", "http\\://url2:bar"]),
('prog --url "http://url1" --email ', ["[email protected]:", "[email protected]:", "[email protected]:", "[email protected]:", "[email protected]:"]),
('prog --url "http://url1" --email a', ["[email protected]:", "[email protected]:", "[email protected]:"]),
('prog --url "http://url1" --email "a@', ["[email protected]:", "[email protected]:"]),
Expand Down

0 comments on commit 5b4397e

Please sign in to comment.