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 5b5c538
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 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
19 changes: 15 additions & 4 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,19 @@ 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_match = venv_match.group(0)
insertions.append((len(curcode),
[(0, Generic.Prompt.VirtualEnv, line[:len(venv_match)])]))
line = line[len(venv_match):]

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 +222,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
17 changes: 16 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,18 @@ 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.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 5b5c538

Please sign in to comment.