|
|
@ -1,12 +1,8 @@
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
pygments.console
|
|
|
|
Formatter for ANSI colored console output.
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Format colored console output.
|
|
|
|
Based heavily on Pygments terminal colorizing code, originally by Georg Brandl.
|
|
|
|
|
|
|
|
|
|
|
|
:copyright: Copyright 2006-2011 by the Pygments team, see AUTHORS.
|
|
|
|
|
|
|
|
:license: BSD, see LICENSE for details.
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
@ -34,18 +30,21 @@ def gettermsize():
|
|
|
|
cr = (25, 80)
|
|
|
|
cr = (25, 80)
|
|
|
|
return int(cr[1]), int(cr[0])
|
|
|
|
return int(cr[1]), int(cr[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _Colorizer(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
esc = "\x1b["
|
|
|
|
esc = "\x1b["
|
|
|
|
|
|
|
|
|
|
|
|
codes = {}
|
|
|
|
self.codes = {}
|
|
|
|
codes[""] = ""
|
|
|
|
self.codes[""] = ""
|
|
|
|
codes["reset"] = esc + "39;49;00m"
|
|
|
|
self.codes["reset"] = esc + "39;49;00m"
|
|
|
|
|
|
|
|
|
|
|
|
codes["bold"] = esc + "01m"
|
|
|
|
self.codes["bold"] = esc + "01m"
|
|
|
|
codes["faint"] = esc + "02m"
|
|
|
|
self.codes["faint"] = esc + "02m"
|
|
|
|
codes["standout"] = esc + "03m"
|
|
|
|
self.codes["standout"] = esc + "03m"
|
|
|
|
codes["underline"] = esc + "04m"
|
|
|
|
self.codes["underline"] = esc + "04m"
|
|
|
|
codes["blink"] = esc + "05m"
|
|
|
|
self.codes["blink"] = esc + "05m"
|
|
|
|
codes["overline"] = esc + "06m"
|
|
|
|
self.codes["overline"] = esc + "06m"
|
|
|
|
|
|
|
|
|
|
|
|
dark_colors = ["black", "darkred", "darkgreen", "brown", "darkblue",
|
|
|
|
dark_colors = ["black", "darkred", "darkgreen", "brown", "darkblue",
|
|
|
|
"purple", "teal", "lightgray"]
|
|
|
|
"purple", "teal", "lightgray"]
|
|
|
@ -54,27 +53,24 @@ light_colors = ["darkgray", "red", "green", "yellow", "blue",
|
|
|
|
|
|
|
|
|
|
|
|
x = 30
|
|
|
|
x = 30
|
|
|
|
for d, l in zip(dark_colors, light_colors):
|
|
|
|
for d, l in zip(dark_colors, light_colors):
|
|
|
|
codes[d] = esc + "%im" % x
|
|
|
|
self.codes[d] = esc + "%im" % x
|
|
|
|
codes[l] = esc + "%i;01m" % x
|
|
|
|
self.codes[l] = esc + "%i;01m" % x
|
|
|
|
x += 1
|
|
|
|
x += 1
|
|
|
|
|
|
|
|
|
|
|
|
del d, l, x
|
|
|
|
del d, l, x
|
|
|
|
|
|
|
|
|
|
|
|
codes["darkteal"] = codes["turquoise"]
|
|
|
|
self.codes["darkteal"] = self.codes["turquoise"]
|
|
|
|
codes["darkyellow"] = codes["brown"]
|
|
|
|
self.codes["darkyellow"] = self.codes["brown"]
|
|
|
|
codes["fuscia"] = codes["fuchsia"]
|
|
|
|
self.codes["fuscia"] = self.codes["fuchsia"]
|
|
|
|
codes["white"] = codes["bold"]
|
|
|
|
self.codes["white"] = self.codes["bold"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reset_color():
|
|
|
|
|
|
|
|
return codes["reset"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def reset_color(self):
|
|
|
|
|
|
|
|
return self.codes["reset"]
|
|
|
|
|
|
|
|
|
|
|
|
def colorize(color_key, text):
|
|
|
|
def colorize(self, color_key, text):
|
|
|
|
return codes[color_key] + text + codes["reset"]
|
|
|
|
return self.codes[color_key] + text + self.codes["reset"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ansiformat(self, attr, text):
|
|
|
|
def ansiformat(attr, text):
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Format ``text`` with a color and/or some attributes::
|
|
|
|
Format ``text`` with a color and/or some attributes::
|
|
|
|
|
|
|
|
|
|
|
@ -85,15 +81,34 @@ def ansiformat(attr, text):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
result = []
|
|
|
|
result = []
|
|
|
|
if attr[:1] == attr[-1:] == '+':
|
|
|
|
if attr[:1] == attr[-1:] == '+':
|
|
|
|
result.append(codes['blink'])
|
|
|
|
result.append(self.codes['blink'])
|
|
|
|
attr = attr[1:-1]
|
|
|
|
attr = attr[1:-1]
|
|
|
|
if attr[:1] == attr[-1:] == '*':
|
|
|
|
if attr[:1] == attr[-1:] == '*':
|
|
|
|
result.append(codes['bold'])
|
|
|
|
result.append(self.codes['bold'])
|
|
|
|
attr = attr[1:-1]
|
|
|
|
attr = attr[1:-1]
|
|
|
|
if attr[:1] == attr[-1:] == '_':
|
|
|
|
if attr[:1] == attr[-1:] == '_':
|
|
|
|
result.append(codes['underline'])
|
|
|
|
result.append(self.codes['underline'])
|
|
|
|
attr = attr[1:-1]
|
|
|
|
attr = attr[1:-1]
|
|
|
|
result.append(codes[attr])
|
|
|
|
result.append(self.codes[attr])
|
|
|
|
result.append(text)
|
|
|
|
result.append(text)
|
|
|
|
result.append(codes['reset'])
|
|
|
|
result.append(self.codes['reset'])
|
|
|
|
return ''.join(result)
|
|
|
|
return ''.join(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
colorizer = _Colorizer()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_colorizer(color):
|
|
|
|
|
|
|
|
"""Creates a function that colorizes text with the given color.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
green = make_colorizer('darkgreen')
|
|
|
|
|
|
|
|
red = make_colorizer('red')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Then, you can use:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print "It's either " + green('OK') + ' or ' + red('Oops')
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
def inner(text):
|
|
|
|
|
|
|
|
return colorizer.colorize(color, text)
|
|
|
|
|
|
|
|
return inner
|
|
|
|