summaryrefslogtreecommitdiff
path: root/cmd2
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2019-06-30 23:56:17 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2019-06-30 23:56:17 -0400
commit0bcd2905177bfd62c51c9181926869c9ec2deb0d (patch)
tree03d3879448bdec9d106ee5c1b12fc67dfdea4136 /cmd2
parent8417cfed896aa21753b55dce8be6ab8848edc6b3 (diff)
parentab64843f638be59f252197f24e38819821b02490 (diff)
downloadcmd2-git-0bcd2905177bfd62c51c9181926869c9ec2deb0d.tar.gz
Merge branch 'master' into colorama_encapsulation
Diffstat (limited to 'cmd2')
-rw-r--r--cmd2/cmd2.py52
1 files changed, 41 insertions, 11 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 217a92c8..7efa6849 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -1195,7 +1195,7 @@ class Cmd(cmd.Cmd):
def _redirect_complete(self, text: str, line: str, begidx: int, endidx: int, compfunc: Callable) -> List[str]:
"""Called by complete() as the first tab completion function for all commands
- It determines if it should tab complete for redirection (|, <, >, >>) or use the
+ It determines if it should tab complete for redirection (|, >, >>) or use the
completer function for the current command
:param text: the string prefix we are attempting to match (all returned matches must begin with it)
@@ -1214,28 +1214,58 @@ class Cmd(cmd.Cmd):
if not raw_tokens:
return []
+ # Must at least have the command
if len(raw_tokens) > 1:
- # Check if there are redirection strings prior to the token being completed
- seen_pipe = False
+ # True when command line contains any redirection tokens
has_redirection = False
- for cur_token in raw_tokens[:-1]:
+ # Keep track of state while examining tokens
+ in_pipe = False
+ in_file_redir = False
+ do_shell_completion = False
+ do_path_completion = False
+ prior_token = None
+
+ for cur_token in raw_tokens:
+ # Process redirection tokens
if cur_token in constants.REDIRECTION_TOKENS:
has_redirection = True
+ # Check if we are at a pipe
if cur_token == constants.REDIRECTION_PIPE:
- seen_pipe = True
+ # Do not complete bad syntax (e.g cmd | |)
+ if prior_token == constants.REDIRECTION_PIPE:
+ return []
+
+ in_pipe = True
+ in_file_redir = False
+
+ # Otherwise this is a file redirection token
+ else:
+ if prior_token in constants.REDIRECTION_TOKENS or in_file_redir:
+ # Do not complete bad syntax (e.g cmd | >) (e.g cmd > blah >)
+ return []
+
+ in_pipe = False
+ in_file_redir = True
+
+ # Not a redirection token
+ else:
+ do_shell_completion = False
+ do_path_completion = False
+
+ if prior_token == constants.REDIRECTION_PIPE:
+ do_shell_completion = True
+ elif in_pipe or prior_token in (constants.REDIRECTION_OUTPUT, constants.REDIRECTION_APPEND):
+ do_path_completion = True
- # Get token prior to the one being completed
- prior_token = raw_tokens[-2]
+ prior_token = cur_token
- # If a pipe is right before the token being completed, complete a shell command as the piped process
- if prior_token == constants.REDIRECTION_PIPE:
+ if do_shell_completion:
return self.shell_cmd_complete(text, line, begidx, endidx)
- # Otherwise do path completion either as files to redirectors or arguments to the piped process
- elif prior_token in constants.REDIRECTION_TOKENS or seen_pipe:
+ elif do_path_completion:
return self.path_complete(text, line, begidx, endidx)
# If there were redirection strings anywhere on the command line, then we