toolbox/profile.d/toolbox.sh
Debarshi Ray 61efad34bc profile.d: Fix the PS1 on Z shell
Currently, on Fedora, a nested instance of Z shell inside a Toolbox
container renders the PS1 like this:
\[\]⬢\[\][\u@\h \W]\$

Notice that Z shell doesn't like that the terminal escape sequences
for the foreground colour are wrapped in '\[' and '\]' [1], and doesn't
understand the special characters like '\u' and '\h'.

This is fixed by making the PS1 specific to the shell. The prompt for
Z shell is based on the default prompt used on Fedora, just like the
one for Bash.

Note that this only affects nested instances of Z shell because of the
way the start-up scripts for Z shell are written on Fedora. Toolbox
invokes top-level shell as a login shell, and for those the PS1 set by
profile.d/toolbox.sh is overwritten by the operating system's default
in /etc/zshrc. See:
https://bugzilla.redhat.com/show_bug.cgi?id=2026749

[1] Commit bc1a816ea3
    https://github.com/debarshiray/toolbox/issues/190

https://github.com/containers/toolbox/pull/936
2021-11-25 19:04:20 +01:00

91 lines
3.2 KiB
Bash

# shellcheck shell=sh
# shellcheck disable=SC2153
[ "${BASH_VERSION:-}" != "" ] || [ "${ZSH_VERSION:-}" != "" ] || return 0
[ "$PS1" != "" ] || return 0
toolbox_config="$HOME/.config/toolbox"
host_welcome_stub="$toolbox_config/host-welcome-shown"
toolbox_welcome_stub="$toolbox_config/toolbox-welcome-shown"
# shellcheck disable=2046
# shellcheck disable=SC1091
eval $(
. /usr/lib/os-release
echo ID="$ID"
echo VARIANT_ID="$VARIANT_ID"
)
if [ -f /run/ostree-booted ] \
&& ! [ -f "$host_welcome_stub" ] \
&& [ "${ID}" = "fedora" ] \
&& { [ "${VARIANT_ID}" = "workstation" ] || [ "${VARIANT_ID}" = "silverblue" ] || [ "${VARIANT_ID}" = "kinoite" ]; }; then
echo ""
# shellcheck disable=SC3059
echo "Welcome to Fedora ${VARIANT_ID^}. This terminal is running on the"
echo "host system. You may want to try out the Toolbox for a directly"
echo "mutable environment that allows package installation with DNF."
echo ""
printf "For more information, see the "
# shellcheck disable=SC1003
printf '\033]8;;https://docs.fedoraproject.org/en-US/fedora-silverblue/toolbox/\033\\documentation\033]8;;\033\\'
printf ".\n"
echo ""
mkdir -p "$toolbox_config"
touch "$host_welcome_stub"
fi
if [ -f /run/.containerenv ] \
&& [ -f /run/.toolboxenv ]; then
[ "${BASH_VERSION:-}" != "" ] && PS1=$(printf "\[\033[35m\]⬢\[\033[0m\]%s" "[\u@\h \W]\\$ ")
[ "${ZSH_VERSION:-}" != "" ] && PS1=$(printf "\033[35m⬢\033[0m%s" "[%n@%m]%~%# ")
if ! [ -f "$toolbox_welcome_stub" ]; then
echo ""
echo "Welcome to the Toolbox; a container where you can install and run"
echo "all your tools."
echo ""
echo " - Use DNF in the usual manner to install command line tools."
echo " - To create a new tools container, run 'toolbox create'."
echo ""
printf "For more information, see the "
# shellcheck disable=SC1003
printf '\033]8;;https://docs.fedoraproject.org/en-US/fedora-silverblue/toolbox/\033\\documentation\033]8;;\033\\'
printf ".\n"
echo ""
mkdir -p "$toolbox_config"
touch "$toolbox_welcome_stub"
fi
if ! [ -f /etc/profile.d/vte.sh ] && [ -z "$PROMPT_COMMAND" ] && [ "${VTE_VERSION:-0}" -ge 3405 ]; then
case "$TERM" in
xterm*|vte*)
[ -n "${BASH_VERSION:-}" ] && PROMPT_COMMAND=" "
;;
esac
fi
if [ "$TERM" != "" ]; then
error_message="Error: terminfo entry not found for $TERM"
term_without_first_character="${TERM#?}"
term_just_first_character="${TERM%$term_without_first_character}"
terminfo_sub_directory="$term_just_first_character/$TERM"
if [ "$TERMINFO" = "" ]; then
! [ -e "/usr/share/terminfo/$terminfo_sub_directory" ] \
&& ! [ -e "/lib/terminfo/$terminfo_sub_directory" ] \
&& ! [ -e "$HOME/.terminfo/$terminfo_sub_directory" ] \
&& echo "$error_message" >&2
else
! [ -e "$TERMINFO/$terminfo_sub_directory" ] \
&& echo "$error_message" >&2
fi
fi
fi
unset toolbox_config
unset host_welcome_stub
unset toolbox_welcome_stub