From a4ae0c85f184ffe1aeef7821930622884f93c13d Mon Sep 17 00:00:00 2001 From: Yuri Khan Date: Mon, 26 Sep 2016 14:52:07 +0700 Subject: [PATCH] Support 256-color terminals (#17750) --- lib/ansible/utils/color.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/ansible/utils/color.py b/lib/ansible/utils/color.py index f32a530077..75dd15cae7 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