fbcda4d5a92e023388259652652d154558e49ef3

Author
Andrew Baumann <0xabu@users.noreply.github.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

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.

Diff

This diff is truncated to protect this page.

  1diff --git a/themes/agnoster.zsh-theme b/themes/agnoster.zsh-theme
  2old mode 100644
  3new mode 100755
  4index 07546fd34d437a4b04f89939d2a536a7b5451f7e..292551f4405e4b7f713c8c4dc0566b12b0d6a89a
  5--- a/themes/agnoster.zsh-theme
  6+++ b/themes/agnoster.zsh-theme
  7@@ -29,6 +29,7 @@
  8 # A few utility functions to make it easy and re-usable to draw segmented prompts
  9 
 10 CURRENT_BG='NONE'
 11+zmodload zsh/parameter
 12 
 13 # Special Powerline characters
 14 
 15@@ -55,23 +56,23 @@ prompt_segment() {
 16   [[ -n $1 ]] && bg="%K{$1}" || bg="%k"
 17   [[ -n $2 ]] && fg="%F{$2}" || fg="%f"
 18   if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]]; then
 19-    echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
 20+    PROMPT+=" %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} "
 21   else
 22-    echo -n "%{$bg%}%{$fg%} "
 23+    PROMPT+="%{$bg%}%{$fg%} "
 24   fi
 25   CURRENT_BG=$1
 26-  [[ -n $3 ]] && echo -n $3
 27+  [[ -n $3 ]] && PROMPT+=$3
 28 }
 29 
 30 # End the prompt, closing any open segments
 31 prompt_end() {
 32   if [[ -n $CURRENT_BG ]]; then
 33-    echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
 34+    PROMPT+=" %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
 35   else
 36-    echo -n "%{%k%}"
 37+    PROMPT+="%{%k%}"
 38   fi
 39-  echo -n "%{%f%}"
 40-  CURRENT_BG=''
 41+  PROMPT+="%{%f%}"
 42+  CURRENT_BG='NONE'
 43 }
 44 
 45 ### Prompt components
 46@@ -93,9 +94,9 @@ prompt_git() {
 47     PL_BRANCH_CHAR=$'\ue0a0'         # 
 48   }
 49   local ref dirty mode repo_path
 50-  repo_path=$(git rev-parse --git-dir 2>/dev/null)
 51 
 52   if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
 53+    repo_path=$(git rev-parse --git-dir 2>/dev/null)
 54     dirty=$(parse_git_dirty)
 55     ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git rev-parse --short HEAD 2> /dev/null)"
 56     if [[ -n $dirty ]]; then
 57@@ -123,7 +124,7 @@ prompt_git() {
 58     zstyle ':vcs_info:*' formats ' %u%c'
 59     zstyle ':vcs_info:*' actionformats ' %u%c'
 60     vcs_info
 61-    echo -n "${ref/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
 62+    PROMPT+="${ref/refs\/heads\//$PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode}"
 63   fi
 64 }
 65 
 66@@ -135,15 +136,15 @@ prompt_bzr() {
 67         revision=`bzr log | head -n2 | tail -n1 | sed 's/^revno: //'`
 68         if [[ $status_mod -gt 0 ]] ; then
 69             prompt_segment yellow black
 70-            echo -n "bzr@"$revision "✚ "
 71+            PROMPT+="bzr@$revision ✚ "
 72         else
 73             if [[ $status_all -gt 0 ]] ; then
 74                 prompt_segment yellow black
 75-                echo -n "bzr@"$revision
 76+                PROMPT+="bzr@$revision"
 77 
 78             else
 79                 prompt_segment green black
 80-                echo -n "bzr@"$revision
 81+                PROMPT+="bzr@$revision"
 82             fi
 83         fi
 84     fi
 85@@ -151,36 +152,36 @@ prompt_bzr() {
 86 
 87 prompt_hg() {
 88   (( $+commands[hg] )) || return
 89-  local rev status
 90+  local rev st branch
 91   if $(hg id >/dev/null 2>&1); then
 92     if $(hg prompt >/dev/null 2>&1); then
 93       if [[ $(hg prompt "{status|unknown}") = "?" ]]; then
 94         # if files are not added
 95         prompt_segment red white
 96-        st='±'
 97+        st=' ±'
 98       elif [[ -n $(hg prompt "{status|modified}") ]]; then
 99         # if any modification
100         prompt_segment yellow black
101-        st='±'
102+        st=' ±'
103       else
104         # if working copy is clean
105         prompt_segment green black
106       fi