2016-04-30 22:23:46 -07:00
|
|
|
"""A set of common utilities used within the environments. These are
|
|
|
|
not intended as API functions, and will not remain stable over time.
|
|
|
|
"""
|
|
|
|
|
|
|
|
color2num = dict(
|
|
|
|
gray=30,
|
|
|
|
red=31,
|
|
|
|
green=32,
|
|
|
|
yellow=33,
|
|
|
|
blue=34,
|
|
|
|
magenta=35,
|
|
|
|
cyan=36,
|
|
|
|
white=37,
|
2021-07-29 02:26:34 +02:00
|
|
|
crimson=38,
|
2016-04-30 22:23:46 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-29 02:26:34 +02:00
|
|
|
def colorize(string, color, bold=False, highlight=False):
|
2016-04-30 22:23:46 -07:00
|
|
|
"""Return string surrounded by appropriate terminal color codes to
|
|
|
|
print colorized text. Valid colors: gray, red, green, yellow,
|
|
|
|
blue, magenta, cyan, white, crimson
|
|
|
|
"""
|
|
|
|
|
|
|
|
attr = []
|
|
|
|
num = color2num[color]
|
2021-07-29 02:26:34 +02:00
|
|
|
if highlight:
|
|
|
|
num += 10
|
2020-04-10 17:10:34 -05:00
|
|
|
attr.append(str(num))
|
2021-07-29 02:26:34 +02:00
|
|
|
if bold:
|
|
|
|
attr.append("1")
|
|
|
|
attrs = ";".join(attr)
|
|
|
|
return "\x1b[%sm%s\x1b[0m" % (attrs, string)
|