Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
irc.py154 linesDownload Raw Back to formatters
1"""2    pygments.formatters.irc3    ~~~~~~~~~~~~~~~~~~~~~~~4 5    Formatter for IRC output6 7    :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.8    :license: BSD, see LICENSE for details.9"""10 11from pygments.formatter import Formatter12from pygments.token import Keyword, Name, Comment, String, Error, \13    Number, Operator, Generic, Token, Whitespace14from pygments.util import get_choice_opt15 16 17__all__ = ['IRCFormatter']18 19 20#: Map token types to a tuple of color values for light and dark21#: backgrounds.22IRC_COLORS = {23    Token:              ('',            ''),24 25    Whitespace:         ('gray',   'brightblack'),26    Comment:            ('gray',   'brightblack'),27    Comment.Preproc:    ('cyan',        'brightcyan'),28    Keyword:            ('blue',    'brightblue'),29    Keyword.Type:       ('cyan',        'brightcyan'),30    Operator.Word:      ('magenta',      'brightcyan'),31    Name.Builtin:       ('cyan',        'brightcyan'),32    Name.Function:      ('green',   'brightgreen'),33    Name.Namespace:     ('_cyan_',      '_brightcyan_'),34    Name.Class:         ('_green_', '_brightgreen_'),35    Name.Exception:     ('cyan',        'brightcyan'),36    Name.Decorator:     ('brightblack',    'gray'),37    Name.Variable:      ('red',     'brightred'),38    Name.Constant:      ('red',     'brightred'),39    Name.Attribute:     ('cyan',        'brightcyan'),40    Name.Tag:           ('brightblue',        'brightblue'),41    String:             ('yellow',       'yellow'),42    Number:             ('blue',    'brightblue'),43 44    Generic.Deleted:    ('brightred',        'brightred'),45    Generic.Inserted:   ('green',  'brightgreen'),46    Generic.Heading:    ('**',         '**'),47    Generic.Subheading: ('*magenta*',   '*brightmagenta*'),48    Generic.Error:      ('brightred',        'brightred'),49 50    Error:              ('_brightred_',      '_brightred_'),51}52 53 54IRC_COLOR_MAP = {55    'white': 0,56    'black': 1,57    'blue': 2,58    'brightgreen': 3,59    'brightred': 4,60    'yellow': 5,61    'magenta': 6,62    'orange': 7,63    'green': 7, #compat w/ ansi64    'brightyellow': 8,65    'lightgreen': 9,66    'brightcyan': 9, # compat w/ ansi67    'cyan': 10,68    'lightblue': 11,69    'red': 11, # compat w/ ansi70    'brightblue': 12,71    'brightmagenta': 13,72    'brightblack': 14,73    'gray': 15,74}75 76def ircformat(color, text):77    if len(color) < 1:78        return text79    add = sub = ''80    if '_' in color: # italic81        add += '\x1D'82        sub = '\x1D' + sub83        color = color.strip('_')84    if '*' in color: # bold85        add += '\x02'86        sub = '\x02' + sub87        color = color.strip('*')88    # underline (\x1F) not supported89    # backgrounds (\x03FF,BB) not supported90    if len(color) > 0: # actual color - may have issues with ircformat("red", "blah")+"10" type stuff91        add += '\x03' + str(IRC_COLOR_MAP[color]).zfill(2)92        sub = '\x03' + sub93    return add + text + sub94 95 96class IRCFormatter(Formatter):97    r"""98    Format tokens with IRC color sequences99 100    The `get_style_defs()` method doesn't do anything special since there is101    no support for common styles.102 103    Options accepted:104 105    `bg`106        Set to ``"light"`` or ``"dark"`` depending on the terminal's background107        (default: ``"light"``).108 109    `colorscheme`110        A dictionary mapping token types to (lightbg, darkbg) color names or111        ``None`` (default: ``None`` = use builtin colorscheme).112 113    `linenos`114        Set to ``True`` to have line numbers in the output as well115        (default: ``False`` = no line numbers).116    """117    name = 'IRC'118    aliases = ['irc', 'IRC']119    filenames = []120 121    def __init__(self, **options):122        Formatter.__init__(self, **options)123        self.darkbg = get_choice_opt(options, 'bg',124                                     ['light', 'dark'], 'light') == 'dark'125        self.colorscheme = options.get('colorscheme', None) or IRC_COLORS126        self.linenos = options.get('linenos', False)127        self._lineno = 0128 129    def _write_lineno(self, outfile):130        if self.linenos:131            self._lineno += 1132            outfile.write("%04d: " % self._lineno)133 134    def format_unencoded(self, tokensource, outfile):135        self._write_lineno(outfile)136 137        for ttype, value in tokensource:138            color = self.colorscheme.get(ttype)139            while color is None:140                ttype = ttype[:-1]141                color = self.colorscheme.get(ttype)142            if color:143                color = color[self.darkbg]144                spl = value.split('\n')145                for line in spl[:-1]:146                    if line:147                        outfile.write(ircformat(color, line))148                    outfile.write('\n')149                    self._write_lineno(outfile)150                if spl[-1]:151                    outfile.write(ircformat(color, spl[-1]))152            else:153                outfile.write(value)154 
codekingpro/portable-devtools · Team Ai