Skip to content

Instantly share code, notes, and snippets.

@kazeto
Created February 28, 2018 05:35
Show Gist options
  • Save kazeto/31b2fd3f47ecc24519cec6be273bc1cc to your computer and use it in GitHub Desktop.
Save kazeto/31b2fd3f47ecc24519cec6be273bc1cc to your computer and use it in GitHub Desktop.
Python code to color a string with ANSI escape sequence.
from functools import partial
def ansi_color(code, text, is_bold=False):
if is_bold:
code = ('1;' + code)
return '\033[%sm%s\033[0m' % (code, text)
ansi_red = partial(ansi_color, '31')
ansi_green = partial(ansi_color, '32')
ansi_yellow = partial(ansi_color, '33')
ansi_blue = partial(ansi_color, '34')
ansi_pink = partial(ansi_color, '35')
ansi_cyan = partial(ansi_color, '36')
ansi_silver = partial(ansi_color, '37')
ansi_gray = partial(ansi_color, '90')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment