parent
d5a4fcf73c
commit
cce9406670
@ -1,47 +1,90 @@
|
|||||||
"""
|
"""
|
||||||
Small code to handle colored text
|
Small code to handle colored text
|
||||||
"""
|
"""
|
||||||
|
import sys
|
||||||
import re
|
import re
|
||||||
|
|
||||||
bold = '\033[1m'
|
def _color_supported(stream):
|
||||||
end = '\033[0m'
|
"""Returns True is the stream supports colors"""
|
||||||
|
if sys.platform == 'win32' and 'ANSICON' not in os.environ:
|
||||||
black = '\033[0;30m'
|
return False
|
||||||
red = '\033[0;31m'
|
if hasattr(stream, 'isatty') and stream.isatty(): # we have a tty
|
||||||
green = '\033[0;32m'
|
try:
|
||||||
yellow = '\033[0;33m'
|
import curses
|
||||||
blue = '\033[0;34m'
|
curses.setupterm()
|
||||||
purple = '\033[0;35m'
|
return curses.tigetnum('colors') >= 8
|
||||||
cyan = '\033[0;36m'
|
except Exception: # not picky.
|
||||||
grey = '\033[0;37m'
|
return False
|
||||||
|
return False
|
||||||
ok = green
|
|
||||||
error = red
|
COLOR_LIST = [u'black', u'red', u'green', u'yellow', u'blue', u'purple', u'cyan', u'grey']
|
||||||
normal = grey
|
|
||||||
citekey = purple
|
def generate_colors(stream, color=True, bold=True, italic=True):
|
||||||
filepath = bold
|
colors = {name: u'' for name in COLOR_LIST}
|
||||||
tag = cyan
|
colors.update({u'b' +name: u'' for name in COLOR_LIST})
|
||||||
|
colors.update({u'i' +name: u'' for name in COLOR_LIST})
|
||||||
def dye(s, color=end, bold=False):
|
colors.update({u'bi'+name: u'' for name in COLOR_LIST})
|
||||||
assert color[0] == '\033'
|
colors[u'bold'] = u''
|
||||||
|
colors[u'italic'] = u''
|
||||||
|
colors[u'end'] = u''
|
||||||
|
|
||||||
|
if (color or bold or italic) and _color_supported(stream):
|
||||||
|
bold_flag, italic_flag = '', ''
|
||||||
|
if bold:
|
||||||
|
colors['bold'] = u'\x1b[1m'
|
||||||
|
bold_flag = '1;'
|
||||||
|
if italic:
|
||||||
|
colors['italic'] = u'\x1b[3m'
|
||||||
|
italic_flag = '3;'
|
||||||
|
|
||||||
|
for i, name in enumerate(COLOR_LIST):
|
||||||
|
if color:
|
||||||
|
color_flag = '3{}'.format(name)
|
||||||
|
colors[name] = u'\x1b[{}m'.format(color_flag)
|
||||||
|
colors.update({u'b'+name: u'\x1b[{}3{}m'.format(bold_flag, i) for i, name in enumerate(COLOR_LIST)})
|
||||||
|
colors.update({u'i'+name: u'\x1b[{}3{}m'.format(italic_flag, i) for i, name in enumerate(COLOR_LIST)})
|
||||||
|
colors.update({u'bi'+name: u'\x1b[{}3{}m'.format(bold_flag, italic_flag, i) for i, name in enumerate(COLOR_LIST)})
|
||||||
|
else:
|
||||||
if bold:
|
if bold:
|
||||||
color = '\033[1' + color[3:]
|
colors.update({u'b'+name: u'\x1b[{}m'.format(bold_flag, i) for i, name in enumerate(COLOR_LIST)})
|
||||||
return color + s + end
|
if italic:
|
||||||
|
colors.update({u'i'+name: u'\x1b[{}m'.format(italic_flag, i) for i, name in enumerate(COLOR_LIST)})
|
||||||
|
if bold or italic:
|
||||||
|
colors.update({u'bi'+name: u'\x1b[{}m'.format(bold_flag, italic_flag, i) for i, name in enumerate(COLOR_LIST)})
|
||||||
|
|
||||||
|
if color or bold or italic:
|
||||||
|
colors[u'end'] = u'\x1b[0m'
|
||||||
|
|
||||||
|
return colors
|
||||||
|
|
||||||
|
|
||||||
|
COLORS_OUT = generate_colors(sys.stdout, color=True, bold=True, italic=True)
|
||||||
|
COLORS_ERR = generate_colors(sys.stderr, color=True, bold=True, italic=True)
|
||||||
|
|
||||||
|
def dye_out(s, color='end'):
|
||||||
|
return '{}{}{}'.format(COLORS_OUT[color], s, COLORS_OUT['end'])
|
||||||
|
|
||||||
|
def dye_err(s, color='end'):
|
||||||
|
return '{}{}{}'.format(COLORS_ERR[color], s, COLORS_OUT['end'])
|
||||||
|
|
||||||
_dye = dye
|
|
||||||
def _nodye(s, *args, **kwargs):
|
def _nodye(s, *args, **kwargs):
|
||||||
return s
|
return s
|
||||||
|
|
||||||
def setup(enable = True):
|
def setup(color=True, bold=True, italic=True):
|
||||||
global dye
|
global COLORS_OUT, COLORS_ERR
|
||||||
if enable:
|
COLORS_OUT = generate_colors(sys.stdout, color=color, bold=color, italic=color)
|
||||||
dye = _dye
|
COLORS_ERR = generate_colors(sys.stderr, color=color, bold=color, italic=color)
|
||||||
else:
|
|
||||||
dye = _nodye
|
|
||||||
|
|
||||||
|
|
||||||
|
# undye
|
||||||
undye_re = re.compile('\x1b\[[;\d]*[A-Za-z]')
|
undye_re = re.compile('\x1b\[[;\d]*[A-Za-z]')
|
||||||
|
|
||||||
def undye(s):
|
def undye(s):
|
||||||
"""Purge string s of color"""
|
"""Purge string s of color"""
|
||||||
return undye_re.sub('', s)
|
return undye_re.sub('', s)
|
||||||
|
|
||||||
|
# colors
|
||||||
|
ok = 'green'
|
||||||
|
error = 'red'
|
||||||
|
citekey = 'purple'
|
||||||
|
filepath = 'bold'
|
||||||
|
tag = 'cyan'
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
# Adjusting paths.
|
# Adjusting paths.
|
||||||
import os, sys
|
import os, sys
|
||||||
sys.path.insert(0, os.path.abspath(os.path.join(__file__, '../..')))
|
sys.path.insert(0, os.path.abspath(os.path.join(__file__, '../..')))
|
||||||
|
|
||||||
|
import logging
|
||||||
|
logging.disable(logging.CRITICAL)
|
||||||
|
Loading…
Reference in new issue