diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-03-25 19:56:12 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-03-25 19:56:12 -0400 |
commit | 2d111d856be7f8dff5522815487d7e73bf6a06f9 (patch) | |
tree | 19cda3fc3a4908f5732191de34da1de25575e75a | |
parent | 826b0492ac1b92297c0d92396fc0423562573953 (diff) | |
download | cmd2-git-2d111d856be7f8dff5522815487d7e73bf6a06f9.tar.gz |
Improved parsing for tab completing redirectors
-rwxr-xr-x | cmd2.py | 42 |
1 files changed, 22 insertions, 20 deletions
@@ -1302,8 +1302,8 @@ class Cmd(cmd.Cmd): # Use non-POSIX parsing to keep the quotes around the tokens initial_tokens = shlex.split(tmp_line[:tmp_endidx], posix=False) - # If the cursor is at the end of the line and not in a quoted string, then the actual - # token being completed is blank. Add this to our list. + # If the cursor is at an empty token outside of a quoted string, + # then that is the token being completed. Add it to the list. if not unclosed_quote and begidx == tmp_endidx: initial_tokens.append('') break @@ -1736,28 +1736,30 @@ class Cmd(cmd.Cmd): # Build a list of all redirection tokens all_redirects = REDIRECTION_CHARS + ['>>'] - # Examine each token up to the prior one to see if a redirector has already appeared - for cur_token in raw_tokens[:-2]: + # Check how many redirectors have appeared before the token being completed + seen_pipe = False + num_redirectors = 0 + + for cur_token in raw_tokens[:-1]: if cur_token in all_redirects: - # If a pipe has appeared, then perform path completion for arguments to the pipe process. + num_redirectors += 1 + if num_redirectors > 1: + # Too many redirectors on the line for tab completion + return [] + if cur_token == '|': - return self.path_complete(text, line, begidx, endidx) + seen_pipe = True - # Some other redirector has already appeared. No more completion is needed. - else: - return [] + # Get token prior to the one being completed + prior_token = raw_tokens[-2] + + # If a pipe is right before the token being completed, complete a shell command as the piped process + if prior_token == '|': + return self.shell_cmd_complete(text, line, begidx, endidx) - # If the prior token is a redirector then perform completion based on that - if raw_tokens[-2] in all_redirects: - - flag_dict = \ - { - '|': self.shell_cmd_complete, - '>': self.path_complete, - '>>': self.path_complete, - '<': self.path_complete - } - return self.flag_based_complete(text, line, begidx, endidx, flag_dict) + # Otherwise do path completion either as files to redirectors or arguments to the piped process + elif prior_token in all_redirects or seen_pipe: + return self.path_complete(text, line, begidx, endidx) # Call the command's completer function return compfunc(text, line, begidx, endidx) |