1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Support 256-color terminals (#17750)

This commit is contained in:
Yuri Khan 2016-09-26 14:52:07 +07:00 committed by Brian Coca
parent 8824387cf8
commit a4ae0c85f1

View file

@ -17,6 +17,7 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import re
import sys import sys
from ansible import constants as C from ansible import constants as C
@ -66,11 +67,28 @@ codeCodes = {
'normal': u'0' , '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): def stringc(text, color):
"""String in color.""" """String in color."""
if ANSIBLE_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: else:
return text return text