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 e336456
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
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
10 changes: 10 additions & 0 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 = r'^\([^)]*\)'

def get_tokens_unprocessed(self, text):
innerlexer = self._innerLexerCls(**self.options)

Expand All @@ -165,6 +168,7 @@ def get_tokens_unprocessed(self, text):
for match in line_re.finditer(text):
line = match.group()
m = re.match(self._ps1rgx, line)
venv_re = re.match(self._venv, line)
if backslash_continuation:
curcode += line
backslash_continuation = curcode.endswith('\\\n')
Expand All @@ -184,6 +188,12 @@ def get_tokens_unprocessed(self, text):
[(0, Generic.Prompt, line[:len(self._ps2)])]))
curcode += line[len(self._ps2):]
backslash_continuation = curcode.endswith('\\\n')
elif venv_re:
venv_match = venv_re.group(0)
insertions.append((len(curcode),
[(0, Generic.Prompt, line[:len(venv_match)])]))
curcode += line[len(venv_match):]
backslash_continuation = curcode.endswith('\\\n')
else:
if insertions:
toks = innerlexer.get_tokens_unprocessed(curcode)
Expand Down
20 changes: 19 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,21 @@ 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, u'(env)'),
(Token.Text, u' '),
(Token.Operator, u'['),
(Token.Text, u'~/project'),
(Token.Operator, u']'),
(Token.Text, u'$'),
(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 e336456

Please sign in to comment.