Skip to content

Instantly share code, notes, and snippets.

@afresh1
Last active October 13, 2021 11:03
Show Gist options
  • Save afresh1/5249697 to your computer and use it in GitHub Desktop.
Save afresh1/5249697 to your computer and use it in GitHub Desktop.
My git branch prompt, colorizes the branch based on status. Might need some work on the colors as yet. Apart from the definition, works in bash or ksh.
function _git_prompt() {
local _branch=`git branch --no-color 2> /dev/null | sed -ne 's/^* //p'`
[[ -z $_branch ]] && return
local _bgcolor
local _color
for s in `git status --porcelain | tr ' ' '.' | cut -c 1-2`; do
if [ -z "$_color" ]; then
case "$s" in
?D) _color='\e[35m' ;;
?M) _color='\e[36m' ;;
\?\?) _color='\e[33m' ;;
esac
fi
if [ -z "$_bgcolor" ]; then
case $s in
A?) _bgcolor='\e[43m' ;;
D?) _bgcolor='\e[41m' ;;
M?) _bgcolor='\e[44m' ;;
esac
fi
done
# Highligh it we haven't done a git pull today
local now=`date +%s`
local last_pull=0
local fetch_head=$( git rev-parse --show-toplevel )/.git/FETCH_HEAD
[ -e $fetch_head ] && last_pull=`stat -c %Y $fetch_head`
age=$(( $now - $last_pull ))
if [ $age -gt 86400 ]; then
_color='\x1b[5m'$_color
fi
_color=${_color:-'\e[32m'}
#_bgcolor=${_bgcolor:-'\e[42m'}
echo -en "\[\e[0m\]{\[$_bgcolor$_color\]${_branch}\[\e[0m\]}"
}
prompt_cmd() {
PS1="\n[$(date +%H:%M)]-[\u@\h: \w] $(_git_prompt) \$ "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment