Вообще, можно посмотреть готовое решение на эту тему:
coloramaНу, а представленный код, можно было начатьоптимизировать приблизительно так:
# Common sequence, lets follow the DRY principle
ANSI = '\033['
# Colormapping...
CMAP = {'default':'99m', # TODO: bad name for color... renaming needed
'red': '91m',
'green': '92m',
'blue': '94m',
'cyan': '96m',
'white': '97m',
'yellow': '93m'
# etc.
}
# underline seq.
UFLAG = '4m' # Underline flag
NUFLAG = '5m' # TODO: checking needed ... wrong code
# ------------------------------------------------
def colorize(text, color='default', underline=False):
'''Changes text color in console'''
result_string = ''
if color not in CMAP:
color = CMAP['default']
if underline:
result_string += ANSI + UFLAG
if underline:
_style = ANSI + UFLAG
else:
_style = ANSI + NUFLAG
result_string += _style + ANSI + CMAP[color] + text
print result_string