1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
"""
Generates a dictionary of ANSI escape codes
http://en.wikipedia.org/wiki/ANSI_escape_code
Uses colorama as an optional dependancy to support color on Windows
"""
try:
import colorama
except ImportError:
pass
else:
colorama.init()
__all__ = ['escape_codes']
# Returns escape codes from format codes
esc = lambda *x: '\033[' + ';'.join(x) + 'm'
# The initial list of escape codes
escape_codes = {
'reset': esc('0'),
'bold': esc('01'),
}
# The color names
COLORS = [
'black',
'red',
'green',
'yellow',
'blue',
'purple',
'cyan',
'white'
]
PREFIXES = [
# Foreground without prefix
('3', ''), ('01;3', 'bold_'),
# Foreground with fg_ prefix
('3', 'fg_'), ('01;3', 'fg_bold_'),
# Background with bg_ prefix - bold/light works differently
('4', 'bg_'), ('10', 'bg_bold_'),
]
for prefix, prefix_name in PREFIXES:
for code, name in enumerate(COLORS):
escape_codes[prefix_name + name] = esc(prefix + str(code))
|