Last active
December 17, 2015 15:58
-
-
Save hit9/5634949 to your computer and use it in GitHub Desktop.
utility to return ansi colored text.
This file contains 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): | |
""" | |
utility to return ansi colored text. | |
usage:: | |
>>> from color import colored | |
>>> colored("text","red") | |
'\x1b[31mtext\x1b[0m' | |
""" | |
colors = { | |
'black': 30, | |
'red': 31, | |
'green': 32, | |
'yellow': 33, | |
'blue': 34, | |
'magenta': 35, | |
'cyan': 36, | |
'white': 37, | |
'bgred': 41, | |
'bggrey': 100 | |
} | |
prefix = '\033[' | |
suffix = '\033[0m' | |
def colored(self, text, color=None): | |
""" | |
usage: | |
""" | |
if color not in self.colors: | |
color = 'white' | |
clr = self.colors[color] | |
return (self.prefix+'%dm%s'+self.suffix) % (clr, text) | |
colored = Color().colored | |
if __name__ == '__main__': | |
for clr in Color.colors: | |
print colored(clr, clr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment