diff options
author | Matthäus G. Chajdas <dev@anteru.net> | 2019-11-24 17:22:37 +0100 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2019-11-25 12:12:58 +0100 |
commit | 3f403687036fce8c9f3d49a5bb2a8bbcdc41c8ba (patch) | |
tree | a8195b60e3ef69160c117c461b017da5616065f2 | |
parent | 1dc6c8e2b473717188632f9a0c28c4342428e684 (diff) | |
download | pygments-git-3f403687036fce8c9f3d49a5bb2a8bbcdc41c8ba.tar.gz |
Fix #1253.
-rw-r--r-- | CHANGES | 1 | ||||
-rw-r--r-- | pygments/lexers/shell.py | 30 | ||||
-rw-r--r-- | tests/test_shell.py | 19 |
3 files changed, 42 insertions, 8 deletions
@@ -18,6 +18,7 @@ Version 2.5.0 - Updated lexers: * Apache2 Configuration (PR#1251) + * Bash sessions (#1253) * CSound (PR#1250) * Handlebars (PR#773) * Python3 (PR#1255) diff --git a/pygments/lexers/shell.py b/pygments/lexers/shell.py index 972c4004..c12cb3f1 100644 --- a/pygments/lexers/shell.py +++ b/pygments/lexers/shell.py @@ -154,6 +154,9 @@ class ShellSessionBaseLexer(Lexer): .. versionadded:: 2.1 """ + + _venv = re.compile(r'^(\([^)]*\))(\s*)') + def get_tokens_unprocessed(self, text): innerlexer = self._innerLexerCls(**self.options) @@ -164,11 +167,24 @@ class ShellSessionBaseLexer(Lexer): for match in line_re.finditer(text): line = match.group() - m = re.match(self._ps1rgx, line) if backslash_continuation: curcode += line backslash_continuation = curcode.endswith('\\\n') - elif m: + continue + + venv_match = self._venv.match(line) + if venv_match: + venv = venv_match.group(1) + venv_whitespace = venv_match.group(2) + insertions.append((len(curcode), + [(0, Generic.Prompt.VirtualEnv, venv)])) + if venv_whitespace: + insertions.append((len(curcode), + [(0, Text, venv_whitespace)])) + line = line[venv_match.end():] + + m = self._ps1rgx.match(line) + if m: # To support output lexers (say diff output), the output # needs to be broken by prompts whenever the output lexer # changes. @@ -211,9 +227,9 @@ class BashSessionLexer(ShellSessionBaseLexer): mimetypes = ['application/x-shell-session', 'application/x-sh-session'] _innerLexerCls = BashLexer - _ps1rgx = \ + _ps1rgx = re.compile( r'^((?:(?:\[.*?\])|(?:\(\S+\))?(?:| |sh\S*?|\w+\S+[@:]\S+(?:\s+\S+)' \ - r'?|\[\S+[@:][^\n]+\].+))\s*[$#%])(.*\n?)' + r'?|\[\S+[@:][^\n]+\].+))\s*[$#%])(.*\n?)') _ps2 = '>' @@ -540,7 +556,7 @@ class MSDOSSessionLexer(ShellSessionBaseLexer): mimetypes = [] _innerLexerCls = BatchLexer - _ps1rgx = r'^([^>]*>)(.*\n?)' + _ps1rgx = re.compile(r'^([^>]*>)(.*\n?)') _ps2 = 'More? ' @@ -625,7 +641,7 @@ class TcshSessionLexer(ShellSessionBaseLexer): mimetypes = [] _innerLexerCls = TcshLexer - _ps1rgx = r'^([^>]+>)(.*\n?)' + _ps1rgx = re.compile(r'^([^>]+>)(.*\n?)') _ps2 = '? ' @@ -756,7 +772,7 @@ class PowerShellSessionLexer(ShellSessionBaseLexer): mimetypes = [] _innerLexerCls = PowerShellLexer - _ps1rgx = r'^(PS [^>]+> )(.*\n?)' + _ps1rgx = re.compile(r'^(PS [^>]+> )(.*\n?)') _ps2 = '>> ' diff --git a/tests/test_shell.py b/tests/test_shell.py index bbb3da35..64918a2e 100644 --- a/tests/test_shell.py +++ b/tests/test_shell.py @@ -134,7 +134,7 @@ def test_end_of_line_nums(lexer_bash): assert list(lexer_bash.get_tokens(fragment)) == tokens -def test_needs_name(lexer_session): +def test_newline_in_echo(lexer_session): fragment = u'$ echo \\\nhi\nhi\n' tokens = [ (Token.Text, u''), @@ -162,3 +162,20 @@ def test_msdos_gt_only(lexer_msdos): (Token.Generic.Output, u'hi\n'), ] assert list(lexer_msdos.get_tokens(fragment)) == tokens + +def test_virtualenv(lexer_session): + fragment = u'(env) [~/project]$ foo -h\n' + tokens = [ + (Token.Text, u''), + (Token.Generic.Prompt.VirtualEnv, u'(env)'), + (Token.Text, u''), + (Token.Text, u' '), + (Token.Text, u''), + (Token.Generic.Prompt, u'[~/project]$'), + (Token.Text, u' '), + (Token.Text, u'foo'), + (Token.Text, u' '), + (Token.Text, u'-h'), + (Token.Text, u'\n'), + ] + assert list(lexer_session.get_tokens(fragment)) == tokens |