agnoster: avoid subshell when rendering prompt

Rather than forking a subshell to print the prompt, construct it
incrementally by appending to $PROMPT.
This commit is contained in:
Andrew Baumann 2017-07-28 11:08:23 -07:00
parent 036364c299
commit 3e4bcd1730

View File

@ -56,22 +56,22 @@ prompt_segment() {
[[ -n $1 ]] && bg="%K{$1}" || bg="%k" [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
[[ -n $2 ]] && fg="%F{$2}" || fg="%f" [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} " PROMPT+=" %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
else else
echo -n "%{$bg%}%{$fg%} " PROMPT+="%{$bg%}%{$fg%} "
fi fi
CURRENT_BG=$1 CURRENT_BG=$1
[[ -n $3 ]] && echo -n $3 [[ -n $3 ]] && PROMPT+=$3
} }
# End the prompt, closing any open segments # End the prompt, closing any open segments
prompt_end() { prompt_end() {
if [[ -n $CURRENT_BG ]]; then if [[ -n $CURRENT_BG ]]; then
echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR" PROMPT+=" %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
else else
echo -n "%{%k%}" PROMPT+="%{%k%}"
fi fi
echo -n "%{%f%}" PROMPT+="%{%f%}"
CURRENT_BG='' CURRENT_BG=''
} }
@ -124,7 +124,7 @@ prompt_git() {
zstyle ':vcs_info:*' formats ' %u%c' zstyle ':vcs_info:*' formats ' %u%c'
zstyle ':vcs_info:*' actionformats ' %u%c' zstyle ':vcs_info:*' actionformats ' %u%c'
vcs_info vcs_info
echo -n "${ref/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}" PROMPT+="${ref/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
fi fi
} }
@ -136,15 +136,15 @@ prompt_bzr() {
revision=`bzr log | head -n2 | tail -n1 | sed 's/^revno: //'` revision=`bzr log | head -n2 | tail -n1 | sed 's/^revno: //'`
if [[ $status_mod -gt 0 ]] ; then if [[ $status_mod -gt 0 ]] ; then
prompt_segment yellow black prompt_segment yellow black
echo -n "bzr@"$revision "✚ " PROMPT+="bzr@"$revision "✚ "
else else
if [[ $status_all -gt 0 ]] ; then if [[ $status_all -gt 0 ]] ; then
prompt_segment yellow black prompt_segment yellow black
echo -n "bzr@"$revision PROMPT+="bzr@"$revision
else else
prompt_segment green black prompt_segment green black
echo -n "bzr@"$revision PROMPT+="bzr@"$revision
fi fi
fi fi
fi fi
@ -167,7 +167,7 @@ prompt_hg() {
# if working copy is clean # if working copy is clean
prompt_segment green black prompt_segment green black
fi fi
echo -n $(hg prompt "☿ {rev}@{branch}") $st PROMPT+=$(hg prompt "☿ {rev}@{branch}") $st
else else
st="" st=""
rev=$(hg id -n 2>/dev/null | sed 's/[^-0-9]//g') rev=$(hg id -n 2>/dev/null | sed 's/[^-0-9]//g')
@ -181,7 +181,7 @@ prompt_hg() {
else else
prompt_segment green black prompt_segment green black
fi fi
echo -n "☿ $rev@$branch" $st PROMPT+="☿ $rev@$branch" $st
fi fi
fi fi
} }
@ -216,6 +216,7 @@ prompt_status() {
## Main prompt ## Main prompt
build_prompt() { build_prompt() {
RETVAL=$? RETVAL=$?
PROMPT='%{%f%b%k%}'
prompt_status prompt_status
prompt_virtualenv prompt_virtualenv
prompt_context prompt_context
@ -224,6 +225,8 @@ build_prompt() {
prompt_bzr prompt_bzr
prompt_hg prompt_hg
prompt_end prompt_end
PROMPT+=' '
} }
PROMPT='%{%f%b%k%}$(build_prompt) ' autoload -U add-zsh-hook
add-zsh-hook precmd build_prompt