agnoster: cut down on fork/execs improve performance when not in a repo (#6210)

* agnoster: improve perf with use of $jobstates (zsh/parameter module)

This saves multiple fork/execs (for the subshell and wc) each
time the prompt is rendered

* agnoster: compute git repo_path only when in a git repo

this avoids needlessly invoking git twice every time we render the prompt

* agnoster: avoid subshell when rendering prompt

Rather than forking a subshell to print the prompt, construct it
incrementally by appending to $PROMPT.

v2: fix incorrect CURRENT_BG
v3: fix bzr and hg prompting

Thanks @mcornella for the help.
This commit is contained in:
Andrew Baumann 2018-05-17 05:46:27 -07:00 committed by Marc Cornellà
parent 3dab7e46e8
commit fbcda4d5a9

46
themes/agnoster.zsh-theme Normal file → Executable file
View file

@ -29,6 +29,7 @@
# A few utility functions to make it easy and re-usable to draw segmented prompts # A few utility functions to make it easy and re-usable to draw segmented prompts
CURRENT_BG='NONE' CURRENT_BG='NONE'
zmodload zsh/parameter
# Special Powerline characters # Special Powerline characters
@ -55,23 +56,23 @@ 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='NONE'
} }
### Prompt components ### Prompt components
@ -93,9 +94,9 @@ prompt_git() {
PL_BRANCH_CHAR=$'\ue0a0' #  PL_BRANCH_CHAR=$'\ue0a0' # 
} }
local ref dirty mode repo_path local ref dirty mode repo_path
repo_path=$(git rev-parse --git-dir 2>/dev/null)
if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
repo_path=$(git rev-parse --git-dir 2>/dev/null)
dirty=$(parse_git_dirty) dirty=$(parse_git_dirty)
ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git rev-parse --short HEAD 2> /dev/null)" ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git rev-parse --short HEAD 2> /dev/null)"
if [[ -n $dirty ]]; then if [[ -n $dirty ]]; then
@ -123,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
} }
@ -135,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
@ -151,36 +152,36 @@ prompt_bzr() {
prompt_hg() { prompt_hg() {
(( $+commands[hg] )) || return (( $+commands[hg] )) || return
local rev status local rev st branch
if $(hg id >/dev/null 2>&1); then if $(hg id >/dev/null 2>&1); then
if $(hg prompt >/dev/null 2>&1); then if $(hg prompt >/dev/null 2>&1); then
if [[ $(hg prompt "{status|unknown}") = "?" ]]; then if [[ $(hg prompt "{status|unknown}") = "?" ]]; then
# if files are not added # if files are not added
prompt_segment red white prompt_segment red white
st='±' st=' ±'
elif [[ -n $(hg prompt "{status|modified}") ]]; then elif [[ -n $(hg prompt "{status|modified}") ]]; then
# if any modification # if any modification
prompt_segment yellow black prompt_segment yellow black
st='±' st=' ±'
else else
# 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')
branch=$(hg id -b 2>/dev/null) branch=$(hg id -b 2>/dev/null)
if `hg st | grep -q "^\?"`; then if `hg st | grep -q "^\?"`; then
prompt_segment red black prompt_segment red black
st='±' st=' ±'
elif `hg st | grep -q "^[MA]"`; then elif `hg st | grep -q "^[MA]"`; then
prompt_segment yellow black prompt_segment yellow black
st='±' st=' ±'
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
} }
@ -207,7 +208,7 @@ prompt_status() {
symbols=() symbols=()
[[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘" [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘"
[[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡" [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
[[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{cyan}%}⚙" [[ ${#jobstates} -ne 0 ]] && symbols+="%{%F{cyan}%}⚙"
[[ -n "$symbols" ]] && prompt_segment black default "$symbols" [[ -n "$symbols" ]] && prompt_segment black default "$symbols"
} }
@ -215,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
@ -223,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