diff --git a/lib/ansible/utils/color.py b/lib/ansible/utils/color.py index f32a5300777..75dd15cae72 100644 --- a/lib/ansible/utils/color.py +++ b/lib/ansible/utils/color.py @@ -17,6 +17,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +import re import sys from ansible import constants as C @@ -66,11 +67,28 @@ codeCodes = { 'normal': u'0' , } +def parsecolor(color): + """SGR parameter string for the specified color name.""" + matches = re.match(r"color(?P[0-9]+)" + r"|(?Prgb(?P[0-5])(?P[0-5])(?P[0-5]))" + r"|gray(?P[0-9]+)", color) + if not matches: + return codeCodes[color] + if matches.group('color'): + return u'38;5;%d' % int(matches.group('color')) + if matches.group('rgb'): + return u'38;5;%d' % (16 + 36 * int(matches.group('red')) + + 6 * int(matches.group('green')) + + int(matches.group('blue'))) + if matches.group('gray'): + return u'38;5;%d' % (232 + int(matches.group('gray'))) + def stringc(text, color): """String in color.""" if ANSIBLE_COLOR: - return "\n".join([u"\033[%sm%s\033[0m" % (codeCodes[color], t) for t in text.split('\n')]) + color_code = parsecolor(color) + return "\n".join([u"\033[%sm%s\033[0m" % (color_code, t) for t in text.split('\n')]) else: return text