-
-
Save gencer/3f8d79ec6dabcf5b5f85784c8bb6b9a9 to your computer and use it in GitHub Desktop.
python coloring for linux, based on this answer http://stackoverflow.com/a/26445590/3191896 and modified to make it more usable in a pythonic way.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
class Color(object): | |
""" | |
reference from https://gist.github.com/Jossef/0ee20314577925b4027f and modified bit. | |
""" | |
def __init__(self, text, **user_styles): | |
styles = { | |
# styles | |
'reset': '\033[0m', | |
'bold': '\033[01m', | |
'disabled': '\033[02m', | |
'underline': '\033[04m', | |
'reverse': '\033[07m', | |
'strike_through': '\033[09m', | |
'invisible': '\033[08m', | |
# text colors | |
'fg_black': '\033[30m', | |
'fg_red': '\033[31m', | |
'fg_green': '\033[32m', | |
'fg_orange': '\033[33m', | |
'fg_blue': '\033[34m', | |
'fg_purple': '\033[35m', | |
'fg_cyan': '\033[36m', | |
'fg_light_grey': '\033[37m', | |
'fg_dark_grey': '\033[90m', | |
'fg_light_red': '\033[91m', | |
'fg_light_green': '\033[92m', | |
'fg_yellow': '\033[93m', | |
'fg_light_blue': '\033[94m', | |
'fg_pink': '\033[95m', | |
'fg_light_cyan': '\033[96m', | |
'fg_white': '\033[97m', | |
'fg_default': '\033[99m', | |
# background colors | |
'bg_black': '\033[40m', | |
'bg_red': '\033[41m', | |
'bg_green': '\033[42m', | |
'bg_orange': '\033[43m', | |
'bg_blue': '\033[44m', | |
'bg_purple': '\033[45m', | |
'bg_cyan': '\033[46m', | |
'bg_light_grey': '\033[47m' | |
} | |
self.color_text = '' | |
for style in user_styles: | |
try: | |
self.color_text += styles[style] | |
except KeyError: | |
raise KeyError('def color: parameter `{}` does not exist'.format(style)) | |
self.color_text += text | |
def __format__(self): | |
return '\033[0m{}\033[0m'.format(self.color_text) | |
@classmethod | |
def red(clazz, text): | |
cls = clazz(text, bold=True, fg_red=True) | |
return cls.__format__() | |
@classmethod | |
def orange(clazz, text): | |
cls = clazz(text, bold=True, fg_orange=True) | |
return cls.__format__() | |
@classmethod | |
def green(clazz, text): | |
cls = clazz(text, bold=True, fg_green=True) | |
return cls.__format__() | |
@classmethod | |
def custom(clazz, text, **custom_styles): | |
cls = clazz(text, **custom_styles) | |
return cls.__format__() |
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from colors import Color | |
def main(): | |
print Color.red('sorry') | |
print Color.custom('its blue', bold=True, fg_blue=True) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment