Support 256-color terminals (#17750)

pull/21798/head
Yuri Khan 8 years ago committed by Brian Coca
parent 8824387cf8
commit a4ae0c85f1

@ -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<color>[0-9]+)"
r"|(?P<rgb>rgb(?P<red>[0-5])(?P<green>[0-5])(?P<blue>[0-5]))"
r"|gray(?P<gray>[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

Loading…
Cancel
Save