The interface of color has changed. color.colored(s, 'red') becomes color.dye(s, color.red) The code behind it is simpler and shorter. The decision to use color or not is made when the UI class is instanciated, and the configuration is read. There is no need to handle it on a per-file basis. The default before repository instanciation is with color, but that might (should) change.main
commit
db14fb94f3
@ -1,60 +1,38 @@
|
|||||||
# display
|
"""
|
||||||
|
Small code to handle colored text
|
||||||
BOLD = '\033[1m'
|
"""
|
||||||
END = '\033[0m'
|
|
||||||
|
bold = '\033[1m'
|
||||||
COLORS = {
|
end = '\033[0m'
|
||||||
'black' : '\033[0;30m',
|
|
||||||
'red' : '\033[0;31m',
|
black = '\033[0;30m'
|
||||||
'green' : '\033[0;32m',
|
red = '\033[0;31m'
|
||||||
'yellow': '\033[0;33m',
|
green = '\033[0;32m'
|
||||||
'blue' : '\033[0;34m',
|
yellow = '\033[0;33m'
|
||||||
'purple': '\033[0;35m',
|
blue = '\033[0;34m'
|
||||||
'cyan' : '\033[0;36m',
|
purple = '\033[0;35m'
|
||||||
'grey' : '\032[0;37m',
|
cyan = '\033[0;36m'
|
||||||
}
|
grey = '\033[0;37m'
|
||||||
|
|
||||||
# Bold
|
ok = green
|
||||||
BCOLORS = {
|
error = red
|
||||||
'black' : '\033[1;30m',
|
normal = grey
|
||||||
'red' : '\033[1;31m',
|
citekey = purple
|
||||||
'green' : '\033[1;32m',
|
filepath = cyan
|
||||||
'yellow': '\033[1;33m',
|
|
||||||
'blue' : '\033[1;34m',
|
def dye(s, color=end, bold=False):
|
||||||
'purple': '\033[1;35m',
|
assert color[0] == '\033'
|
||||||
'cyan' : '\033[1;36m',
|
if bold:
|
||||||
'grey' : '\033[1;37m',
|
s = '\033[1' + s[3:]
|
||||||
}
|
return color + s + end
|
||||||
|
|
||||||
# application specific
|
_dye = dye
|
||||||
ALIASES = {
|
def _nodye(s, **kwargs):
|
||||||
'ok' : 'green',
|
return s
|
||||||
'error' : 'red',
|
|
||||||
'normal' : 'grey',
|
|
||||||
'citekey' : 'purple',
|
|
||||||
'filepath': 'cyan',
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def colored(s, color=None, bold=False):
|
def color_setup(config):
|
||||||
if color in ALIASES:
|
global dye
|
||||||
color = ALIASES[color]
|
if config.getboolean(configs.MAIN_SECTION, 'color'):
|
||||||
try:
|
dye = _dye
|
||||||
if bold:
|
|
||||||
color_code = BCOLORS[color]
|
|
||||||
else:
|
|
||||||
color_code = COLORS[color]
|
|
||||||
except KeyError:
|
|
||||||
if bold:
|
|
||||||
color_code = BOLD
|
|
||||||
else:
|
|
||||||
color_code = ''
|
|
||||||
if color_code != '':
|
|
||||||
end_code = END
|
|
||||||
else:
|
else:
|
||||||
end_code = ''
|
dye = _nodye
|
||||||
return color_code + s + end_code
|
|
||||||
|
|
||||||
|
|
||||||
def not_colored(s, **kwargs):
|
|
||||||
return s
|
|
@ -0,0 +1,10 @@
|
|||||||
|
import testenv
|
||||||
|
from papers import color
|
||||||
|
|
||||||
|
def perf_color():
|
||||||
|
s = str(range(1000))
|
||||||
|
for _ in range(5000000):
|
||||||
|
color.dye(s, color.red)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
perf_color()
|
@ -0,0 +1,3 @@
|
|||||||
|
# Adjusting paths.
|
||||||
|
import os, sys
|
||||||
|
sys.path.insert(0, os.path.abspath(os.path.join(__file__, '../..')))
|
Loading…
Reference in new issue