summaryrefslogtreecommitdiff
path: root/lib/spack/llnl/util/tty/color.py
diff options
context:
space:
mode:
authorAdam J. Stewart <ajstewart426@gmail.com>2018-06-02 23:02:28 -0500
committerTodd Gamblin <tgamblin@llnl.gov>2018-06-02 21:02:28 -0700
commitfec11757d5535e3b186069b6b0d2285fc7d054f7 (patch)
tree532084650976259c196b2f25a7fc4326ed8472b3 /lib/spack/llnl/util/tty/color.py
parent9862c97d386bf9d886efa0c192b09be309299dde (diff)
downloadspack-fec11757d5535e3b186069b6b0d2285fc7d054f7.tar.gz
spack-fec11757d5535e3b186069b6b0d2285fc7d054f7.tar.bz2
spack-fec11757d5535e3b186069b6b0d2285fc7d054f7.tar.xz
spack-fec11757d5535e3b186069b6b0d2285fc7d054f7.zip
Fix coloring of error messages containing '}' symbol (#8277)
Diffstat (limited to 'lib/spack/llnl/util/tty/color.py')
-rw-r--r--lib/spack/llnl/util/tty/color.py31
1 files changed, 25 insertions, 6 deletions
diff --git a/lib/spack/llnl/util/tty/color.py b/lib/spack/llnl/util/tty/color.py
index de26deceb2..3e40443a40 100644
--- a/lib/spack/llnl/util/tty/color.py
+++ b/lib/spack/llnl/util/tty/color.py
@@ -190,7 +190,7 @@ class match_to_ansi(object):
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):