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