74a3db75e46067ebc627a22b12aac523a9606b92

Author
Marc Cornellà <hello@mcornella.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

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

Diff

 1diff --git a/themes/bureau.zsh-theme b/themes/bureau.zsh-theme
 2index 3ca231f6f78484b6ee11d81601045b31c43084e4..a5a8a2b2028339529929a85c93ad869974d0d498 100644
 3--- a/themes/bureau.zsh-theme
 4+++ b/themes/bureau.zsh-theme
 5@@ -16,7 +16,7 @@ ZSH_THEME_GIT_PROMPT_STAGED="%{$fg_bold[green]%}●%{$reset_color%}"
 6 ZSH_THEME_GIT_PROMPT_UNSTAGED="%{$fg_bold[yellow]%}●%{$reset_color%}"
 7 ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg_bold[red]%}●%{$reset_color%}"
 8 
 9-bureau_git_branch () {
10+bureau_git_info () {
11   local ref
12   ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
13   ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
14@@ -30,16 +30,16 @@ bureau_git_status() {
15   # check status of files
16   local gitfiles="$(tail -n +2 <<< "$gitstatus")"
17   if [[ -n "$gitfiles" ]]; then
18-    if $(echo "$gitfiles" | command grep -q '^[AMRD]. '); then
19+    if [[ "$gitfiles" =~ $'(^|\n)[AMRD]. ' ]]; then
20       result+="$ZSH_THEME_GIT_PROMPT_STAGED"
21     fi
22-    if $(echo "$gitfiles" | command grep -q '^.[MTD] '); then
23+    if [[ "$gitfiles" =~ $'(^|\n).[MTD] ' ]]; then
24       result+="$ZSH_THEME_GIT_PROMPT_UNSTAGED"
25     fi
26-    if $(echo "$gitfiles" | command grep -q -E '^\?\? '); then
27+    if [[ "$gitfiles" =~ $'(^|\n)\\?\\? ' ]]; then
28       result+="$ZSH_THEME_GIT_PROMPT_UNTRACKED"
29     fi
30-    if $(echo "$gitfiles" | command grep -q '^UU '); then
31+    if [[ "$gitfiles" =~ $'(^|\n)UU ' ]]; then
32       result+="$ZSH_THEME_GIT_PROMPT_UNMERGED"
33     fi
34   else
35@@ -48,17 +48,18 @@ bureau_git_status() {
36 
37   # check status of local repository
38   local gitbranch="$(head -n 1 <<< "$gitstatus")"
39-  if $(echo "$gitbranch" | command grep -q '^## .*ahead'); then
40+  if [[ "$gitbranch" =~ '^## .*ahead' ]]; then
41     result+="$ZSH_THEME_GIT_PROMPT_AHEAD"
42   fi
43-  if $(echo "$gitbranch" | command grep -q '^## .*behind'); then
44+  if [[ "$gitbranch" =~ '^## .*behind' ]]; then
45     result+="$ZSH_THEME_GIT_PROMPT_BEHIND"
46   fi
47-  if $(echo "$gitbranch" | command grep -q '^## .*diverged'); then
48+  if [[ "$gitbranch" =~ '^## .*diverged' ]]; then
49     result+="$ZSH_THEME_GIT_PROMPT_DIVERGED"
50   fi
51 
52-  if $(command git rev-parse --verify refs/stash &> /dev/null); then
53+  # check if there are stashed changes
54+  if command git rev-parse --verify refs/stash &> /dev/null; then
55     result+="$ZSH_THEME_GIT_PROMPT_STASHED"
56   fi
57 
58@@ -66,21 +67,22 @@ bureau_git_status() {
59 }
60 
61 bureau_git_prompt() {
62-  local gitbranch=$(bureau_git_branch)
63-  local gitstatus=$(bureau_git_status)
64-  local info
65-
66-  if [[ -z "$gitbranch" ]]; then
67+  # check git information
68+  local gitinfo=$(bureau_git_info)
69+  if [[ -z "$gitinfo" ]]; then
70     return
71   fi
72 
73-  info="${gitbranch:gs/%/%%}"
74+  # quote % in git information
75+  local output="${gitinfo:gs/%/%%}"
76 
77+  # check git status
78+  local gitstatus=$(bureau_git_status)
79   if [[ -n "$gitstatus" ]]; then
80-    info+=" $gitstatus"
81+    output+=" $gitstatus"
82   fi
83 
84-  echo "${ZSH_THEME_GIT_PROMPT_PREFIX}${info}${ZSH_THEME_GIT_PROMPT_SUFFIX}"
85+  echo "${ZSH_THEME_GIT_PROMPT_PREFIX}${output}${ZSH_THEME_GIT_PROMPT_SUFFIX}"
86 }
87 
88