refactoring git functions to remove dead spaces on non-git folders.

This commit is contained in:
Andrew Davidson 2020-06-23 07:31:08 -04:00
parent 14b77cb964
commit 7337e02dff
Signed by: amd
GPG key ID: 17AF8F2A49CF25C6

View file

@ -1,31 +1,30 @@
#setopt promptsubst
autoload -U colors && colors # Enable colors in prompt
# Show Git branch/tag, or name-rev if on detached head
function git_branch {
function git_prompt {
local FULL_BRANCH="$(git symbolic-ref -q HEAD 2>/dev/null || git name-rev --name-only --no-undefined --always HEAD 2>/dev/null)"
local SHORT_BRANCH="$(echo $FULL_BRANCH | sed 's/refs\/heads\///')"
echo "$SHORT_BRANCH"
}
PBRANCH="$(echo $FULL_BRANCH | sed 's/refs\/heads\///')"
if [ -n $PBRANCH ]; then
echo -n " ${PBRANCH}"
# Are there unstaged changes in the working directory
local CHANGED=$(git status --porcelain --ignore-submodule -unormal 2>/dev/null | wc -l)
if [ $CHANGED -gt 0 ]; then
PCHANGED="◊"
fi
# Are we behind the origin?
local NUM_BEHIND="$(git log --oneline ..@{u} 2>/dev/null | wc -l | tr -d ' ' )"
if [ "$NUM_BEHIND" -gt 0 ]; then
PBEHIND="↓"
fi
# Are we ahead of the origin?
local NUM_AHEAD="$(git log --oneline @{u}.. 2>/dev/null | wc -l | tr -d ' ')"
if [ "$NUM_AHEAD" -gt 0 ]; then
PAHEAD="↑"
fi
function git_is_ahead {
local NUM_AHEAD="$(git log --oneline @{u}.. 2>/dev/null | wc -l | tr -d ' ')"
if [ "$NUM_AHEAD" -gt 0 ]; then
echo "↑"
fi
}
function git_is_behind {
local NUM_BEHIND="$(git log --oneline ..@{u} 2>/dev/null | wc -l | tr -d ' ' )"
if [ "$NUM_BEHIND" -gt 0 ]; then
echo "↓"
fi
}
function git_unstaged {
local CHANGED=$(git status --porcelain --ignore-submodule -unormal 2>/dev/null | wc -l)
if [ $CHANGED -gt 0 ]; then
echo -e "◊\c"
if [[ -n $PCHANGED || -n $PAHEAD || -n $PBEHIND ]]; then
echo -n " ${PCHANGED}${PBEHIND}${PAHEAD} "
fi
fi
}
@ -62,5 +61,5 @@ function check_reboot {
test -f /var/run/reboot-required && echo "R "
}
PROMPT='${PR_BOLD_RED}$(check_reboot)${reset_color}%m ${PR_BOLD_YELLOW}$(truncated_pwd 3)$(git_branch)%{$reset_color%}$(git_unstaged)$(git_is_ahead)$(git_is_behind) %(?.%F{magenta}.%F{red})%f '
PROMPT='${PR_BOLD_RED}$(check_reboot)${reset_color}%m ${PR_BOLD_YELLOW}$(truncated_pwd 3)$(git_prompt)%(?.%F{magenta}.%F{red})%f '
#PROMPT='$(check_reboot)%m $(truncated_pwd 3) $(git_branch) $(git_unstaged)$(git_is_ahead)$(git_is_behind)> '