Skip to content

Commit

Permalink
Merge pull request olivierverdier#21 from biggerfisch/show_component_…
Browse files Browse the repository at this point in the history
…flags

Add flags to show/hide components
  • Loading branch information
biggerfisch committed May 23, 2022
2 parents 47cbeb9 + 86de1a5 commit bfadc6c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ There is now a Haskell implementation as well, which can be four to six times fa

- Define the variable `ZSH_GIT_PROMPT_SHOW_UPSTREAM=2` to show the remote as above but omit the remote branch when its name is equal to the local branch.

- To disable any status component(s), set the following control variables to `0`. All default to `1` (enabled) unless otherwise noted.
- `ZSH_GIT_PROMPT_SHOW_UPSTREAM`
- Defaults to `0` (off)
- `ZSH_GIT_PROMPT_SHOW_BEHIND`
- `ZSH_GIT_PROMPT_SHOW_AHEAD`
- `ZSH_GIT_PROMPT_SHOW_REBASE`
- `ZSH_GIT_PROMPT_SHOW_MERGING`
- `ZSH_GIT_PROMPT_SHOW_BISECT`
- `ZSH_GIT_PROMPT_SHOW_STAGED`
- `ZSH_GIT_PROMPT_SHOW_CONFLICTS`
- `ZSH_GIT_PROMPT_SHOW_CHANGED`
- `ZSH_GIT_PROMPT_SHOW_UNTRACKED`
- `ZSH_GIT_PROMPT_SHOW_STASHED`

Demo:

![upstream example](https://user-images.githubusercontent.com/470400/40869339-52ae782c-65e7-11e8-89a9-2e053b3f8198.png)
Expand Down
44 changes: 29 additions & 15 deletions zshrc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,14 @@ update_current_git_vars() {
}

repo_check() {
local content=""
local cmd="content=\"\$REPO_$1\""
eval $cmd
local repo_var="REPO_$1"
local content="${(P)repo_var}"
[ -n "$content" ] && [ "$content" != "0" ]
}

repo_check_not() {
local content=""
local cmd="content=\"\$REPO_$1\""
eval $cmd
local repo_var="REPO_$1"
local content="${(P)repo_var}"
[ -n "$content" ] && [ "$content" = "0" ]
}

Expand All @@ -111,15 +109,18 @@ add_color_reset() {
}

add_repo_var() {
eval STATUS="\$STATUS\$REPO_$1"
local repo_var="REPO_$1"
STATUS="${STATUS}${(P)repo_var}"
}

add_theme_var() {
eval STATUS="\$STATUS\$ZSH_THEME_GIT_PROMPT_$1"
local theme_var="ZSH_THEME_GIT_PROMPT_$1"
STATUS="${STATUS}${(P)theme_var}"
}

repo_info_with_check() {
if repo_check $1; then
local show_var="ZSH_GIT_PROMPT_SHOW_$1"
if [ "${(P)show_var}" -ne "0" ] && repo_check $1; then
add_theme_var $1
add_repo_var $1
add_color_reset
Expand Down Expand Up @@ -154,7 +155,7 @@ git_build_status() {
add_color_reset
fi

if repo_check BEHIND || repo_check AHEAD; then
if (repo_check BEHIND && [ "$ZSH_GIT_PROMPT_SHOW_BEHIND" -ne "0" ]) || (repo_check AHEAD && [ "$ZSH_GIT_PROMPT_SHOW_AHEAD" -ne "0" ]); then
add_str " "
fi

Expand All @@ -173,11 +174,11 @@ git_build_status() {
repo_info_with_check STASHED

local clean=0
if repo_check_not STAGED &&
repo_check_not CONFLICTS &&
repo_check_not CHANGED &&
repo_check_not UNTRACKED &&
repo_check_not STASHED; then
if ([ "$ZSH_GIT_PROMPT_SHOW_STAGED" -eq "0" ] || repo_check_not STAGED) &&
([ "$ZSH_GIT_PROMPT_SHOW_CONFLICTS" -eq "0" ] || repo_check_not CONFLICTS) &&
([ "$ZSH_GIT_PROMPT_SHOW_CHANGED" -eq "0" ] || repo_check_not CHANGED) &&
([ "$ZSH_GIT_PROMPT_SHOW_UNTRACKED" -eq "0" ] || repo_check_not UNTRACKED) &&
([ "$ZSH_GIT_PROMPT_SHOW_STASHED" -eq "0" ] || repo_check_not STASHED); then
add_theme_var CLEAN
add_color_reset
clean=1
Expand Down Expand Up @@ -252,4 +253,17 @@ ZSH_THEME_GIT_PROMPT_MERGING="%{$fg_bold[magenta]%}|MERGING%{${reset_color}%}"
ZSH_THEME_GIT_PROMPT_REBASE="%{$fg_bold[magenta]%}|REBASE%{${reset_color}%} "
ZSH_THEME_GIT_PROMPT_BISECT="%{$fg_bold[magenta]%}|BISECT%{${reset_color}%} "

# Settings defaults
ZSH_GIT_PROMPT_SHOW_UPSTREAM=0
ZSH_GIT_PROMPT_SHOW_BEHIND=1
ZSH_GIT_PROMPT_SHOW_AHEAD=1
ZSH_GIT_PROMPT_SHOW_REBASE=1
ZSH_GIT_PROMPT_SHOW_MERGING=1
ZSH_GIT_PROMPT_SHOW_BISECT=1
ZSH_GIT_PROMPT_SHOW_STAGED=1
ZSH_GIT_PROMPT_SHOW_CONFLICTS=1
ZSH_GIT_PROMPT_SHOW_CHANGED=1
ZSH_GIT_PROMPT_SHOW_UNTRACKED=1
ZSH_GIT_PROMPT_SHOW_STASHED=1

# vim: filetype=zsh: tabstop=4 shiftwidth=4 expandtab

0 comments on commit bfadc6c

Please sign in to comment.