summaryrefslogtreecommitdiff
path: root/lib/spack/llnl/util/tty/color.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/spack/llnl/util/tty/color.py')
-rw-r--r--lib/spack/llnl/util/tty/color.py58
1 files changed, 29 insertions, 29 deletions
diff --git a/lib/spack/llnl/util/tty/color.py b/lib/spack/llnl/util/tty/color.py
index c1365a93a0..53a1150ada 100644
--- a/lib/spack/llnl/util/tty/color.py
+++ b/lib/spack/llnl/util/tty/color.py
@@ -1,27 +1,8 @@
-##############################################################################
-# Copyright (c) 2013-2017, Lawrence Livermore National Security, LLC.
-# Produced at the Lawrence Livermore National Laboratory.
+# Copyright 2013-2018 Lawrence Livermore National Security, LLC and other
+# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
-# This file is part of Spack.
-# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
-# LLNL-CODE-647188
-#
-# For details, see https://github.com/spack/spack
-# Please also see the NOTICE and LICENSE files for our notice and the LGPL.
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU Lesser General Public License (as
-# published by the Free Software Foundation) version 2.1, February 1999.
-#
-# This program is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
-# conditions of the GNU Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-##############################################################################
+# SPDX-License-Identifier: (Apache-2.0 OR MIT)
+
"""
This file implements an expression syntax, similar to ``printf``, for adding
ANSI colors to text.
@@ -190,7 +171,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 +196,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 +207,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 +229,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):