Back to index

moin  1.9.0~rc2
console.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     pygments.console
00004     ~~~~~~~~~~~~~~~~
00005 
00006     Format colored console output.
00007 
00008     :copyright: Copyright 2006-2009 by the Pygments team, see AUTHORS.
00009     :license: BSD, see LICENSE for details.
00010 """
00011 
00012 esc = "\x1b["
00013 
00014 codes = {}
00015 codes[""]          = ""
00016 codes["reset"]     = esc + "39;49;00m"
00017 
00018 codes["bold"]      = esc + "01m"
00019 codes["faint"]     = esc + "02m"
00020 codes["standout"]  = esc + "03m"
00021 codes["underline"] = esc + "04m"
00022 codes["blink"]     = esc + "05m"
00023 codes["overline"]  = esc + "06m"
00024 
00025 dark_colors  = ["black", "darkred", "darkgreen", "brown", "darkblue",
00026                 "purple", "teal", "lightgray"]
00027 light_colors = ["darkgray", "red", "green", "yellow", "blue",
00028                 "fuchsia", "turquoise", "white"]
00029 
00030 x = 30
00031 for d, l in zip(dark_colors, light_colors):
00032     codes[d] = esc + "%im" % x
00033     codes[l] = esc + "%i;01m" % x
00034     x += 1
00035 
00036 del d, l, x
00037 
00038 codes["darkteal"]   = codes["turquoise"]
00039 codes["darkyellow"] = codes["brown"]
00040 codes["fuscia"]     = codes["fuchsia"]
00041 codes["white"]      = codes["bold"]
00042 
00043 
00044 def reset_color():
00045     return codes["reset"]
00046 
00047 
00048 def colorize(color_key, text):
00049     return codes[color_key] + text + codes["reset"]
00050 
00051 
00052 def ansiformat(attr, text):
00053     """
00054     Format ``text`` with a color and/or some attributes::
00055 
00056         color       normal color
00057         *color*     bold color
00058         _color_     underlined color
00059         +color+     blinking color
00060     """
00061     result = []
00062     if attr[:1] == attr[-1:] == '+':
00063         result.append(codes['blink'])
00064         attr = attr[1:-1]
00065     if attr[:1] == attr[-1:] == '*':
00066         result.append(codes['bold'])
00067         attr = attr[1:-1]
00068     if attr[:1] == attr[-1:] == '_':
00069         result.append(codes['underline'])
00070         attr = attr[1:-1]
00071     result.append(codes[attr])
00072     result.append(text)
00073     result.append(codes['reset'])
00074     return ''.join(result)