diff options
Diffstat (limited to 'lib/spack/spack/environment/shell.py')
-rw-r--r-- | lib/spack/spack/environment/shell.py | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/lib/spack/spack/environment/shell.py b/lib/spack/spack/environment/shell.py index a4f9634a8d..7ead2093a7 100644 --- a/lib/spack/spack/environment/shell.py +++ b/lib/spack/spack/environment/shell.py @@ -3,6 +3,7 @@ # # SPDX-License-Identifier: (Apache-2.0 OR MIT) import os +import textwrap from typing import Optional import llnl.util.tty as tty @@ -54,22 +55,35 @@ def activate_header(env, shell, prompt=None, view: Optional[str] = None): if view: cmds += "$Env:SPACK_ENV_VIEW='%s'\n" % view else: - if "color" in os.getenv("TERM", "") and prompt: - prompt = colorize("@G{%s}" % prompt, color=True, enclose=True) + bash_color_prompt = colorize(f"@G{{{prompt}}}", color=True, enclose=True) + zsh_color_prompt = colorize(f"@G{{{prompt}}}", color=True, enclose=False, zsh=True) cmds += "export SPACK_ENV=%s;\n" % env.path if view: cmds += "export SPACK_ENV_VIEW=%s;\n" % view cmds += "alias despacktivate='spack env deactivate';\n" if prompt: - cmds += "if [ -z ${SPACK_OLD_PS1+x} ]; then\n" - cmds += " if [ -z ${PS1+x} ]; then\n" - cmds += " PS1='$$$$';\n" - cmds += " fi;\n" - cmds += ' export SPACK_OLD_PS1="${PS1}";\n' - cmds += "fi;\n" - cmds += 'export PS1="%s ${PS1}";\n' % prompt - + cmds += textwrap.dedent( + rf""" + if [ -z ${{SPACK_OLD_PS1+x}} ]; then + if [ -z ${{PS1+x}} ]; then + PS1='$$$$'; + fi; + export SPACK_OLD_PS1="${{PS1}}"; + fi; + if [ -n "${{TERM:-}}" ] && [ "${{TERM#*color}}" != "${{TERM}}" ] && \ + [ -n "${{BASH:-}}" ]; + then + export PS1="{bash_color_prompt} ${{PS1}}"; + elif [ -n "${{TERM:-}}" ] && [ "${{TERM#*color}}" != "${{TERM}}" ] && \ + [ -n "${{ZSH_NAME:-}}" ]; + then + export PS1="{zsh_color_prompt} ${{PS1}}"; + else + export PS1="{prompt} ${{PS1}}"; + fi + """ + ).lstrip("\n") return cmds |