summaryrefslogtreecommitdiff
path: root/lib/spack/llnl/util/tty/color.py
diff options
context:
space:
mode:
authorBrian Vanderwende <vanderwb@ucar.edu>2022-10-10 07:29:58 -0600
committerGitHub <noreply@github.com>2022-10-10 07:29:58 -0600
commit27cf8dddec398dcc779067f38a7bbc62b32c3d17 (patch)
tree3fe5984f1715c7e6c32e0ef5580e9cc609f08055 /lib/spack/llnl/util/tty/color.py
parent7cb745b03a81093d452deebe2cb4fbcf3635feec (diff)
downloadspack-27cf8dddec398dcc779067f38a7bbc62b32c3d17.tar.gz
spack-27cf8dddec398dcc779067f38a7bbc62b32c3d17.tar.bz2
spack-27cf8dddec398dcc779067f38a7bbc62b32c3d17.tar.xz
spack-27cf8dddec398dcc779067f38a7bbc62b32c3d17.zip
shell prompt: enclose control sequence in brackets (#33079)
When setting `PS1` in Bash, it's required to enclose non-printable characters in square brackets, so that the width of the terminal is handled correctly. See https://www.gnu.org/software/bash/manual/bash.html#Controlling-the-Prompt
Diffstat (limited to 'lib/spack/llnl/util/tty/color.py')
-rw-r--r--lib/spack/llnl/util/tty/color.py12
1 files changed, 9 insertions, 3 deletions
diff --git a/lib/spack/llnl/util/tty/color.py b/lib/spack/llnl/util/tty/color.py
index 1bc80331d4..ad6862fc28 100644
--- a/lib/spack/llnl/util/tty/color.py
+++ b/lib/spack/llnl/util/tty/color.py
@@ -150,13 +150,17 @@ def color_when(value):
class match_to_ansi(object):
- def __init__(self, color=True):
+ def __init__(self, color=True, enclose=False):
self.color = _color_when_value(color)
+ self.enclose = enclose
def escape(self, s):
"""Returns a TTY escape sequence for a color"""
if self.color:
- return "\033[%sm" % s
+ if self.enclose:
+ return r"\[\033[%sm\]" % s
+ else:
+ return "\033[%sm" % s
else:
return ""
@@ -201,9 +205,11 @@ def colorize(string, **kwargs):
Keyword Arguments:
color (bool): If False, output will be plain text without control
codes, for output to non-console devices.
+ enclose (bool): If True, enclose ansi color sequences with
+ square brackets to prevent misestimation of terminal width.
"""
color = _color_when_value(kwargs.get("color", get_color_when()))
- string = re.sub(color_re, match_to_ansi(color), string)
+ string = re.sub(color_re, match_to_ansi(color, kwargs.get("enclose")), string)
string = string.replace("}}", "}")
return string