Skip to content

Instantly share code, notes, and snippets.

@randy3k
Last active April 5, 2017 03:21
Show Gist options
  • Save randy3k/1ff38257f4aaff87f3c7 to your computer and use it in GitHub Desktop.
Save randy3k/1ff38257f4aaff87f3c7 to your computer and use it in GitHub Desktop.
git aware prompt
function git-branch-name {
echo `git symbolic-ref HEAD --short 2> /dev/null || (git branch | sed -n 's/\* (*\([^)]*\))*/\1/p')`
}
function git-dirty {
[[ `wc -l <<< "$1" ` -eq 1 ]] || echo "*"
}
function git-unpushed {
if [[ "$1" =~ ("behind "([[:digit:]]*)) ]]
then
echo -n "-${BASH_REMATCH[2]}"
fi
if [[ "$1" =~ ("ahead "([[:digit:]]*)) ]]
then
echo -n "+${BASH_REMATCH[2]}"
fi
}
function gitcolor {
st=$(git status -b --porcelain 2>/dev/null)
[[ $? -eq 0 ]] || return
if [[ $(git-dirty "$st") == "*" ]];
then
echo -e "\033[31m"
elif [[ $(git-unpushed "$st") != "" ]];
then
echo -e "\033[33m"
else
echo -e "\033[32m"
fi
}
function gitify {
st=$(git status -b --porcelain 2>/dev/null)
[[ $? -eq 0 ]] || return
dirty=$(git-dirty "$st")
unpushed=$(git-unpushed "$st")
echo -e " ($(git-branch-name)$dirty$unpushed)"
}
PS1="\[\033[33m\](\h)\[\033[00m\]-\W\[\$(gitcolor)\]\$(gitify)\[\033[00m\]\$ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment