zsh: reduce the number of global variables used for colors in prompt

This commit is contained in:
Fernando Schauenburg 2024-07-27 22:41:51 +02:00
parent bb3f6a7b6f
commit 62dd4efc38

View file

@ -1,13 +1,20 @@
fg_reset='%{%f%}'
fg_black='%{%F{black}%}'
fg_red='%{%F{red}%}'
fg_green='%{%F{green}%}'
fg_yellow='%{%F{yellow}%}'
fg_blue='%{%F{blue}%}'
fg_magenta='%{%F{magenta}%}'
fg_cyan='%{%F{cyan}%}'
fg_white='%{%F{white}%}'
fg_gray='%{%F{8}%}' # %F{...} only supports the 8 basic colors by name
typeset -gA zfg=(
[rst]='%{%f%}'
[black]='%{%F{black}%}'
[red]='%{%F{red}%}'
[green]='%{%F{green}%}'
[yellow]='%{%F{yellow}%}'
[blue]='%{%F{blue}%}'
[magenta]='%{%F{magenta}%}'
[cyan]='%{%F{cyan}%}'
[white]='%{%F{white}%}'
# %F{...} only supports the 8 basic colors by name
[gray]='%{%F{8}%}'
[bright]='%{%F{15}%}'
[faded]='%{%F{240}%}'
)
render_prompt() {
setopt localoptions shortloops
@ -22,11 +29,11 @@ render_prompt() {
"$(render_exec_time)"
)
local separator="${fg_gray} "
local separator="${zfg[gray]} "
echo ${(pj.$separator.)sections:#}
echo -n "${fg_gray}"
echo -n "${zfg[gray]}"
printf '%.0s' {1..$SHLVL}
echo -n "${fg_reset} "
echo -n "${zfg[rst]} "
}
render_exit_code() {
@ -44,22 +51,22 @@ render_user_host() {
# username in red if root, yellow if otherwise relevant
if [[ $UID == 0 ]]; then
parts+="${fg_red}%n"
parts+="${zfg[red]}%n"
elif [[ $LOGNAME != $USER ]] || [[ -n $SSH_CONNECTION ]]; then
parts+="${fg_yellow}%n"
parts+="${zfg[yellow]}%n"
fi
# hostname in yellow if relevant
[[ -n $SSH_CONNECTION ]] && parts+="${fg_yellow}%m"
[[ -n $SSH_CONNECTION ]] && parts+="${zfg[yellow]}%m"
(($#parts)) && {
local separator="${fg_gray}@"
local separator="${zfg[gray]}@"
print "${(pj:$separator:)parts}"
}
}
render_pwd() {
print "${fg_cyan}%~${fg_reset}"
print "${zfg[cyan]}%~"
}
render_git() {
@ -129,29 +136,29 @@ render_git() {
}
local track_parts=()
(($ahead > 0 )) && track_parts+="${fg_blue}${ahead}"
(($behind > 0 )) && track_parts+="${fg_cyan}${behind}"
(($ahead > 0 )) && track_parts+="${zfg[blue]}${ahead}"
(($behind > 0 )) && track_parts+="${zfg[cyan]}${behind}"
local state_parts=()
(($staged > 0)) && state_parts+="${fg_green}+${staged}"
(($dirty > 0 )) && state_parts+="${fg_red}${dirty}"
(($staged > 0)) && state_parts+="${zfg[green]}+${staged}"
(($dirty > 0 )) && state_parts+="${zfg[red]}${dirty}"
local separator="${fg_gray}"
local gitinfo=("${fg_blue}${branch}")
local separator="${zfg[gray]}"
local gitinfo=("${zfg[blue]}${branch}")
(($#track_parts > 0)) && gitinfo+="${(pj:$separator:)track_parts}"
(($conflicts > 0 )) && gitinfo+="${fg_red}${conflicts}"
(($untracked > 0 )) && gitinfo+="${fg_white}${untracked}?"
(($conflicts > 0 )) && gitinfo+="${zfg[red]}${conflicts}"
(($untracked > 0 )) && gitinfo+="${zfg[white]}${untracked}?"
(($#state_parts)) && gitinfo+="${(pj:$separator:)state_parts}"
print "${(j: :)gitinfo}"
}
render_venv() {
[[ -n "$VIRTUAL_ENV" ]] && print "${fg_green}${VIRTUAL_ENV:t}"
[[ -n "$VIRTUAL_ENV" ]] && print "${zfg[green]}${VIRTUAL_ENV:t}"
}
render_jobs() {
(($PROMPT_JOB_COUNT > 0)) && print "${fg_magenta}%j bg"
(($PROMPT_JOB_COUNT > 0)) && print "${zfg[magenta]}%j bg"
}
render_exec_time() {
@ -163,7 +170,7 @@ render_exec_time() {
"$((PROMPT_EXEC_TIME / 60 % 60))m" # minutes
"$((PROMPT_EXEC_TIME % 60))s" # seconds
)
print ${fg_gray}${parts:#0*} # only keep non-zero parts
print ${zfg[gray]}${parts:#0*} # only keep non-zero parts
}
# Hook triggered when a command is about to be executed.