dot-files/bash/.bash_prompt

54 lines
1.7 KiB
Bash

# get current status of git repo
function __prompt_parse_git_dirty() {
git_status=$(git status 2>&1 | tee)
dirty=$(echo -n "${git_status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?")
untracked=$(echo -n "${git_status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?")
ahead=$(echo -n "${git_status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?")
newfile=$(echo -n "${git_status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?")
renamed=$(echo -n "${git_status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?")
deleted=$(echo -n "${git_status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?")
bits=''
if [ ${renamed} -eq 0 ]; then
bits=">${bits}"
fi
if [ ${ahead} -eq 0 ]; then
bits="*${bits}"
fi
if [ ${newfile} -eq 0 ]; then
bits="+${bits}"
fi
if [ ${untracked} -eq 0 ]; then
bits="?${bits}"
fi
if [ ${deleted} -eq 0 ]; then
bits="x${bits}"
fi
if [ ${dirty} -eq 0 ]; then
bits="!${bits}"
fi
if [ -z "$bits" ]; then
echo ""
else
echo " ${bits}"
fi
}
# get current branch in git repo
function __prompt_parse_git_branch() {
git_branch=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ -z "${git_branch}" ]; then
echo ""
else
git_stat=$(__prompt_parse_git_dirty)
echo "[${git_branch}${git_stat}]"
fi
}
# Display return value only when non-zero
function __prompt_nonzero_return() {
RETVAL=$?
[ $RETVAL -ne 0 ] && echo "[$RETVAL]"
}
export PS1="\e[31m\`__prompt_nonzero_return\`\e[m[\[\e[32m\]\u\[\e[m\]@\[\e[34m\]\h\[\e[36m\] \w\[\e[m\]]\e[35m\`__prompt_parse_git_branch\`\e[m\n42sh$ "