Skip to content

Commit 49da47f

Browse files
committed
Simplified and renamed _shell_command_complete since it didn't need to be an entire completer function
1 parent 59a5923 commit 49da47f

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

cmd2.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def path_complete(text, line, begidx, endidx, dir_exe_only=False, dir_only=False
354354

355355
# If there is a single completion
356356
if len(completions) == 1:
357-
# If it is a file and we are at the end of the line, then add a space for convenience
357+
# If it is a file and we are at the end of the line, then add a space
358358
if os.path.isfile(path_completions[0]) and endidx == len(line):
359359
completions[0] += ' '
360360
# If tilde was expanded without a separator, prepend one
@@ -2030,22 +2030,20 @@ def do_shell(self, command):
20302030
proc = subprocess.Popen(command, stdout=self.stdout, shell=True)
20312031
proc.communicate()
20322032

2033-
# noinspection PyUnusedLocal
20342033
@staticmethod
2035-
def _shell_command_complete(text, line, begidx, endidx):
2036-
"""Method called to complete an input line by environment PATH executable completion.
2034+
def _get_exes_in_path(starts_with, at_eol):
2035+
"""
2036+
Called by complete_shell to get names of executables in a user's path
20372037
2038-
:param text: str - the string prefix we are attempting to match (all returned matches must begin with it)
2039-
:param line: str - the current input line with leading whitespace removed
2040-
:param begidx: int - the beginning index of the prefix text
2041-
:param endidx: int - the ending index of the prefix text
2038+
:param starts_with: str - what the exes should start with
2039+
:param at_eol: bool - tells if the user's cursor is at the end of the command line
20422040
:return: List[str] - a list of possible tab completions
20432041
"""
20442042

20452043
# Purposely don't match any executable containing wildcards
20462044
wildcards = ['*', '?']
20472045
for wildcard in wildcards:
2048-
if wildcard in text:
2046+
if wildcard in starts_with:
20492047
return []
20502048

20512049
# Get a list of every directory in the PATH environment variable and ignore symbolic links
@@ -2054,9 +2052,9 @@ def _shell_command_complete(text, line, begidx, endidx):
20542052
# Use a set to store exe names since there can be duplicates
20552053
exes = set()
20562054

2057-
# Find every executable file in the PATH that matches the pattern
2055+
# Find every executable file in the user's path that matches the pattern
20582056
for path in paths:
2059-
full_path = os.path.join(path, text)
2057+
full_path = os.path.join(path, starts_with)
20602058
matches = [f for f in glob.glob(full_path + '*') if os.path.isfile(f) and os.access(f, os.X_OK)]
20612059

20622060
for match in matches:
@@ -2067,13 +2065,13 @@ def _shell_command_complete(text, line, begidx, endidx):
20672065
results.sort()
20682066

20692067
# If there is a single completion and we are at end of the line, then add a space at the end for convenience
2070-
if len(results) == 1 and endidx == len(line):
2068+
if len(results) == 1 and at_eol:
20712069
results[0] += ' '
20722070

20732071
return results
20742072

20752073
def complete_shell(self, text, line, begidx, endidx):
2076-
"""Handles tab completion of executable commands and local file system paths.
2074+
"""Handles tab completion of executable commands and local file system paths for the shell command
20772075
20782076
:param text: str - the string prefix we are attempting to match (all returned matches must begin with it)
20792077
:param line: str - the current input line with leading whitespace removed
@@ -2106,19 +2104,19 @@ def complete_shell(self, text, line, begidx, endidx):
21062104
if len(cmd_token) == 0:
21072105
return []
21082106

2107+
# Look for path characters in the token
21092108
if not (cmd_token.startswith('~') or os.path.sep in cmd_token):
21102109
# No path characters are in this token, it is OK to try shell command completion.
2111-
command_completions = self._shell_command_complete(text, line, begidx, endidx)
2110+
command_completions = self._get_exes_in_path(text, endidx == len(line))
21122111

21132112
if command_completions:
21142113
return command_completions
21152114

21162115
# If we have no results, try path completion to find the shell commands
21172116
return path_complete(text, line, begidx, endidx, dir_exe_only=True)
21182117

2119-
# Shell command has been completed
2118+
# We are past the shell command, so do path completion
21202119
else:
2121-
# Do path completion
21222120
return path_complete(text, line, begidx, endidx)
21232121

21242122
# noinspection PyBroadException

0 commit comments

Comments
 (0)