92 lines
2.4 KiB
Python
92 lines
2.4 KiB
Python
import curses
|
|
|
|
inited = False
|
|
#default_color = False
|
|
default_color = True
|
|
|
|
def init():
|
|
global colors, _colors, _pairs, attributes, inited, index
|
|
if not inited:
|
|
index = 1
|
|
|
|
colors = {
|
|
'cyan': curses.COLOR_CYAN,
|
|
'green': curses.COLOR_GREEN,
|
|
'red': curses.COLOR_RED,
|
|
'yellow': curses.COLOR_YELLOW,
|
|
'blue': curses.COLOR_BLUE,
|
|
'magenta': curses.COLOR_MAGENTA,
|
|
'black': curses.COLOR_BLACK,
|
|
'white': curses.COLOR_WHITE,
|
|
}
|
|
|
|
for i in range(0, curses.COLORS):
|
|
if curses.COLORS == 256:
|
|
colors['f%02x' % i] = i
|
|
else:
|
|
colors['f%02x' % i] = curses.COLOR_WHITE
|
|
|
|
if default_color:
|
|
colors["default"] = -1
|
|
|
|
_colors = []
|
|
_pairs = {}
|
|
|
|
for key in _pairs:
|
|
fg, bg = key
|
|
curses.init_pair(index, colors[fg], colors[bg])
|
|
_pairs[key] = curses.color_pair(index)
|
|
_colors.append(key)
|
|
index = len(_colors) + 1
|
|
|
|
attributes = {
|
|
'bold': curses.A_BOLD,
|
|
'reverse': curses.A_REVERSE,
|
|
'normal': curses.A_NORMAL,
|
|
'underline': curses.A_UNDERLINE,
|
|
'dim': curses.A_DIM,
|
|
'standout': curses.A_STANDOUT,
|
|
}
|
|
|
|
inited = True
|
|
|
|
def pairs(fg, bg):
|
|
if not curses.has_colors():
|
|
return curses.color_pair(0)
|
|
|
|
global colors, _colors, _pairs, index
|
|
key = (fg, bg)
|
|
if key not in _pairs:
|
|
assert index < curses.COLOR_PAIRS
|
|
if not default_color:
|
|
if fg == "default":
|
|
fg = "white"
|
|
if bg == "default":
|
|
bg = "black"
|
|
curses.init_pair(index, colors[fg], colors[bg])
|
|
_pairs[key] = curses.color_pair(index)
|
|
_colors.append(key)
|
|
index = len(_colors) + 1
|
|
return _pairs[key]
|
|
|
|
def get_pairs(index):
|
|
if index == 0:
|
|
return ('white', 'black')
|
|
else:
|
|
return _colors[index-1]
|
|
|
|
def reverse_colors(attr):
|
|
return attr ^ curses.A_REVERSE
|
|
|
|
def build(fg, bg, *attr):
|
|
cattr = pairs(fg, bg)
|
|
return build_attr(cattr, *attr)
|
|
|
|
def build_attr(*attr):
|
|
v = curses.A_NORMAL
|
|
for x in attr:
|
|
if type(x) == type(''):
|
|
x = attributes[x]
|
|
v = v | x
|
|
return v
|