Skip to content

Instantly share code, notes, and snippets.

@kgantsov
Created May 9, 2018 10:03
Show Gist options
  • Select an option

  • Save kgantsov/2056d76bfeaa152f8d43b66c1354f1ec to your computer and use it in GitHub Desktop.

Select an option

Save kgantsov/2056d76bfeaa152f8d43b66c1354f1ec to your computer and use it in GitHub Desktop.
Bunch of functions for colorizing text that needs to be printed to a terminal
from functools import partial
# https://misc.flogisoft.com/bash/tip_colors_and_formatting
def colorize(color, text):
"""
Usage:
>>> print(colorize("BLUE", "This text will be printed in blue color"))
This text will be printed in blue color
"""
colors = {
'RED': '0;31',
'LIGHT_RED': '1;31',
'GREEN': '0;32',
'LIGHT_GREEN': '1;32',
'YELLOW': '0;33',
'LIGHT_YELLOW': '1;33',
'BLUE': '0;34',
'LIGHT_BLUE': '1;34',
'MAGENTA': '0;35',
'LIGHT_MAGENTA': '1;35',
'CYAN': '0;36',
'LIGHT_CYAN': '1;36',
'WHITE': '0;37',
'LIGHT_WHITE': '1;37',
}
if color in colors:
return '\033[{color}m{text}\033[0m'.format(color=colors[color], text=text)
else:
return text
red = partial(colorize, 'RED')
light_red = partial(colorize, 'LIGHT_RED')
green = partial(colorize, 'GREEN')
light_green = partial(colorize, 'LIGHT_GREEN')
yellow = partial(colorize, 'YELLOW')
light_yellow = partial(colorize, 'LIGHT_YELLOW')
blue = partial(colorize, 'BLUE')
light_blue = partial(colorize, 'LIGHT_BLUE')
magenta = partial(colorize, 'MAGENTA')
light_magenta = partial(colorize, 'LIGHT_MAGENTA')
cyan = partial(colorize, 'CYAN')
light_cyan = partial(colorize, 'LIGHT_CYAN')
white = partial(colorize, 'WHITE')
light_white = partial(colorize, 'LIGHT_WHITE')
if __name__ == '__main__':
funcs = [
red,
light_red,
green,
light_green,
yellow,
light_yellow,
blue,
light_blue,
magenta,
light_magenta,
cyan,
light_cyan,
white,
light_white,
]
for func in funcs:
print(func(func))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment