Skip to content

Instantly share code, notes, and snippets.

@hit9
Last active December 17, 2015 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hit9/5634949 to your computer and use it in GitHub Desktop.
Save hit9/5634949 to your computer and use it in GitHub Desktop.
utility to return ansi colored text.
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