Fix coloring of error messages containing '}' symbol (#8277)

This commit is contained in:
Adam J. Stewart
2018-06-02 23:02:28 -05:00
committed by Todd Gamblin
parent 9862c97d38
commit fec11757d5
3 changed files with 30 additions and 11 deletions

View File

@@ -190,7 +190,7 @@ def __call__(self, match):
string = styles[style]
if color:
if color not in colors:
raise ColorParseError("invalid color specifier: '%s' in '%s'"
raise ColorParseError("Invalid color specifier: '%s' in '%s'"
% (color, match.string))
string += ';' + str(colors[color])
@@ -215,7 +215,9 @@ def colorize(string, **kwargs):
codes, for output to non-console devices.
"""
color = _color_when_value(kwargs.get('color', get_color_when()))
return re.sub(color_re, match_to_ansi(color), string)
string = re.sub(color_re, match_to_ansi(color), string)
string = string.replace('}}', '}')
return string
def clen(string):
@@ -224,14 +226,14 @@ def clen(string):
def cextra(string):
""""Length of extra color characters in a string"""
"""Length of extra color characters in a string"""
return len(''.join(re.findall(r'\033[^m]*m', string)))
def cwrite(string, stream=sys.stdout, color=None):
"""Replace all color expressions in string with ANSI control
codes and write the result to the stream. If color is
False, this will write plain text with o color. If True,
False, this will write plain text with no color. If True,
then it will always write colored output. If not supplied,
then it will be set based on stream.isatty().
"""
@@ -246,8 +248,25 @@ def cprint(string, stream=sys.stdout, color=None):
def cescape(string):
"""Replace all @ with @@ in the string provided."""
return str(string).replace('@', '@@')
"""Escapes special characters needed for color codes.
Replaces the following symbols with their equivalent literal forms:
===== ======
``@`` ``@@``
``}`` ``}}``
===== ======
Parameters:
string (str): the string to escape
Returns:
(str): the string with color codes escaped
"""
string = str(string)
string = string.replace('@', '@@')
string = string.replace('}', '}}')
return string
class ColorStream(object):