From 74a3db75e46067ebc627a22b12aac523a9606b92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 7 Feb 2022 17:44:05 +0100 Subject: [PATCH] perf(bureau): remove multiple grep calls in git status check --- themes/bureau.zsh-theme | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/themes/bureau.zsh-theme b/themes/bureau.zsh-theme index 3ca231f6f..a5a8a2b20 100644 --- a/themes/bureau.zsh-theme +++ b/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}" }