Skip to content

Instantly share code, notes, and snippets.

@Inndy
Created August 18, 2014 17:06
Show Gist options
  • Save Inndy/e523ba1e3b75beb61f72 to your computer and use it in GitHub Desktop.
Save Inndy/e523ba1e3b75beb61f72 to your computer and use it in GitHub Desktop.
Colorize your text with ansi escape char
class COLOR(object):
"""
>>> print(COLOR.red('Red Text'))
>>> print(COLOR.RED + 'Red' + COLOR.BLUE + 'BLUE' + COLOR.NONE)
"""
class _INNER_COLOR(type):
colors = {
'NONE' : '\x1b[38;5;0m',
'RED' : '\x1b[38;5;9m',
'GREEN' : '\x1b[38;5;10m',
'YELLOW' : '\x1b[38;5;11m',
'BLUE' : '\x1b[38;5;12m',
'MAGENTA' : '\x1b[38;5;13m',
'CYAN' : '\x1b[38;5;14m',
'WHITE' : '\x1b[38;5;15m',
}
def __getattr__(cls, attr):
if attr.isupper():
return cls.colors[attr]
else:
return lambda x: cls.colors[attr.upper()] + x + cls.colors['NONE']
__metaclass__ = _INNER_COLOR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment