summaryrefslogtreecommitdiff
path: root/cmd2/cmd2.py
diff options
context:
space:
mode:
Diffstat (limited to 'cmd2/cmd2.py')
-rwxr-xr-xcmd2/cmd2.py50
1 files changed, 18 insertions, 32 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 89e07802..630e79b4 100755
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -48,6 +48,9 @@ from code import InteractiveConsole
import pyperclip
+from . import constants
+from . import utils
+
# Set up readline
from .rl_utils import rl_force_redisplay, readline, rl_type, RlType
from .argparse_completer import AutoCompleter, ACArgumentParser
@@ -119,10 +122,6 @@ except ImportError:
__version__ = '0.9.0'
-# Used for tab completion and word breaks. Do not change.
-QUOTES = ['"', "'"]
-REDIRECTION_CHARS = ['|', '<', '>']
-
# optional attribute, when tagged on a function, allows cmd2 to categorize commands
HELP_CATEGORY = 'help_category'
HELP_SUMMARY = 'help_summary'
@@ -330,19 +329,6 @@ class EmptyStatement(Exception):
pass
-# Regular expression to match ANSI escape codes
-ANSI_ESCAPE_RE = re.compile(r'\x1b[^m]*m')
-
-
-def strip_ansi(text: str) -> str:
- """Strip ANSI escape codes from a string.
-
- :param text: string which may contain ANSI escape codes
- :return: the same string with any ANSI escape codes removed
- """
- return ANSI_ESCAPE_RE.sub('', text)
-
-
def _pop_readline_history(clear_history: bool=True) -> List[str]:
"""Returns a copy of readline's history and optionally clears it (default)"""
# noinspection PyArgumentList
@@ -782,7 +768,7 @@ class Cmd(cmd.Cmd):
:return: str - prompt stripped of any ANSI escape codes
"""
- return strip_ansi(self.prompt)
+ return utils.strip_ansi(self.prompt)
def _finalize_app_parameters(self):
# noinspection PyUnresolvedReferences
@@ -947,7 +933,7 @@ class Cmd(cmd.Cmd):
Both items are None
"""
unclosed_quote = ''
- quotes_to_try = copy.copy(QUOTES)
+ quotes_to_try = copy.copy(constants.QUOTES)
tmp_line = line[:endidx]
tmp_endidx = endidx
@@ -990,7 +976,7 @@ class Cmd(cmd.Cmd):
for cur_initial_token in initial_tokens:
# Save tokens up to 1 character in length or quoted tokens. No need to parse these.
- if len(cur_initial_token) <= 1 or cur_initial_token[0] in QUOTES:
+ if len(cur_initial_token) <= 1 or cur_initial_token[0] in constants.QUOTES:
raw_tokens.append(cur_initial_token)
continue
@@ -1002,10 +988,10 @@ class Cmd(cmd.Cmd):
cur_raw_token = ''
while True:
- if cur_char not in REDIRECTION_CHARS:
+ if cur_char not in constants.REDIRECTION_CHARS:
# Keep appending to cur_raw_token until we hit a redirect char
- while cur_char not in REDIRECTION_CHARS:
+ while cur_char not in constants.REDIRECTION_CHARS:
cur_raw_token += cur_char
cur_index += 1
if cur_index < len(cur_initial_token):
@@ -1411,7 +1397,7 @@ class Cmd(cmd.Cmd):
if len(raw_tokens) > 1:
# Build a list of all redirection tokens
- all_redirects = REDIRECTION_CHARS + ['>>']
+ all_redirects = constants.REDIRECTION_CHARS + ['>>']
# Check if there are redirection strings prior to the token being completed
seen_pipe = False
@@ -1626,7 +1612,7 @@ class Cmd(cmd.Cmd):
raw_completion_token = raw_tokens[-1]
# Check if the token being completed has an opening quote
- if raw_completion_token and raw_completion_token[0] in QUOTES:
+ if raw_completion_token and raw_completion_token[0] in constants.QUOTES:
# Since the token is still being completed, we know the opening quote is unclosed
unclosed_quote = raw_completion_token[0]
@@ -2349,11 +2335,11 @@ class Cmd(cmd.Cmd):
readline.set_completer(self.complete)
# Break words on whitespace and quotes when tab completing
- completer_delims = " \t\n" + ''.join(QUOTES)
+ completer_delims = " \t\n" + ''.join(constants.QUOTES)
if self.allow_redirection:
# If redirection is allowed, then break words on those characters too
- completer_delims += ''.join(REDIRECTION_CHARS)
+ completer_delims += ''.join(constants.REDIRECTION_CHARS)
readline.set_completer_delims(completer_delims)
@@ -2793,13 +2779,13 @@ Usage: Usage: unalias [-a] name [name ...]
# 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:
+ if first_char in constants.QUOTES:
tokens[index] = strip_quotes(tokens[index])
tokens[index] = os.path.expanduser(tokens[index])
# Restore the quotes
- if first_char in QUOTES:
+ if first_char in constants.QUOTES:
tokens[index] = first_char + tokens[index] + first_char
expanded_command = ' '.join(tokens)
@@ -3505,13 +3491,13 @@ class Cmd2TestCase(unittest.TestCase):
def _test_transcript(self, fname, transcript):
line_num = 0
finished = False
- line = strip_ansi(next(transcript))
+ line = utils.strip_ansi(next(transcript))
line_num += 1
while not finished:
# Scroll forward to where actual commands begin
while not line.startswith(self.cmdapp.visible_prompt):
try:
- line = strip_ansi(next(transcript))
+ line = utils.strip_ansi(next(transcript))
except StopIteration:
finished = True
break
@@ -3535,13 +3521,13 @@ class Cmd2TestCase(unittest.TestCase):
self.cmdapp.onecmd_plus_hooks(command)
result = self.cmdapp.stdout.read()
# Read the expected result from transcript
- if strip_ansi(line).startswith(self.cmdapp.visible_prompt):
+ if utils.strip_ansi(line).startswith(self.cmdapp.visible_prompt):
message = '\nFile {}, line {}\nCommand was:\n{}\nExpected: (nothing)\nGot:\n{}\n'.format(
fname, line_num, command, result)
self.assert_(not (result.strip()), message)
continue
expected = []
- while not strip_ansi(line).startswith(self.cmdapp.visible_prompt):
+ while not utils.strip_ansi(line).startswith(self.cmdapp.visible_prompt):
expected.append(line)
try:
line = next(transcript)