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] string = styles[style]
if color: if color:
if color not in colors: if color not in colors:
raise ColorParseError("invalid color specifier: '%s' in '%s'" raise ColorParseError("Invalid color specifier: '%s' in '%s'"
% (color, match.string)) % (color, match.string))
string += ';' + str(colors[color]) string += ';' + str(colors[color])
@ -215,7 +215,9 @@ def colorize(string, **kwargs):
codes, for output to non-console devices. codes, for output to non-console devices.
""" """
color = _color_when_value(kwargs.get('color', get_color_when())) 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): def clen(string):
@ -224,14 +226,14 @@ def clen(string):
def cextra(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))) return len(''.join(re.findall(r'\033[^m]*m', string)))
def cwrite(string, stream=sys.stdout, color=None): def cwrite(string, stream=sys.stdout, color=None):
"""Replace all color expressions in string with ANSI control """Replace all color expressions in string with ANSI control
codes and write the result to the stream. If color is 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 always write colored output. If not supplied,
then it will be set based on stream.isatty(). then it will be set based on stream.isatty().
""" """
@ -246,8 +248,25 @@ def cprint(string, stream=sys.stdout, color=None):
def cescape(string): def cescape(string):
"""Replace all @ with @@ in the string provided.""" """Escapes special characters needed for color codes.
return str(string).replace('@', '@@')
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): class ColorStream(object):

View file

@ -62,7 +62,7 @@
from six import StringIO from six import StringIO
import llnl.util.tty as tty import llnl.util.tty as tty
from llnl.util.tty.color import colorize from llnl.util.tty.color import cescape, colorize
from llnl.util.filesystem import mkdirp, install, install_tree from llnl.util.filesystem import mkdirp, install, install_tree
import spack.build_systems.cmake import spack.build_systems.cmake
@ -801,7 +801,7 @@ def make_stack(tb, stack=None):
marked = ' {0}{1:-6d}{2}'.format( marked = ' {0}{1:-6d}{2}'.format(
mark, start + start_ctx + i, line.rstrip()) mark, start + start_ctx + i, line.rstrip())
if is_error: if is_error:
marked = colorize('@R{%s}' % marked) marked = colorize('@R{%s}' % cescape(marked))
lines.append(marked) lines.append(marked)
return lines return lines

View file

@ -30,7 +30,7 @@
from ctest_log_parser import CTestLogParser, BuildError, BuildWarning from ctest_log_parser import CTestLogParser, BuildError, BuildWarning
import llnl.util.tty as tty import llnl.util.tty as tty
from llnl.util.tty.color import colorize from llnl.util.tty.color import cescape, colorize
__all__ = ['parse_log_events', 'make_log_context'] __all__ = ['parse_log_events', 'make_log_context']
@ -130,8 +130,8 @@ def make_log_context(log_events, width=None):
wrapped_line = line_fmt % (i, '\n'.join(lines)) wrapped_line = line_fmt % (i, '\n'.join(lines))
if i in error_lines: if i in error_lines:
out.write( out.write(colorize(
colorize(' @%s{>> %s}\n' % (color, wrapped_line))) ' @%s{>> %s}\n' % (color, cescape(wrapped_line))))
else: else:
out.write(' %s\n' % wrapped_line) out.write(' %s\n' % wrapped_line)