diff options
author | kotfu <kotfu@kotfu.net> | 2018-04-27 10:09:21 -0600 |
---|---|---|
committer | kotfu <kotfu@kotfu.net> | 2018-04-27 10:09:21 -0600 |
commit | bed0b6be151b2eafd03111cd3782e2672d193443 (patch) | |
tree | 368134105548c83ee224af234081b981be157725 /cmd2 | |
parent | ab74278cc5aa48d62294d648c3813fffe2151937 (diff) | |
download | cmd2-git-bed0b6be151b2eafd03111cd3782e2672d193443.tar.gz |
Create utils module for utility functions
Diffstat (limited to 'cmd2')
-rwxr-xr-x | cmd2/cmd2.py | 26 | ||||
-rw-r--r-- | cmd2/constants.py | 5 | ||||
-rw-r--r-- | cmd2/utils.py | 13 |
3 files changed, 25 insertions, 19 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 184da5a9..1f48ad5a 100755 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -49,7 +49,8 @@ from code import InteractiveConsole import pyparsing import pyperclip -import cmd2.constants as constants +from . import constants +from . import utils # Set up readline from .rl_utils import rl_force_redisplay, readline, rl_type, RlType @@ -396,19 +397,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 @@ -853,7 +841,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): self.commentGrammars.ignore(pyparsing.quotedString).setParseAction(lambda x: '') @@ -3711,13 +3699,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 @@ -3741,13 +3729,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) diff --git a/cmd2/constants.py b/cmd2/constants.py index cfdf7624..6784264f 100644 --- a/cmd2/constants.py +++ b/cmd2/constants.py @@ -2,6 +2,11 @@ # coding=utf-8 """Constants and definitions""" +import re + # Used for command parsing, tab completion and word breaks. Do not change. QUOTES = ['"', "'"] REDIRECTION_CHARS = ['|', '<', '>'] + +# Regular expression to match ANSI escape codes +ANSI_ESCAPE_RE = re.compile(r'\x1b[^m]*m') diff --git a/cmd2/utils.py b/cmd2/utils.py new file mode 100644 index 00000000..33215dc0 --- /dev/null +++ b/cmd2/utils.py @@ -0,0 +1,13 @@ +# +# coding=utf-8 +"""Shared utility functions""" + +from . import constants + +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 constants.ANSI_ESCAPE_RE.sub('', text) |