Skip to content

Instantly share code, notes, and snippets.

@KelSolaar KelSolaar/ansiColors.py
Last active Aug 29, 2015

Embed
What would you like to do?
ANSI Colors
BACKGROUND_CODES = (("bBlack", "40m"),
("bRed", "41m"),
("bGreen", "42m"),
("bYellow", "43m"),
("bBlue", "44m"),
("bPurple", "45m"),
("bCyan", "46m"),
("bWhite", "47m"),
("bHighBlack", "100m"),
("bHighRed", "101m"),
("bHighGreen", "102m"),
("bHighYellow", "103m"),
("bHighBlue", "104m"),
("bHighPurple", "105m"),
("bHighCyan", "106m"),
("bHighWhite", "107m"))
FOREGROUND_CODES = (("black", "30m"),
("red", "31m"),
("green", "32m"),
("yellow", "33m"),
("blue", "34m"),
("purple", "35m"),
("cyan", "36m"),
("white", "37m"),
("boldBlack", "1;30m"),
("boldRed", "1;31m"),
("boldGreen", "1;32m"),
("boldYellow", "1;33m"),
("boldBlue", "1;34m"),
("boldPurple", "1;35m"),
("boldCyan", "1;36m"),
("boldWhite", "1;37m"),
("underlineBlack", "4;30m"),
("underlineRed", "4;31m"),
("underlineGreen", "4;32m"),
("underlineYellow", "4;33m"),
("underlineBlue", "4;34m"),
("underlinePurple", "4;35m"),
("underlineCyan", "4;36m"),
("underlineWhite", "4;37m"),
("highBlack", "0;90m"),
("highRed", "0;91m"),
("highGreen", "0;92m"),
("highYellow", "0;93m"),
("highBlue", "0;94m"),
("highPurple", "0;95m"),
("highCyan", "0;96m"),
("highWhite", "0;97m"),
("highBoldBlack", "1;90m"),
("highBoldRed", "1;91m"),
("highBoldGreen", "1;92m"),
("highBoldYellow", "1;93m"),
("highBoldBlue", "1;94m"),
("highBoldPurple", "1;95m"),
("highBoldCyan", "1;96m"),
("highBoldWhite", "1;97m"))
class AnsiColors():
"""
Defines *ANSI* colors escape codes.
"""
reset = "\033[0m"
def _setAnsiColors():
"""
Injects *ANSI* colors escape codes into :class:`AnsiColors` class.
"""
setColorAttribute = lambda x, y: setattr(AnsiColors, x, y)
for foregroundCodeName, foregroundCode in FOREGROUND_CODES:
setColorAttribute(foregroundCodeName,
"\033[{0}".format(foregroundCode))
for backgroundCodeName, backgroundCode in BACKGROUND_CODES:
setColorAttribute(backgroundCodeName,
"\033[{0}".format(backgroundCode))
for backgroundCodeName, backgroundCode in BACKGROUND_CODES:
for foregroundCodeName, foregroundCode in FOREGROUND_CODES:
setColorAttribute("{0}{1}".format(foregroundCodeName,
"{0}{1}".format(backgroundCodeName[0].upper(), backgroundCodeName[1:])),
"\033[{0}\033[{1}".format(foregroundCode, backgroundCode))
_setAnsiColors()
import pprint
pprint.pprint(dir(AnsiColors))
print AnsiColors.bRed
print AnsiColors.bHighCyan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.