Skip to content

Instantly share code, notes, and snippets.

@rizo
Created June 20, 2013 20:26
Show Gist options
  • Save rizo/5826289 to your computer and use it in GitHub Desktop.
Save rizo/5826289 to your computer and use it in GitHub Desktop.
Small python module for terminal colors usage.
__all__ = [
"color", "black", "dark_gray", "light_gray", "blue", "light_blue",
"green", "light_green", "cyan", "light_cyan", "red", "light_red",
"purple", "light_purple", "yellow", "brown", "white"
]
import os
# --------------------------------------------------------------------------- #
# Output Coloring Functions #
# --------------------------------------------------------------------------- #
_COLOR_FORMAT = '\033[%sm'
_COLOR_CODES = {
"start": "0;95",
"end": "0;0",
"black": "0;30", "dark gray": "1;30", "light gray": "0;37",
"blue" : "0;34", "light blue" : "1;34",
"green" : "0;32", "light green" : "1;32",
"cyan" : "0;36", "light cyan" :"1;36",
"red" : "0;31", "light red" : "1;31",
"purple": "0;35", "light purple": "1;35",
"brown" : "0;33",
"yellow": "1;33",
"white" : "1;37"
}
_COLORS_SUPPORTED = os.system('[ "$TERM" = "xterm-256color" ]') == 0
def _format(color_code):
return _COLOR_FORMAT % _COLOR_CODES[color_code]
_END_COLOR = _format("end")
def color(s, color):
return (_format(color) + str(s) + _END_COLOR) \
if (_COLORS_SUPPORTED) else str(s)
def black(str):
return color(str, "black")
def dark_gray(str):
return color(str, "dark gray")
def light_gray(str):
return color(str, "light gray")
def blue(str):
return color(str, "blue")
def light_blue(str):
return color(str, "light blue")
def green(str):
return color(str, "green")
def light_green(str):
return color(str, "light green")
def cyan(str):
return color(str, "cyan")
def light_cyan(str):
return color(str, "light cyan")
def red(str):
return color(str, "red")
def light_red(str):
return color(str, "light red")
def purple(str):
return color(str, "purple")
def light_purple(str):
return color(str, "light purple")
def yellow(str):
return color(str, "yellow")
def brown(str):
return color(str, "brown")
def white(str):
return color(str, "white")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment