Rewrite the ANSI color formatter a bit.

main
Vincent Driessen 13 years ago
parent edcc012b2d
commit 4ad6281641

@ -4,7 +4,9 @@ import os
import time import time
import optparse import optparse
from rq import use_redis, Queue from rq import use_redis, Queue
from rq.utils import gettermsize, colorize from rq.utils import gettermsize, make_colorizer
green = make_colorizer('darkgreen')
def get_scale(x): def get_scale(x):
@ -60,7 +62,7 @@ def show_queues(opts, args, parser):
for q in qs: for q in qs:
count = counts[q] count = counts[q]
if opts.graph: if opts.graph:
chart = colorize('darkgreen', '|' + '█' * int(ratio * count)) chart = green('|' + '█' * int(ratio * count))
line = '%-12s %s %d' % (q.name, chart, count) line = '%-12s %s %d' % (q.name, chart, count)
else: else:
line = '%-12s %d' % (q.name, count) line = '%-12s %d' % (q.name, count)

@ -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,66 +30,85 @@ def gettermsize():
cr = (25, 80) cr = (25, 80)
return int(cr[1]), int(cr[0]) return int(cr[1]), int(cr[0])
esc = "\x1b["
codes = {} class _Colorizer(object):
codes[""] = "" def __init__(self):
codes["reset"] = esc + "39;49;00m" esc = "\x1b["
codes["bold"] = esc + "01m" self.codes = {}
codes["faint"] = esc + "02m" self.codes[""] = ""
codes["standout"] = esc + "03m" self.codes["reset"] = esc + "39;49;00m"
codes["underline"] = esc + "04m"
codes["blink"] = esc + "05m"
codes["overline"] = esc + "06m"
dark_colors = ["black", "darkred", "darkgreen", "brown", "darkblue", self.codes["bold"] = esc + "01m"
"purple", "teal", "lightgray"] self.codes["faint"] = esc + "02m"
light_colors = ["darkgray", "red", "green", "yellow", "blue", self.codes["standout"] = esc + "03m"
"fuchsia", "turquoise", "white"] self.codes["underline"] = esc + "04m"
self.codes["blink"] = esc + "05m"
self.codes["overline"] = esc + "06m"
x = 30 dark_colors = ["black", "darkred", "darkgreen", "brown", "darkblue",
for d, l in zip(dark_colors, light_colors): "purple", "teal", "lightgray"]
codes[d] = esc + "%im" % x light_colors = ["darkgray", "red", "green", "yellow", "blue",
codes[l] = esc + "%i;01m" % x "fuchsia", "turquoise", "white"]
x += 1
del d, l, x x = 30
for d, l in zip(dark_colors, light_colors):
self.codes[d] = esc + "%im" % x
self.codes[l] = esc + "%i;01m" % x
x += 1
codes["darkteal"] = codes["turquoise"] del d, l, x
codes["darkyellow"] = codes["brown"]
codes["fuscia"] = codes["fuchsia"]
codes["white"] = codes["bold"]
self.codes["darkteal"] = self.codes["turquoise"]
self.codes["darkyellow"] = self.codes["brown"]
self.codes["fuscia"] = self.codes["fuchsia"]
self.codes["white"] = self.codes["bold"]
def reset_color(): def reset_color(self):
return codes["reset"] return self.codes["reset"]
def colorize(self, color_key, text):
return self.codes[color_key] + text + self.codes["reset"]
def colorize(color_key, text): def ansiformat(self, attr, text):
return codes[color_key] + text + codes["reset"] """
Format ``text`` with a color and/or some attributes::
color normal color
*color* bold color
_color_ underlined color
+color+ blinking color
"""
result = []
if attr[:1] == attr[-1:] == '+':
result.append(self.codes['blink'])
attr = attr[1:-1]
if attr[:1] == attr[-1:] == '*':
result.append(self.codes['bold'])
attr = attr[1:-1]
if attr[:1] == attr[-1:] == '_':
result.append(self.codes['underline'])
attr = attr[1:-1]
result.append(self.codes[attr])
result.append(text)
result.append(self.codes['reset'])
return ''.join(result)
def ansiformat(attr, text):
"""
Format ``text`` with a color and/or some attributes::
color normal color colorizer = _Colorizer()
*color* bold color
_color_ underlined color def make_colorizer(color):
+color+ blinking 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')
""" """
result = [] def inner(text):
if attr[:1] == attr[-1:] == '+': return colorizer.colorize(color, text)
result.append(codes['blink']) return inner
attr = attr[1:-1]
if attr[:1] == attr[-1:] == '*':
result.append(codes['bold'])
attr = attr[1:-1]
if attr[:1] == attr[-1:] == '_':
result.append(codes['underline'])
attr = attr[1:-1]
result.append(codes[attr])
result.append(text)
result.append(codes['reset'])
return ''.join(result)

Loading…
Cancel
Save