diff options
Diffstat (limited to 'cmd2.py')
-rwxr-xr-x | cmd2.py | 846 |
1 files changed, 675 insertions, 171 deletions
@@ -27,6 +27,7 @@ import atexit import cmd import codecs import collections +import copy import datetime import functools import glob @@ -36,6 +37,7 @@ import os import platform import re import shlex +import shutil import signal import six import sys @@ -47,6 +49,33 @@ from code import InteractiveConsole import pyparsing import pyperclip +# Collection is a container that is sizable and iterable +# It was introduced in Python 3.6. We will try to import it, otherwise use our implementation +try: + from collections.abc import Collection +except ImportError: + + if six.PY3: + from collections.abc import Sized, Iterable, Container + else: + from collections import Sized, Iterable, Container + + # noinspection PyAbstractClass + class Collection(Sized, Iterable, Container): + + __slots__ = () + + # noinspection PyPep8Naming + @classmethod + def __subclasshook__(cls, C): + if cls is Collection: + if any("__len__" in B.__dict__ for B in C.__mro__) and \ + any("__iter__" in B.__dict__ for B in C.__mro__) and \ + any("__contains__" in B.__dict__ for B in C.__mro__): + return True + return NotImplemented + + # Newer versions of pyperclip are released as a single file, but older versions had a more complicated structure try: from pyperclip.exceptions import PyperclipException @@ -100,6 +129,124 @@ except ImportError: except ImportError: pass +# Load the GNU readline lib so we can make changes to it +readline_lib = None +if not sys.platform.startswith('win'): + import ctypes + from ctypes.util import find_library + + readline_lib_name = find_library("readline") + if readline_lib_name is not None and readline_lib_name: + readline_lib = ctypes.CDLL(readline_lib_name) + +# On Windows, we save the original pyreadline display completion function since we have to override it +else: + # noinspection PyProtectedMember + orig_pyreadline_display = readline.rl.mode._display_completions + +############################################################################################################ +# The following variables are used by tab-completion functions. They are reset each time complete() +# is run, and it is up to the completer functions to set them on a case-by-case basis +# For convenience, use the setter functions for these variables. The setters appear after the variables. +############################################################################################################ + +# If true and a single match is returned to complete(), then a space will be appended +# if the match appears at the end of the line +allow_appended_space = True + +# If true and a single match is returned to complete(), then a closing quote +# will be added if there is an umatched opening quote +allow_closing_quote = True + +########################################################################################################### +# display_entire_match +# If this is True, then the tab completion suggestions will be the entire token that was matched. +# If False, then this works like path matching where only the portion of the completion being matched +# is shown as tab completion suggestions. See the documentation for display_match_delimiter below +# to use a delimiter other than a path slash to determine what portion of the completion to display. +# +# Note: complete_shell() and path_complete() always behave as if this flag is False +# +# display_match_delimiter +# This delimiter can be used to separate matches with something other than a path slash. For instance, +# if you are matching against strings formatted like name::address::zip, you could set the delimiter to '::'. +# That way, the tab completion suggestions will only display the part of that string being completed instead +# of showing the entire string for each completion suggestions. + +# Note: Defaults to os.path.sep (OS specific path slash) +# Note: Only applies when display_entire_match is False +########################################################################################################### +display_entire_match = True +display_match_delimiter = os.path.sep + +# If the tab-completion matches should be displayed in a way that is different than the actual match values, +# then place those results in this list. +matches_to_display = None + + +# Use these functions to set the readline variables +def set_allow_appended_space(allow): + """ + Sets allow_appended_space flag + :param allow: bool - the new value for allow_appended_space + """ + global allow_appended_space + allow_appended_space = allow + + +def set_allow_closing_quote(allow): + """ + Sets allow_closing_quote flag + :param allow: bool - the new value for allow_closing_quote + """ + global allow_closing_quote + allow_closing_quote = allow + + +def set_display_entire_match(entire, delim=os.path.sep): + """ + Sets display_entire_match flag + :param entire: bool - the new value for display_entire_match + :param delim: str - the new value for display_match_delimiter + """ + global display_entire_match + global display_match_delimiter + + display_entire_match = entire + display_match_delimiter = delim + + +def set_matches_to_display(matches): + """ + Sets the tab-completion matches that will be displayed to the screen + :param matches: the matches to display + """ + global matches_to_display + matches_to_display = matches + + +def set_completion_defaults(): + """ + Resets tab completion settings + Called each time complete() is called + """ + set_allow_appended_space(True) + set_allow_closing_quote(True) + set_display_entire_match(True) + set_matches_to_display(None) + + if readline_lib is not None: + # Set GNU readline's rl_basic_quote_characters to NULL so it won't automatically add a closing quote + rl_basic_quote_characters = ctypes.c_char_p.in_dll(readline_lib, "rl_basic_quote_characters") + rl_basic_quote_characters.value = None + + # Set GNU readline's rl_completion_suppress_quote to 1 so it won't automatically add a closing quote + suppress_quote = ctypes.c_int.in_dll(readline_lib, "rl_completion_suppress_quote") + suppress_quote.value = 1 + + +QUOTES = ['"', "'"] + # BrokenPipeError and FileNotFoundError exist only in Python 3. Use IOError for Python 2. if six.PY3: BROKEN_PIPE_ERROR = BrokenPipeError @@ -169,24 +316,136 @@ def set_use_arg_list(val): USE_ARG_LIST = val +def tokens_for_completion(line, begidx, endidx, preserve_quotes=False): + """ + Used by tab completion functions to get all tokens through the one being completed + This also handles tab-completion within quotes + :param line: str - the current input line with leading whitespace removed + :param begidx: int - the beginning index of the prefix text + :param endidx: int - the ending index of the prefix text + :param preserve_quotes - if True, then the tokens will be returned still wrapped in whatever quotes + appeared on the command line. This defaults to False since tab completion routines + generally need the tokens unquoted. The only reason to set this to True would + be to check if the token being completed has an unclosed quote. complete() is the + only functions that does this. + :return: A list of tokens + """ + tokens = [] + unclosed_quote = '' + quotes_to_try = copy.copy(QUOTES) + + tmp_line = line[:endidx] + tmp_endidx = endidx + + while True: + try: + tokens = shlex.split(tmp_line[:tmp_endidx], posix=POSIX_SHLEX) + break + except ValueError: + # ValueError is caused by missing closing quote + if len(quotes_to_try) == 0: + break + + # Add a closing quote and try to parse again + unclosed_quote = quotes_to_try[0] + quotes_to_try = quotes_to_try[1:] + + tmp_line = line[:endidx] + tmp_endidx = endidx + 1 + tmp_line += unclosed_quote + + # No tokens were parsed + if len(tokens) == 0: + return tokens + + if preserve_quotes: + # If the token being completed had an unclosed quote, we need remove the closing quote that was added + if unclosed_quote: + tokens[-1] = tokens[-1][:-1] + + # Unquote all tokens + else: + for index, cur_token in enumerate(tokens): + tokens[index] = strip_quotes(cur_token) + + # Check if we need to append an empty entry as the token being completed + if begidx == endidx and not unclosed_quote: + + # If begidx is the first character in the line, or is preceded by a space + # then we need to add the blank entry to tokens. + prev_space_index = max(line.rfind(' ', 0, begidx), 0) + if prev_space_index == 0 or prev_space_index == begidx - 1: + tokens.append('') + + return tokens + + # noinspection PyUnusedLocal def basic_complete(text, line, begidx, endidx, match_against): """ Performs tab completion against a list + This is ultimately called by many completer functions like flag_based_complete and index_based_complete. + It can also be used by custom completer functions and that is the suggested approach since this function + handles things like tab completions with spaces as well as the display_entire_match flag. + :param text: str - the string prefix we are attempting to match (all returned matches must begin with it) :param line: str - the current input line with leading whitespace removed :param begidx: int - the beginning index of the prefix text :param endidx: int - the ending index of the prefix text - :param match_against: iterable - the list being matched against + :param match_against: Collection - the list being matched against :return: List[str] - a list of possible tab completions """ - completions = [cur_str for cur_str in match_against if cur_str.startswith(text)] + # Make sure we were given an Collection with items to match against + if not isinstance(match_against, Collection) or len(match_against) == 0: + return [] + + # Get all tokens through the one being completed + tokens = tokens_for_completion(line, begidx, endidx) + if len(tokens) == 0: + return [] + + # Perform matching + completion_token = tokens[-1] + matches = [cur_match for cur_match in match_against if cur_match.startswith(completion_token)] + if len(matches) == 0: + return [] + + # Eliminate duplicates + matches_set = set(matches) + matches = list(matches_set) + + # We will only keep where the text value starts + starting_index = len(completion_token) - len(text) + completions = [cur_match[starting_index:] for cur_match in matches] + + # The tab-completion suggestions that will be displayed + display_matches = [] + + # Tab-completion suggestions will show the entire match + if display_entire_match: + display_matches = matches - # If there is only 1 match and it's at the end of the line, then add a space - if len(completions) == 1 and endidx == len(line): - completions[0] += ' ' + # Display only the portion of the match that's still being completed based on delimiter + elif len(matches) > 0: + # Get the common beginning for the matches + common_prefix = os.path.commonprefix(matches) + prefix_tokens = common_prefix.split(display_match_delimiter) + + display_token_index = 0 + if len(prefix_tokens) > 0: + display_token_index = len(prefix_tokens) - 1 + + # Build the display match list + for cur_match in matches: + match_tokens = cur_match.split(display_match_delimiter) + display_token = match_tokens[display_token_index] + if len(display_token) == 0: + display_token = display_match_delimiter + display_matches.append(display_token) + + # Set what matches will display + set_matches_to_display(display_matches) - completions.sort() return completions @@ -203,44 +462,33 @@ def flag_based_complete(text, line, begidx, endidx, flag_dict, all_else=None): values - there are two types of values 1. iterable list of strings to match against (dictionaries, lists, etc.) 2. function that performs tab completion (ex: path_complete) - :param all_else: iterable or function - an optional parameter for tab completing any token that isn't preceded - by a flag in flag_dict + :param all_else: Collection or function - an optional parameter for tab completing any token that isn't preceded + by a flag in flag_dict :return: List[str] - a list of possible tab completions """ - # Get all tokens prior to token being completed - try: - prev_space_index = max(line.rfind(' ', 0, begidx), 0) - tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX) - except ValueError: - # Invalid syntax for shlex (Probably due to missing closing quote) - return [] - + # Get all tokens through the one being completed + tokens = tokens_for_completion(line, begidx, endidx) if len(tokens) == 0: return [] completions = [] match_against = all_else - # Must have at least the command and one argument for a flag to be present + # Must have at least 2 args for a flag to precede the token being completed if len(tokens) > 1: - flag = tokens[-1] + flag = tokens[-2] if flag in flag_dict: match_against = flag_dict[flag] - # Perform tab completion using an iterable - if isinstance(match_against, collections.Iterable): - completions = [cur_str for cur_str in match_against if cur_str.startswith(text)] - - # If there is only 1 match and it's at the end of the line, then add a space - if len(completions) == 1 and endidx == len(line): - completions[0] += ' ' + # Perform tab completion using an Collection + if isinstance(match_against, Collection): + completions = basic_complete(text, line, begidx, endidx, match_against) # Perform tab completion using a function elif callable(match_against): completions = match_against(text, line, begidx, endidx) - completions.sort() return completions @@ -257,26 +505,20 @@ def index_based_complete(text, line, begidx, endidx, index_dict, all_else=None): values - there are two types of values 1. iterable list of strings to match against (dictionaries, lists, etc.) 2. function that performs tab completion (ex: path_complete) - :param all_else: iterable or function - an optional parameter for tab completing any token that isn't at an - index in index_dict + :param all_else: Collection or function - an optional parameter for tab completing any token that isn't at an + index in index_dict :return: List[str] - a list of possible tab completions """ - # Get all tokens prior to token being completed - try: - prev_space_index = max(line.rfind(' ', 0, begidx), 0) - tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX) - except ValueError: - # Invalid syntax for shlex (Probably due to missing closing quote) - return [] - + # Get all tokens through the one being completed + tokens = tokens_for_completion(line, begidx, endidx) if len(tokens) == 0: return [] completions = [] # Get the index of the token being completed - index = len(tokens) + index = len(tokens) - 1 # Check if token is at an index in the dictionary if index in index_dict: @@ -284,19 +526,14 @@ def index_based_complete(text, line, begidx, endidx, index_dict, all_else=None): else: match_against = all_else - # Perform tab completion using an iterable - if isinstance(match_against, collections.Iterable): - completions = [cur_str for cur_str in match_against if cur_str.startswith(text)] - - # If there is only 1 match and it's at the end of the line, then add a space - if len(completions) == 1 and endidx == len(line): - completions[0] += ' ' + # Perform tab completion using an Collection + if isinstance(match_against, Collection): + completions = basic_complete(text, line, begidx, endidx, match_against) # Perform tab completion using a function elif callable(match_against): completions = match_against(text, line, begidx, endidx) - completions.sort() return completions @@ -312,14 +549,8 @@ def path_complete(text, line, begidx, endidx, dir_exe_only=False, dir_only=False :return: List[str] - a list of possible tab completions """ - # Get all tokens prior to token being completed - try: - prev_space_index = max(line.rfind(' ', 0, begidx), 0) - tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX) - except ValueError: - # Invalid syntax for shlex (Probably due to missing closing quote) - return [] - + # Get all tokens through the one being completed + tokens = tokens_for_completion(line, begidx, endidx) if len(tokens) == 0: return [] @@ -328,36 +559,64 @@ def path_complete(text, line, begidx, endidx, dir_exe_only=False, dir_only=False if endidx == len(line) or (endidx < len(line) and line[endidx] != os.path.sep): add_trailing_sep_if_dir = True - # Readline places begidx after ~ and path separators (/) so we need to extract any directory - # path that appears before the search text - dirname = line[prev_space_index + 1:begidx] + completion_token = tokens[-1] + + # Used to replace cwd in the final results + cwd = os.getcwd() + cwd_added = False - # If no directory path and no search text has been entered, then search in the CWD for * - if not dirname and not text: + # Used to replace ~ in the final results + user_path = os.path.expanduser('~') + tilde_expanded = False + + # If the token being completed is blank, then search in the CWD for * + if not completion_token: search_str = os.path.join(os.getcwd(), '*') + cwd_added = True else: # Purposely don't match any path containing wildcards - what we are doing is complicated enough! wildcards = ['*', '?'] for wildcard in wildcards: - if wildcard in dirname or wildcard in text: + if wildcard in completion_token: return [] - if not dirname: - dirname = os.getcwd() - elif dirname == '~': - # If a ~ was used without a separator between text, then this is invalid - if text: + # Used if we need to prepend a directory to the search string + dirname = '' + + # If the user only entered a '~', then complete it with a slash + if completion_token == '~': + # This is a directory, so don't add a space or quote + set_allow_appended_space(False) + set_allow_closing_quote(False) + return [completion_token + os.path.sep] + + elif completion_token.startswith('~'): + # Tilde without separator between path is invalid + if not completion_token.startswith('~' + os.path.sep): return [] - # If only a ~ was entered, then complete it with a slash - else: - return [os.path.sep] + + # Mark that we are expanding a tilde + tilde_expanded = True + + # If the token does not have a directory, then use the cwd + elif not os.path.dirname(completion_token): + dirname = os.getcwd() + cwd_added = True # Build the search string - search_str = os.path.join(dirname, text + '*') + search_str = os.path.join(dirname, completion_token + '*') # Expand "~" to the real user directory search_str = os.path.expanduser(search_str) + # If the text being completed does not appear at the beginning of the token being completed, + # which can happen if there are spaces, save off the index where our search text begins in the + # search string so we can return only that portion of the completed paths to readline + if len(completion_token) - len(text) > 0: + starting_index = search_str.rfind(text + '*') + else: + starting_index = 0 + # Find all matching path completions path_completions = glob.glob(search_str) @@ -367,24 +626,37 @@ def path_complete(text, line, begidx, endidx, dir_exe_only=False, dir_only=False elif dir_only: path_completions = [c for c in path_completions if os.path.isdir(c)] - # Get the basename of the paths + # Don't append a space or closing quote to directory + if len(path_completions) == 1 and not os.path.isfile(path_completions[0]): + set_allow_appended_space(False) + set_allow_closing_quote(False) + + # We will display only the basename of paths in the tab-completion suggestions + display_matches = [os.path.basename(cur_completion) for cur_completion in path_completions] + + # Extract just the completed text portion of the paths for completions completions = [] - for c in path_completions: - basename = os.path.basename(c) + for cur_completion in path_completions: # Add a separator after directories if the next character isn't already a separator - if os.path.isdir(c) and add_trailing_sep_if_dir: - basename += os.path.sep + if os.path.isdir(cur_completion) and add_trailing_sep_if_dir: + cur_completion += os.path.sep + + # Only keep where text started + completions.append(cur_completion[starting_index:]) + + # Remove cwd if it was added + if cwd_added: + completions = [cur_path.replace(cwd + os.path.sep, '', 1) for cur_path in completions] - completions.append(basename) + # Restore a tilde if we expanded one + if tilde_expanded: + completions = [cur_path.replace(user_path, '~', 1) for cur_path in completions] + display_matches = [cur_path.replace(user_path, '~', 1) for cur_path in display_matches] - # If there is a single completion - if len(completions) == 1: - # If it is a file and we are at the end of the line, then add a space - if os.path.isfile(path_completions[0]) and endidx == len(line): - completions[0] += ' ' + # Set the matches that will display as tab-completion suggestions + set_matches_to_display(display_matches) - completions.sort() return completions @@ -1281,6 +1553,7 @@ class Cmd(cmd.Cmd): # Attempt to detect if we are not running within a fully functional terminal. # Don't try to use the pager when being run by a continuous integration system like Jenkins + pexpect. functional_terminal = False + if self.stdin.isatty() and self.stdout.isatty(): if sys.platform.startswith('win') or os.environ.get('TERM') is not None: functional_terminal = True @@ -1288,6 +1561,7 @@ class Cmd(cmd.Cmd): # Don't attempt to use a pager that can block if redirecting or running a script (either text or Python) # Also only attempt to use a pager if actually running in a real fully functional terminal if functional_terminal and not self.redirecting and not self._in_py and not self._script_dir: + if sys.platform.startswith('win'): pager_cmd = 'more' else: @@ -1333,20 +1607,6 @@ class Cmd(cmd.Cmd): return self._colorcodes[color][True] + val + self._colorcodes[color][False] return val - # ----- Methods which override stuff in cmd ----- - - # noinspection PyMethodOverriding - def completenames(self, text, line, begidx, endidx): - """Override of cmd method which completes command names both for command completion and help.""" - # Call super class method. Need to do it this way for Python 2 and 3 compatibility - cmd_completion = cmd.Cmd.completenames(self, text) - - # If we are completing the initial command name and get exactly 1 result and are at end of line, add a space - if begidx == 0 and len(cmd_completion) == 1 and endidx == len(line): - cmd_completion[0] += ' ' - - return cmd_completion - def get_subcommands(self, command): """ Returns a list of a command's subcommands if they exist @@ -1366,6 +1626,66 @@ class Cmd(cmd.Cmd): return subcommand_names + # noinspection PyUnusedLocal + def _display_matches_gnu_readline(self, substitution, matches, longest_match_length): + """ + A custom completion match display function for use with GNU readline + :param substitution: the search text that was replaced + :param matches: the tab completion matches to display + :param longest_match_length: length of the longest match + """ + if matches_to_display is None: + display_list = matches + else: + display_list = matches_to_display + + # Eliminate duplicates and sort + display_set = set(display_list) + display_list = list(display_set) + display_list.sort() + + # Print the matches + self.poutput("\n") + + if sys.version_info >= (3, 3): + num_cols = shutil.get_terminal_size().columns + else: + proc = subprocess.Popen('stty size', shell=True, stdout=subprocess.PIPE) + out, err = proc.communicate() + if six.PY2: + rows, columns = out.split() + else: + rows, columns = out.decode().split() + num_cols = int(columns) + + self.columnize(display_list, num_cols) + + # Print the prompt + self.poutput(self.prompt, readline.get_line_buffer()) + sys.stdout.flush() + + @staticmethod + def _display_matches_pyreadline(matches): + """ + A custom completion match display function for use with pyreadline + :param matches: the tab completion matches to display + """ + if orig_pyreadline_display is not None: + if matches_to_display is None: + display_list = matches + else: + display_list = matches_to_display + + # Eliminate duplicates and sort + display_set = set(display_list) + display_list = list(display_set) + display_list.sort() + + # Display the matches + orig_pyreadline_display(display_list) + + # ----- Methods which override stuff in cmd ----- + def complete(self, text, state): """Override of command method which returns the next possible completion for 'text'. @@ -1380,23 +1700,49 @@ class Cmd(cmd.Cmd): :param text: str - the current word that user is typing :param state: int - non-negative integer """ + if state == 0: + unclosed_quote = '' + set_completion_defaults() + + # GNU readline specific way to override the completions display function + if readline_lib: + readline.set_completion_display_matches_hook(self._display_matches_gnu_readline) + + # pyreadline specific way to override the completions display function + elif sys.platform.startswith('win'): + readline.rl.mode._display_completions = self._display_matches_pyreadline + origline = readline.get_line_buffer() line = origline.lstrip() stripped = len(origline) - len(line) begidx = readline.get_begidx() - stripped endidx = readline.get_endidx() - stripped - # If begidx is greater than 0, then the cursor is past the command + # If the line starts with a shortcut that has no break between the search text, + # then the text variable will start with the shortcut if its not a completer delimiter + shortcut_to_restore = '' + if begidx == 0: + for (shortcut, expansion) in self.shortcuts: + if text.startswith(shortcut): + # Save the shortcut to adjust the line later + shortcut_to_restore = shortcut + + # Adjust text and where it begins + text = text[len(shortcut_to_restore):] + begidx += len(shortcut_to_restore) + break + + # If begidx is greater than 0, then we are no longer completing the command if begidx > 0: # Parse the command line command, args, expanded_line = self.parseline(line) # We overwrote line with a properly formatted but fully stripped version - # Restore the end spaces from the original since line is only supposed to be - # lstripped when passed to completer functions according to Python docs - rstripped_len = len(origline) - len(origline.rstrip()) + # Restore the end spaces since line is only supposed to be lstripped when + # passed to completer functions according to Python docs + rstripped_len = len(line) - len(line.rstrip()) expanded_line += ' ' * rstripped_len # Fix the index values if expanded_line has a different size than line @@ -1408,18 +1754,48 @@ class Cmd(cmd.Cmd): # Overwrite line to pass into completers line = expanded_line - if command == '': - compfunc = self.completedefault - else: + # Get all tokens through the one being completed + tokens = tokens_for_completion(line, begidx, endidx, preserve_quotes=True) + + # Get the status of quotes in the token being completed + completion_token = tokens[-1] + + if len(completion_token) == 1: + # Check if the token being completed has an unclosed quote + first_char = completion_token[0] + if first_char in QUOTES: + unclosed_quote = first_char + + elif len(completion_token) > 1: + first_char = completion_token[0] + last_char = completion_token[-1] + + # Check if the token being completed has an unclosed quote + if first_char in QUOTES and first_char != last_char: + unclosed_quote = first_char - # Get the completion function for this command + # If the cursor is right after a closed quote, then insert a space + else: + prior_char = line[begidx - 1] + if not unclosed_quote and prior_char in QUOTES: + self.completion_matches = [' '] + return self.completion_matches[state] + + # Check if a valid command was entered + if command not in self.get_command_names(): + # Check if this command should be run as a shell command + if self.default_to_shell and command in self._get_exes_in_path(command): + compfunc = functools.partial(path_complete) + else: + compfunc = self.completedefault + + # A valid command was entered + else: + # Get the completer function for this command try: compfunc = getattr(self, 'complete_' + command) except AttributeError: - if self.default_to_shell and command in self._get_exes_in_path(command): - compfunc = functools.partial(path_complete) - else: - compfunc = self.completedefault + compfunc = self.completedefault # If there are subcommands, then try completing those if the cursor is in # the token at index 1, otherwise default to using compfunc @@ -1433,21 +1809,113 @@ class Cmd(cmd.Cmd): # Call the completer function self.completion_matches = compfunc(text, line, begidx, endidx) + if len(self.completion_matches) > 0: + + # Get the common prefix of all matches. This is what is added to the token being completed. + common_prefix = os.path.commonprefix(self.completion_matches) + + # Check if we need to add an opening quote + if len(completion_token) == 0 or completion_token[0] not in QUOTES: + + # If anything that will be in the token being completed contains a space, then + # we must add an opening quote to the token on screen + if ' ' in completion_token or ' ' in common_prefix: + + # Mark that there is now an unclosed quote + unclosed_quote = '"' + + # Find in the original line on screen where our token starts + starting_index = 0 + for token_index, cur_token in enumerate(tokens): + starting_index = origline.find(cur_token) + if token_index < len(tokens) - 1: + starting_index += len(cur_token) + + # Get where text started in the original line. + # Account for whether we shifted begidx due to a shortcut. + orig_begidx = readline.get_begidx() + if shortcut_to_restore: + orig_begidx += len(shortcut_to_restore) + + # If the token started at begidx, then all we have to do is prepend + # an opening quote to all the completions. Readline will do the rest. + # This is always true in the case where there is a shortcut to restore. + if starting_index == orig_begidx: + self.completion_matches = ['"' + match for match in self.completion_matches] + + # The token started after begidx, therefore we need to manually insert an + # opening quote before the token in the readline buffer. + else: + # GNU readline specific way to insert an opening quote + if readline_lib: + # Get and save the current cursor position + rl_point = ctypes.c_int.in_dll(readline_lib, "rl_point") + orig_rl_point = rl_point.value + + # Move the cursor where the token being completed begins to insert the opening quote + rl_point.value = starting_index + readline.insert_text('"') + + # Restore the cursor 1 past where it was the since we shifted everything + rl_point.value = orig_rl_point + 1 + + # Since we just shifted the whole command line over by one, readline will begin + # inserting the text one spot to the left of where we want it since it still has + # the original values of begidx and endidx and we can't change them. Therefore we + # must prepend to every match the character right before the text variable so it + # doesn't get erased. + saved_char = completion_token[(len(text) + 1) * -1] + self.completion_matches = [saved_char + match for match in self.completion_matches] + + # pyreadline specific way to insert an opening quote + elif sys.platform.startswith('win'): + # Save the current cursor position + orig_rl_point = readline.rl.mode.l_buffer.point + + # Move the cursor where the token being completed begins to insert the opening quote + readline.rl.mode.l_buffer.point = starting_index + readline.insert_text('"') + + # Shift the cursor and completion indexes by 1 to account for the added quote + readline.rl.mode.l_buffer.point = orig_rl_point + 1 + readline.rl.mode.begidx += 1 + readline.rl.mode.endidx += 1 + + # Check if we need to restore a shortcut in the tab completions + if shortcut_to_restore: + # If matches_to_display has not been set, then set it to self.completion_matches + # before we restore the shortcut so the tab completion suggestions that display to + # the user don't have the shortcut character. + if matches_to_display is None: + set_matches_to_display(self.completion_matches) + + # Prepend all tab completions with the shortcut so it doesn't get erased from the command line + self.completion_matches = [shortcut_to_restore + match for match in self.completion_matches] + else: # Complete the command against aliases and command names strs_to_match = list(self.aliases.keys()) - - # Add command names strs_to_match.extend(self.get_command_names()) + self.completion_matches = basic_complete(text, line, begidx, endidx, strs_to_match) + + # Eliminate duplicates and sort + matches_set = set(self.completion_matches) + self.completion_matches = list(matches_set) + self.completion_matches.sort() + + # Handle single result + if len(self.completion_matches) == 1: + str_to_append = '' - # Perform matching - completions = [cur_str for cur_str in strs_to_match if cur_str.startswith(text)] + # Add a closing quote if needed + if allow_closing_quote and unclosed_quote: + str_to_append += unclosed_quote - # If there is only 1 match and it's at the end of the line, then add a space - if len(completions) == 1 and endidx == len(line): - completions[0] += ' ' + # If we are at the end of the line, then add a space + if allow_appended_space and endidx == len(line): + str_to_append += ' ' - self.completion_matches = completions + self.completion_matches[0] = self.completion_matches[0] + str_to_append try: return self.completion_matches[state] @@ -1463,34 +1931,33 @@ class Cmd(cmd.Cmd): Override of parent class method to handle tab completing subcommands """ - # Get all tokens prior to token being completed - try: - prev_space_index = max(line.rfind(' ', 0, begidx), 0) - tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX) - except ValueError: - # Invalid syntax for shlex (Probably due to missing closing quote) + # The command is the token at index 1 in the command line + cmd_index = 1 + + # The subcommand is the token at index 2 in the command line + subcmd_index = 2 + + # Get all tokens through the one being completed + tokens = tokens_for_completion(line, begidx, endidx) + if len(tokens) == 0: return [] completions = [] - # If we have "help" and a completed command token, then attempt to match subcommands - if len(tokens) == 2: - - # Match subcommands if any exist - subcommands = self.get_subcommands(tokens[1]) - if subcommands is not None: - completions = [cur_sub for cur_sub in subcommands if cur_sub.startswith(text)] + # Get the index of the token being completed + index = len(tokens) - 1 - # Run normal help completion from the parent class - else: + # Check if we are completing a command + if index == cmd_index: completions = cmd.Cmd.complete_help(self, text, line, begidx, endidx) - # If only 1 command has been matched and it's at the end of the line, - # then add a space if it has subcommands - if len(completions) == 1 and endidx == len(line) and self.get_subcommands(completions[0]) is not None: - completions[0] += ' ' + # Check if we are completing a subcommand + elif index == subcmd_index: + + # Match subcommands if any exist + command = tokens[cmd_index] + completions = basic_complete(text, line, begidx, endidx, self.get_subcommands(command)) - completions.sort() return completions # noinspection PyUnusedLocal @@ -1502,8 +1969,10 @@ class Cmd(cmd.Cmd): :param signum: int - signal number :param frame """ + # Save copy of pipe_proc since it could theoretically change while this is running pipe_proc = self.pipe_proc + if pipe_proc is not None: pipe_proc.terminate() @@ -1511,7 +1980,8 @@ class Cmd(cmd.Cmd): raise KeyboardInterrupt("Got a keyboard interrupt") def preloop(self): - """Hook method executed once when the cmdloop() method is called.""" + """"Hook method executed once when the cmdloop() method is called.""" + # Register a default SIGINT signal handler for Ctrl+C signal.signal(signal.SIGINT, self.sigint_handler) @@ -1951,8 +2421,9 @@ class Cmd(cmd.Cmd): self.old_completer = readline.get_completer() self.old_delims = readline.get_completer_delims() readline.set_completer(self.complete) - # Don't treat "-" as a readline delimiter since it is commonly used in filesystem paths - readline.set_completer_delims(self.old_delims.replace('-', '')) + + # Use the same completer delimiters as Bash + readline.set_completer_delims(" \t\n\"'@><=;|&(:") readline.parse_and_bind(self.completekey + ": complete") except NameError: pass @@ -2283,7 +2754,30 @@ Usage: Usage: unalias [-a] name [name ...] """Execute a command as if at the OS prompt. Usage: shell <command> [arguments]""" - proc = subprocess.Popen(command, stdout=self.stdout, shell=True) + + try: + tokens = shlex.split(command, posix=POSIX_SHLEX) + except ValueError as err: + self.perror(err, traceback_war=False) + return + + for index, _ in enumerate(tokens): + if len(tokens[index]) > 0: + # Check if the token is quoted. Since shlex.split() passed, there isn't + # an unclosed quote, so we only need to check the first character. + first_char = tokens[index][0] + if first_char in QUOTES: + tokens[index] = strip_quotes(tokens[index]) + + tokens[index] = os.path.expandvars(tokens[index]) + tokens[index] = os.path.expanduser(tokens[index]) + + # Restore the quotes + if first_char in QUOTES: + tokens[index] = first_char + tokens[index] + first_char + + expanded_command = ' '.join(tokens) + proc = subprocess.Popen(expanded_command, stdout=self.stdout, shell=True) proc.communicate() @staticmethod @@ -2329,39 +2823,43 @@ Usage: Usage: unalias [-a] name [name ...] :return: List[str] - a list of possible tab completions """ - # Get all tokens prior to token being completed - try: - prev_space_index = max(line.rfind(' ', 0, begidx), 0) - tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX) - except ValueError: - # Invalid syntax for shlex (Probably due to missing closing quote) - return [] + # The shell command is the token at index 1 in the command line + shell_cmd_index = 1 + # Get all tokens through the one being completed + tokens = tokens_for_completion(line, begidx, endidx) if len(tokens) == 0: return [] - # Check if we are still completing the shell command - if len(tokens) == 1: + # Get the index of the token being completed + index = len(tokens) - 1 - # Readline places begidx after ~ and path separators (/) so we need to get the whole token - # and see if it begins with a possible path in case we need to do path completion - # to find the shell command executables - cmd_token = line[prev_space_index + 1:begidx + 1] + if index < shell_cmd_index: + return [] + + # Complete the shell command + elif index == shell_cmd_index: + + completion_token = tokens[index] # Don't tab complete anything if no shell command has been started - if len(cmd_token) == 0: + if len(completion_token) == 0: return [] - # Look for path characters in the token - if not (cmd_token.startswith('~') or os.path.sep in cmd_token): - # No path characters are in this token, it is OK to try shell command completion. - command_completions = self._get_exes_in_path(text) + # If there are no path characters in this token, it is OK to try shell command completion. + if not (completion_token.startswith('~') or os.path.sep in completion_token): + exes = self._get_exes_in_path(completion_token) - if command_completions: - # If there is only 1 match and it's at the end of the line, then add a space - if len(command_completions) == 1 and endidx == len(line): - command_completions[0] += ' ' - return command_completions + # We will only keep where the text value starts for the tab completions + starting_index = len(completion_token) - len(text) + completions = [cur_exe[starting_index:] for cur_exe in exes] + + # Use the full name of the executables for the completions that are displayed + display_matches = exes + set_matches_to_display(display_matches) + + if completions: + return completions # If we have no results, try path completion to find the shell commands return path_complete(text, line, begidx, endidx, dir_exe_only=True) @@ -2407,18 +2905,15 @@ Usage: Usage: unalias [-a] name [name ...] # The subcommand is the token at index 1 in the command line subcmd_index = 1 - # Get all tokens prior to token being completed - try: - prev_space_index = max(line.rfind(' ', 0, begidx), 0) - tokens = shlex.split(line[:prev_space_index], posix=POSIX_SHLEX) - except ValueError: - # Invalid syntax for shlex (Probably due to missing closing quote) + # Get all tokens through the one being completed + tokens = tokens_for_completion(line, begidx, endidx) + if len(tokens) == 0: return [] completions = [] # Get the index of the token being completed - index = len(tokens) + index = len(tokens) - 1 # If the token being completed is past the subcommand name, then do subcommand specific tab-completion if index > subcmd_index: @@ -2561,7 +3056,10 @@ Paths or arguments that contain spaces must be enclosed in quotes sys.argv = orig_args # Enable tab-completion for pyscript command - complete_pyscript = functools.partial(path_complete) + @staticmethod + def complete_pyscript(text, line, begidx, endidx): + index_dict = {1: path_complete} + return index_based_complete(text, line, begidx, endidx, index_dict) # Only include the do_ipy() method if IPython is available on the system if ipython_available: @@ -2703,7 +3201,10 @@ The editor used is determined by the ``editor`` settable parameter. os.system('"{}"'.format(self.editor)) # Enable tab-completion for edit command - complete_edit = functools.partial(path_complete) + @staticmethod + def complete_edit(text, line, begidx, endidx): + index_dict = {1: path_complete} + return index_based_complete(text, line, begidx, endidx, index_dict) @property def _current_script_dir(self): @@ -2792,7 +3293,10 @@ Script should contain one command per line, just like command would be typed in self._script_dir.append(os.path.dirname(expanded_path)) # Enable tab-completion for load command - complete_load = functools.partial(path_complete) + @staticmethod + def complete_load(text, line, begidx, endidx): + index_dict = {1: path_complete} + return index_based_complete(text, line, begidx, endidx, index_dict) @staticmethod def is_text_file(file_path): |