Browse Source

perf(bureau): remove multiple grep calls in git status check

Marc Cornellà 2 years ago
parent
commit
74a3db75e4
1 changed files with 19 additions and 17 deletions
  1. 19 17
      themes/bureau.zsh-theme

+ 19 - 17
themes/bureau.zsh-theme

@@ -16,7 +16,7 @@ ZSH_THEME_GIT_PROMPT_STAGED="%{$fg_bold[green]%}●%{$reset_color%}"
 ZSH_THEME_GIT_PROMPT_UNSTAGED="%{$fg_bold[yellow]%}●%{$reset_color%}"
 ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg_bold[red]%}●%{$reset_color%}"
 
-bureau_git_branch () {
+bureau_git_info () {
   local ref
   ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
   ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
@@ -30,16 +30,16 @@ bureau_git_status() {
   # check status of files
   local gitfiles="$(tail -n +2 <<< "$gitstatus")"
   if [[ -n "$gitfiles" ]]; then
-    if $(echo "$gitfiles" | command grep -q '^[AMRD]. '); then
+    if [[ "$gitfiles" =~ $'(^|\n)[AMRD]. ' ]]; then
       result+="$ZSH_THEME_GIT_PROMPT_STAGED"
     fi
-    if $(echo "$gitfiles" | command grep -q '^.[MTD] '); then
+    if [[ "$gitfiles" =~ $'(^|\n).[MTD] ' ]]; then
       result+="$ZSH_THEME_GIT_PROMPT_UNSTAGED"
     fi
-    if $(echo "$gitfiles" | command grep -q -E '^\?\? '); then
+    if [[ "$gitfiles" =~ $'(^|\n)\\?\\? ' ]]; then
       result+="$ZSH_THEME_GIT_PROMPT_UNTRACKED"
     fi
-    if $(echo "$gitfiles" | command grep -q '^UU '); then
+    if [[ "$gitfiles" =~ $'(^|\n)UU ' ]]; then
       result+="$ZSH_THEME_GIT_PROMPT_UNMERGED"
     fi
   else
@@ -48,17 +48,18 @@ bureau_git_status() {
 
   # check status of local repository
   local gitbranch="$(head -n 1 <<< "$gitstatus")"
-  if $(echo "$gitbranch" | command grep -q '^## .*ahead'); then
+  if [[ "$gitbranch" =~ '^## .*ahead' ]]; then
     result+="$ZSH_THEME_GIT_PROMPT_AHEAD"
   fi
-  if $(echo "$gitbranch" | command grep -q '^## .*behind'); then
+  if [[ "$gitbranch" =~ '^## .*behind' ]]; then
     result+="$ZSH_THEME_GIT_PROMPT_BEHIND"
   fi
-  if $(echo "$gitbranch" | command grep -q '^## .*diverged'); then
+  if [[ "$gitbranch" =~ '^## .*diverged' ]]; then
     result+="$ZSH_THEME_GIT_PROMPT_DIVERGED"
   fi
 
-  if $(command git rev-parse --verify refs/stash &> /dev/null); then
+  # check if there are stashed changes
+  if command git rev-parse --verify refs/stash &> /dev/null; then
     result+="$ZSH_THEME_GIT_PROMPT_STASHED"
   fi
 
@@ -66,21 +67,22 @@ bureau_git_status() {
 }
 
 bureau_git_prompt() {
-  local gitbranch=$(bureau_git_branch)
-  local gitstatus=$(bureau_git_status)
-  local info
-
-  if [[ -z "$gitbranch" ]]; then
+  # check git information
+  local gitinfo=$(bureau_git_info)
+  if [[ -z "$gitinfo" ]]; then
     return
   fi
 
-  info="${gitbranch:gs/%/%%}"
+  # quote % in git information
+  local output="${gitinfo:gs/%/%%}"
 
+  # check git status
+  local gitstatus=$(bureau_git_status)
   if [[ -n "$gitstatus" ]]; then
-    info+=" $gitstatus"
+    output+=" $gitstatus"
   fi
 
-  echo "${ZSH_THEME_GIT_PROMPT_PREFIX}${info}${ZSH_THEME_GIT_PROMPT_SUFFIX}"
+  echo "${ZSH_THEME_GIT_PROMPT_PREFIX}${output}${ZSH_THEME_GIT_PROMPT_SUFFIX}"
 }