zsh: remove the need to render the prompt in a global array

This commit is contained in:
Fernando Schauenburg 2024-07-27 22:41:09 +02:00
parent 3c8857b1f6
commit bb3f6a7b6f

View file

@ -9,34 +9,33 @@ fg_cyan='%{%F{cyan}%}'
fg_white='%{%F{white}%}' fg_white='%{%F{white}%}'
fg_gray='%{%F{8}%}' # %F{...} only supports the 8 basic colors by name fg_gray='%{%F{8}%}' # %F{...} only supports the 8 basic colors by name
PROMPT_SECTIONS=()
render_prompt() { render_prompt() {
setopt localoptions shortloops setopt localoptions shortloops
PROMPT_SECTIONS=() local sections=(
render_exit_code "$(render_exit_code)"
render_user_host "$(render_user_host)"
render_pwd "$(render_pwd)"
render_git "$(render_git)"
render_venv "$(render_venv)"
render_jobs "$(render_jobs)"
render_exec_time "$(render_exec_time)"
)
local separator="${fg_gray} " local separator="${fg_gray} "
echo ${(pj.$separator.)PROMPT_SECTIONS} echo ${(pj.$separator.)sections:#}
echo -n "${fg_gray}" echo -n "${fg_gray}"
printf '%.0s' {1..$SHLVL} printf '%.0s' {1..$SHLVL}
echo -n "${fg_reset} " echo -n "${fg_reset} "
} }
render_exit_code() { render_exit_code() {
if ((PROMPT_EXIT_CODE != 0)); then ((PROMPT_EXIT_CODE == 0)) && return
if ((PROMPT_EXIT_CODE > 128 && PROMPT_EXIT_CODE < 160)); then if ((PROMPT_EXIT_CODE > 128 && PROMPT_EXIT_CODE < 160)); then
PROMPT_SECTIONS+="${fg_white}$(kill -l $PROMPT_EXIT_CODE)" print "${fg_white}$(kill -l $PROMPT_EXIT_CODE)"
else else
PROMPT_SECTIONS+="${fg_red}$PROMPT_EXIT_CODE" print "${fg_red}$PROMPT_EXIT_CODE"
fi
fi fi
} }
@ -55,12 +54,12 @@ render_user_host() {
(($#parts)) && { (($#parts)) && {
local separator="${fg_gray}@" local separator="${fg_gray}@"
PROMPT_SECTIONS+="${(pj:$separator:)parts}" print "${(pj:$separator:)parts}"
} }
} }
render_pwd() { render_pwd() {
PROMPT_SECTIONS+="${fg_cyan}%~${fg_reset}" print "${fg_cyan}%~${fg_reset}"
} }
render_git() { render_git() {
@ -144,27 +143,27 @@ render_git() {
(($untracked > 0 )) && gitinfo+="${fg_white}${untracked}?" (($untracked > 0 )) && gitinfo+="${fg_white}${untracked}?"
(($#state_parts)) && gitinfo+="${(pj:$separator:)state_parts}" (($#state_parts)) && gitinfo+="${(pj:$separator:)state_parts}"
PROMPT_SECTIONS+="${(j: :)gitinfo}" print "${(j: :)gitinfo}"
} }
render_venv() { render_venv() {
[[ -n "$VIRTUAL_ENV" ]] && PROMPT_SECTIONS+="${fg_green}${VIRTUAL_ENV:t}" [[ -n "$VIRTUAL_ENV" ]] && print "${fg_green}${VIRTUAL_ENV:t}"
} }
render_jobs() { render_jobs() {
(($PROMPT_JOB_COUNT > 0)) && PROMPT_SECTIONS+="${fg_magenta}%j bg" (($PROMPT_JOB_COUNT > 0)) && print "${fg_magenta}%j bg"
} }
render_exec_time() { render_exec_time() {
(($PROMPT_EXEC_TIME > 3)) && { # last command execution time if over 3s (($PROMPT_EXEC_TIME <= 3)) && return # don't print time if under 3s
local parts=( local parts=(
"$((PROMPT_EXEC_TIME / 60 / 60 / 24))d" # days "$((PROMPT_EXEC_TIME / 60 / 60 / 24))d" # days
"$((PROMPT_EXEC_TIME / 60 / 60 % 24))h" # hours "$((PROMPT_EXEC_TIME / 60 / 60 % 24))h" # hours
"$((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
) )
PROMPT_SECTIONS+=${fg_gray}${parts:#0*} # only keep non-zero parts print ${fg_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.