zsh: remove the need to render the prompt in a global array
This commit is contained in:
parent
3c8857b1f6
commit
bb3f6a7b6f
1 changed files with 30 additions and 31 deletions
|
@ -9,34 +9,33 @@ fg_cyan='%{%F{cyan}%}'
|
|||
fg_white='%{%F{white}%}'
|
||||
fg_gray='%{%F{8}%}' # %F{...} only supports the 8 basic colors by name
|
||||
|
||||
PROMPT_SECTIONS=()
|
||||
|
||||
render_prompt() {
|
||||
setopt localoptions shortloops
|
||||
|
||||
PROMPT_SECTIONS=()
|
||||
render_exit_code
|
||||
render_user_host
|
||||
render_pwd
|
||||
render_git
|
||||
render_venv
|
||||
render_jobs
|
||||
render_exec_time
|
||||
local sections=(
|
||||
"$(render_exit_code)"
|
||||
"$(render_user_host)"
|
||||
"$(render_pwd)"
|
||||
"$(render_git)"
|
||||
"$(render_venv)"
|
||||
"$(render_jobs)"
|
||||
"$(render_exec_time)"
|
||||
)
|
||||
|
||||
local separator="${fg_gray} ❯ "
|
||||
echo ${(pj.$separator.)PROMPT_SECTIONS}
|
||||
echo ${(pj.$separator.)sections:#}
|
||||
echo -n "${fg_gray}"
|
||||
printf '❯%.0s' {1..$SHLVL}
|
||||
echo -n "${fg_reset} "
|
||||
}
|
||||
|
||||
render_exit_code() {
|
||||
if ((PROMPT_EXIT_CODE != 0)); then
|
||||
if ((PROMPT_EXIT_CODE > 128 && PROMPT_EXIT_CODE < 160)); then
|
||||
PROMPT_SECTIONS+="${fg_white}$(kill -l $PROMPT_EXIT_CODE)"
|
||||
else
|
||||
PROMPT_SECTIONS+="${fg_red}✘ $PROMPT_EXIT_CODE"
|
||||
fi
|
||||
((PROMPT_EXIT_CODE == 0)) && return
|
||||
|
||||
if ((PROMPT_EXIT_CODE > 128 && PROMPT_EXIT_CODE < 160)); then
|
||||
print "${fg_white}$(kill -l $PROMPT_EXIT_CODE)"
|
||||
else
|
||||
print "${fg_red}✘ $PROMPT_EXIT_CODE"
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -55,12 +54,12 @@ render_user_host() {
|
|||
|
||||
(($#parts)) && {
|
||||
local separator="${fg_gray}@"
|
||||
PROMPT_SECTIONS+="${(pj:$separator:)parts}"
|
||||
print "${(pj:$separator:)parts}"
|
||||
}
|
||||
}
|
||||
|
||||
render_pwd() {
|
||||
PROMPT_SECTIONS+="${fg_cyan}%~${fg_reset}"
|
||||
print "${fg_cyan}%~${fg_reset}"
|
||||
}
|
||||
|
||||
render_git() {
|
||||
|
@ -144,27 +143,27 @@ render_git() {
|
|||
(($untracked > 0 )) && gitinfo+="${fg_white}${untracked}?"
|
||||
(($#state_parts)) && gitinfo+="${(pj:$separator:)state_parts}"
|
||||
|
||||
PROMPT_SECTIONS+="${(j: :)gitinfo}"
|
||||
print "${(j: :)gitinfo}"
|
||||
}
|
||||
|
||||
render_venv() {
|
||||
[[ -n "$VIRTUAL_ENV" ]] && PROMPT_SECTIONS+="${fg_green}${VIRTUAL_ENV:t}"
|
||||
[[ -n "$VIRTUAL_ENV" ]] && print "${fg_green}${VIRTUAL_ENV:t}"
|
||||
}
|
||||
|
||||
render_jobs() {
|
||||
(($PROMPT_JOB_COUNT > 0)) && PROMPT_SECTIONS+="${fg_magenta}%j bg"
|
||||
(($PROMPT_JOB_COUNT > 0)) && print "${fg_magenta}%j bg"
|
||||
}
|
||||
|
||||
render_exec_time() {
|
||||
(($PROMPT_EXEC_TIME > 3)) && { # last command execution time if over 3s
|
||||
local parts=(
|
||||
"$((PROMPT_EXEC_TIME / 60 / 60 / 24))d" # days
|
||||
"$((PROMPT_EXEC_TIME / 60 / 60 % 24))h" # hours
|
||||
"$((PROMPT_EXEC_TIME / 60 % 60))m" # minutes
|
||||
"$((PROMPT_EXEC_TIME % 60))s" # seconds
|
||||
)
|
||||
PROMPT_SECTIONS+=${fg_gray}${parts:#0*} # only keep non-zero parts
|
||||
}
|
||||
(($PROMPT_EXEC_TIME <= 3)) && return # don't print time if under 3s
|
||||
|
||||
local parts=(
|
||||
"$((PROMPT_EXEC_TIME / 60 / 60 / 24))d" # days
|
||||
"$((PROMPT_EXEC_TIME / 60 / 60 % 24))h" # hours
|
||||
"$((PROMPT_EXEC_TIME / 60 % 60))m" # minutes
|
||||
"$((PROMPT_EXEC_TIME % 60))s" # seconds
|
||||
)
|
||||
print ${fg_gray}${parts:#0*} # only keep non-zero parts
|
||||
}
|
||||
|
||||
# Hook triggered when a command is about to be executed.
|
||||
|
|
Loading…
Add table
Reference in a new issue