Skip to content

Commit

Permalink
Fix #1253.
Browse files Browse the repository at this point in the history
  • Loading branch information
Anteru committed Nov 24, 2019
1 parent 3c43bd4 commit 4397b6a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 22 additions & 7 deletions pygments/lexers/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -164,11 +167,23 @@ def get_tokens_unprocessed(self, text):

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:

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, Generic.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.
Expand Down Expand Up @@ -211,9 +226,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 = '>'


Expand Down Expand Up @@ -540,7 +555,7 @@ class MSDOSSessionLexer(ShellSessionBaseLexer):
mimetypes = []

_innerLexerCls = BatchLexer
_ps1rgx = r'^([^>]*>)(.*\n?)'
_ps1rgx = re.compile(r'^([^>]*>)(.*\n?)')
_ps2 = 'More? '


Expand Down Expand Up @@ -625,7 +640,7 @@ class TcshSessionLexer(ShellSessionBaseLexer):
mimetypes = []

_innerLexerCls = TcshLexer
_ps1rgx = r'^([^>]+>)(.*\n?)'
_ps1rgx = re.compile(r'^([^>]+>)(.*\n?)')
_ps2 = '? '


Expand Down Expand Up @@ -756,7 +771,7 @@ class PowerShellSessionLexer(ShellSessionBaseLexer):
mimetypes = []

_innerLexerCls = PowerShellLexer
_ps1rgx = r'^(PS [^>]+> )(.*\n?)'
_ps1rgx = re.compile(r'^(PS [^>]+> )(.*\n?)')
_ps2 = '>> '


Expand Down
19 changes: 18 additions & 1 deletion tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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''),
Expand Down Expand Up @@ -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.Generic.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

0 comments on commit 4397b6a

Please sign in to comment.