summaryrefslogtreecommitdiff
path: root/lib/spack/llnl/util/tty/color.py
diff options
context:
space:
mode:
authorTodd Gamblin <tgamblin@llnl.gov>2022-07-30 15:19:18 -0700
committerTodd Gamblin <tgamblin@llnl.gov>2022-07-31 13:29:20 -0700
commitf52f6e99dbf1131886a80112b8c79dfc414afb7c (patch)
tree05cb7d64b2395922f2f24683da49f472075be12c /lib/spack/llnl/util/tty/color.py
parent549ba1ed32372c67fc57271cde3797d58b7dec6e (diff)
downloadspack-f52f6e99dbf1131886a80112b8c79dfc414afb7c.tar.gz
spack-f52f6e99dbf1131886a80112b8c79dfc414afb7c.tar.bz2
spack-f52f6e99dbf1131886a80112b8c79dfc414afb7c.tar.xz
spack-f52f6e99dbf1131886a80112b8c79dfc414afb7c.zip
black: reformat entire repository with black
Diffstat (limited to 'lib/spack/llnl/util/tty/color.py')
-rw-r--r--lib/spack/llnl/util/tty/color.py90
1 files changed, 46 insertions, 44 deletions
diff --git a/lib/spack/llnl/util/tty/color.py b/lib/spack/llnl/util/tty/color.py
index 99c0a5c7ac..1bc80331d4 100644
--- a/lib/spack/llnl/util/tty/color.py
+++ b/lib/spack/llnl/util/tty/color.py
@@ -76,29 +76,33 @@ class ColorParseError(Exception):
# Text styles for ansi codes
-styles = {'*': '1', # bold
- '_': '4', # underline
- None: '0'} # plain
+styles = {"*": "1", "_": "4", None: "0"} # bold # underline # plain
# Dim and bright ansi colors
-colors = {'k': 30, 'K': 90, # black
- 'r': 31, 'R': 91, # red
- 'g': 32, 'G': 92, # green
- 'y': 33, 'Y': 93, # yellow
- 'b': 34, 'B': 94, # blue
- 'm': 35, 'M': 95, # magenta
- 'c': 36, 'C': 96, # cyan
- 'w': 37, 'W': 97} # white
+colors = {
+ "k": 30,
+ "K": 90, # black
+ "r": 31,
+ "R": 91, # red
+ "g": 32,
+ "G": 92, # green
+ "y": 33,
+ "Y": 93, # yellow
+ "b": 34,
+ "B": 94, # blue
+ "m": 35,
+ "M": 95, # magenta
+ "c": 36,
+ "C": 96, # cyan
+ "w": 37,
+ "W": 97,
+} # white
# Regex to be used for color formatting
-color_re = r'@(?:@|\.|([*_])?([a-zA-Z])?(?:{((?:[^}]|}})*)})?)'
+color_re = r"@(?:@|\.|([*_])?([a-zA-Z])?(?:{((?:[^}]|}})*)})?)"
# Mapping from color arguments to values for tty.set_color
-color_when_values = {
- 'always': True,
- 'auto': None,
- 'never': False
-}
+color_when_values = {"always": True, "auto": None, "never": False}
# Force color; None: Only color if stdout is a tty
# True: Always colorize output, False: Never colorize output
@@ -114,7 +118,7 @@ def _color_when_value(when):
if when in color_when_values:
return color_when_values[when]
elif when not in color_when_values.values():
- raise ValueError('Invalid color setting: %s' % when)
+ raise ValueError("Invalid color setting: %s" % when)
return when
@@ -146,7 +150,6 @@ def color_when(value):
class match_to_ansi(object):
-
def __init__(self, color=True):
self.color = _color_when_value(color)
@@ -155,7 +158,7 @@ class match_to_ansi(object):
if self.color:
return "\033[%sm" % s
else:
- return ''
+ return ""
def __call__(self, match):
"""Convert a match object generated by ``color_re`` into an ansi
@@ -164,22 +167,22 @@ class match_to_ansi(object):
style, color, text = match.groups()
m = match.group(0)
- if m == '@@':
- return '@'
- elif m == '@.':
+ if m == "@@":
+ return "@"
+ elif m == "@.":
return self.escape(0)
- elif m == '@':
- raise ColorParseError("Incomplete color format: '%s' in %s"
- % (m, match.string))
+ elif m == "@":
+ raise ColorParseError("Incomplete color format: '%s' in %s" % (m, match.string))
string = styles[style]
if color:
if color not in colors:
- raise ColorParseError("Invalid color specifier: '%s' in '%s'"
- % (color, match.string))
- string += ';' + str(colors[color])
+ raise ColorParseError(
+ "Invalid color specifier: '%s' in '%s'" % (color, match.string)
+ )
+ string += ";" + str(colors[color])
- colored_text = ''
+ colored_text = ""
if text:
colored_text = text + self.escape(0)
@@ -199,28 +202,28 @@ def colorize(string, **kwargs):
color (bool): If False, output will be plain text without control
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()))
string = re.sub(color_re, match_to_ansi(color), string)
- string = string.replace('}}', '}')
+ string = string.replace("}}", "}")
return string
def clen(string):
"""Return the length of a string, excluding ansi color sequences."""
- return len(re.sub(r'\033[^m]*m', '', string))
+ return len(re.sub(r"\033[^m]*m", "", string))
def cextra(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=None, 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 no color. If True,
- then it will always write colored output. If not supplied,
- then it will be set based on stream.isatty().
+ codes and write the result to the stream. If color is
+ 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().
"""
stream = sys.stdout if stream is None else stream
if color is None:
@@ -251,20 +254,19 @@ def cescape(string):
(str): the string with color codes escaped
"""
string = six.text_type(string)
- string = string.replace('@', '@@')
- string = string.replace('}', '}}')
+ string = string.replace("@", "@@")
+ string = string.replace("}", "}}")
return string
class ColorStream(object):
-
def __init__(self, stream, color=None):
self._stream = stream
self._color = color
def write(self, string, **kwargs):
- raw = kwargs.get('raw', False)
- raw_write = getattr(self._stream, 'write')
+ raw = kwargs.get("raw", False)
+ raw_write = getattr(self._stream, "write")
color = self._color
if self._color is None:
@@ -275,6 +277,6 @@ class ColorStream(object):
raw_write(colorize(string, color=color))
def writelines(self, sequence, **kwargs):
- raw = kwargs.get('raw', False)
+ raw = kwargs.get("raw", False)
for string in sequence:
self.write(string, self.color, raw=raw)