pygments/console.py

2 years ago

author
gbrandl
date
Mon Dec 28 08:53:11 2009 +0100
branch
trunk
changeset 916
b2deea5b5030
parent 225
7f233c9f9b94
child 917
8c21b7cb4d50
permissions
-rw-r--r--

Added the Monokai style (#453).

     1 # -*- coding: utf-8 -*-
     2 """
     3     pygments.console
     4     ~~~~~~~~~~~~~~~~
     6     Format colored console output.
     8     :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
     9     :license: BSD, see LICENSE for details.
    10 """
    12 esc = "\x1b["
    14 codes = {}
    15 codes[""]          = ""
    16 codes["reset"]     = esc + "39;49;00m"
    18 codes["bold"]      = esc + "01m"
    19 codes["faint"]     = esc + "02m"
    20 codes["standout"]  = esc + "03m"
    21 codes["underline"] = esc + "04m"
    22 codes["blink"]     = esc + "05m"
    23 codes["overline"]  = esc + "06m"
    25 dark_colors  = ["black", "darkred", "darkgreen", "brown", "darkblue",
    26                 "purple", "teal", "lightgray"]
    27 light_colors = ["darkgray", "red", "green", "yellow", "blue",
    28                 "fuchsia", "turquoise", "white"]
    30 x = 30
    31 for d, l in zip(dark_colors, light_colors):
    32     codes[d] = esc + "%im" % x
    33     codes[l] = esc + "%i;01m" % x
    34     x += 1
    36 del d, l, x
    38 codes["darkteal"]   = codes["turquoise"]
    39 codes["darkyellow"] = codes["brown"]
    40 codes["fuscia"]     = codes["fuchsia"]
    41 codes["white"]      = codes["bold"]
    44 def reset_color():
    45     return codes["reset"]
    48 def colorize(color_key, text):
    49     return codes[color_key] + text + codes["reset"]
    52 def ansiformat(attr, text):
    53     """
    54     Format ``text`` with a color and/or some attributes::
    56         color       normal color
    57         *color*     bold color
    58         _color_     underlined color
    59         +color+     blinking color
    60     """
    61     result = []
    62     if attr[:1] == attr[-1:] == '+':
    63         result.append(codes['blink'])
    64         attr = attr[1:-1]
    65     if attr[:1] == attr[-1:] == '*':
    66         result.append(codes['bold'])
    67         attr = attr[1:-1]
    68     if attr[:1] == attr[-1:] == '_':
    69         result.append(codes['underline'])
    70         attr = attr[1:-1]
    71     result.append(codes[attr])
    72     result.append(text)
    73     result.append(codes['reset'])
    74     return ''.join(result)

mercurial