File: ansiclr.py

package info (click to toggle)
crmsh 4.0.0~git20190108.3d56538-3%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,636 kB
  • sloc: python: 29,464; sh: 827; exp: 249; xml: 226; makefile: 204; awk: 23
file content (50 lines) | stat: -rw-r--r-- 1,388 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
"""
    pygments.lexers.console
    ~~~~~~~~~~~~~~~~~~~~~~~

    Lexers for misc console output.

    :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""
from __future__ import unicode_literals

from pygments.lexer import RegexLexer, bygroups
from pygments.token import Generic, Text

__all__ = ['ANSIColorsLexer']

_ESC = "\x1b\["
# this is normally to reset (reset attributes, set primary font)
# there could be however other reset sequences and in that case
# sgr0 needs to be updated
_SGR0 = "%s(?:0;10|10;0)m" % _ESC
# BLACK RED GREEN YELLOW
# BLUE MAGENTA CYAN WHITE
_ANSI_COLORS = (Generic.Emph, Generic.Error, Generic.Inserted, Generic.Keyword,
                Generic.Keyword, Generic.Prompt, Generic.Traceback, Generic.Output)


def _ansi2rgb(lexer, match):
    code = match.group(1)
    text = match.group(2)
    yield match.start(), _ANSI_COLORS[int(code)-30], text


class ANSIColorsLexer(RegexLexer):
    """
    Interpret ANSI colors.
    """
    name = 'ANSI Colors'
    aliases = ['ansiclr']
    filenames = ["*.typescript"]

    tokens = {
        'root': [
            (r'%s(3[0-7]+)m(.*?)%s' % (_ESC, _SGR0), _ansi2rgb),
            (r'[^\x1b]+', Text),
            # drop the rest of the graphic codes
            (r'(%s[0-9;]+m)()' % _ESC, bygroups(None, Text)),
        ]
    }