Created
August 18, 2014 17:06
-
-
Save Inndy/e523ba1e3b75beb61f72 to your computer and use it in GitHub Desktop.
Colorize your text with ansi escape char
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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