Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if with_reset:
result = '\x1b[0m'
for c in colors:
#
result += '\x1b['
#
if c == 'reset':
result += '0'
elif c == 'normal':
result += '38'
elif c in termcolor.COLORS:
result += str(termcolor.COLORS[c])
elif c in termcolor.ATTRIBUTES:
result += str(termcolor.ATTRIBUTES[c])
elif c in termcolor.HIGHLIGHTS:
result += str(termcolor.HIGHLIGHTS[c])
elif self._RGB_COLOR_SPEC_RE.match(c):
# BUG Fucking Python! Why not to assign and check a variable inside of `if`
# TODO Avoid double regex match
match = self._RGB_COLOR_SPEC_RE.search(c)
try:
r = self._validate_rgb_component(int(match.group(1)))
g = self._validate_rgb_component(int(match.group(2)))
b = self._validate_rgb_component(int(match.group(3)))
if r <= 5 and g <= 5 and b <= 5:
index = self._rgb_to_index(r, g, b)
result += '38;5;' + str(index)
else:
result += '38;2;{};{};{}'.format(r, g, b)
except ValueError:
raise RuntimeError(
def safe_colored(text, color=None, on_color=None, attrs=None, rlsafe=False):
if os.getenv('ANSI_COLORS_DISABLED') is None:
fmt_str = '\033[%dm'
if rlsafe:
fmt_str = '\001' + fmt_str + '\002'
fmt_str += '%s'
if color is not None:
text = fmt_str % (termcolor.COLORS[color], text)
if on_color is not None:
text = fmt_str % (termcolor.HIGHLIGHTS[on_color], text)
if attrs is not None:
for attr in attrs:
text = fmt_str % (termcolor.ATTRIBUTES[attr], text)
if rlsafe:
text += '\001' + termcolor.RESET + '\002'
else:
text += termcolor.RESET
return text