Skip to content

Instantly share code, notes, and snippets.

@Mrono
Created January 31, 2017 20:35
Show Gist options
  • Save Mrono/ee48e56cc4ba9cf3311433bf845ccd59 to your computer and use it in GitHub Desktop.
Save Mrono/ee48e56cc4ba9cf3311433bf845ccd59 to your computer and use it in GitHub Desktop.
#Include this file in your .bashrc with
#source ~/.git-prompt
GREEN="\[\e[0;32m\]"
BLUE="\[\e[0;34m\]"
RED="\[\e[0;31m\]"
BRED="\e[1;31m\]"
YELLOW="\[\e[0;33m\]"
WHITE="\e[0;37m\]"
BWHITE="\e[1;37m\]"
COLOREND="\[\e[00m\]"
parse_git_branch() {
branch=`__git_ps1 "%s"`
if [[ `tput cols` -lt 110 ]]; then
branch=`echo $branch | sed s/feature/f/1`
branch=`echo $branch | sed s/hotfix/h/1`
branch=`echo $branch | sed s/release/\r/1`
branch=`echo $branch | sed s/master/mstr/1`
branch=`echo $branch | sed s/develop/dev/1`
fi
if [[ $branch != "" ]]; then
if [[ $(git status 2> /dev/null | tail -n1) == "nothing to commit, working directory clean" ]]; then
echo "(${GREEN}$branch${COLOREND}) "
else
echo "(${RED}$branch${COLOREND}) "
fi
fi
}
working_directory() {
dir=`pwd`
in_home=0
if [[ `pwd` =~ ^"$HOME"(/|$) ]]; then
dir="~${dir#$HOME}"
in_home=1
fi
workingdir=""
if [[ `tput cols` -lt 110 ]]; then
first="/`echo $dir | cut -d / -f 2`"
letter=${first:0:2}
if [[ $in_home == 1 ]]; then
letter="~$letter"
fi
proj=`echo $dir | cut -d / -f 3`
beginning="$letter/$proj"
end=`echo "$dir" | rev | cut -d / -f1 | rev`
if [[ $proj == "" ]]; then
workingdir="$dir"
elif [[ $proj == "~" ]]; then
workingdir="$dir"
elif [[ $dir =~ "$first/$proj"$ ]]; then
workingdir="$beginning"
elif [[ $dir =~ "$first/$proj/$end"$ ]]; then
workingdir="$beginning/$end"
else
workingdir="$beginning/…/$end"
fi
else
workingdir="$dir"
fi
echo -e "${YELLOW}$workingdir${COLOREND} "
}
parse_remote_state() {
remote_state=$(git status -sb 2> /dev/null | grep -oh "\[.*\]")
if [[ "$remote_state" != "" ]]; then
out="${BLUE}[${COLOREND}"
if [[ "$remote_state" == *ahead* ]] && [[ "$remote_state" == *behind* ]]; then
behind_num=$(echo "$remote_state" | grep -ohP "behind \d*" | grep -ohP "\d*$")
ahead_num=$(echo "$remote_state" | grep -ohP "ahead \d*" | grep -ohP "\d*$")
out="$out${RED}$behind_num${COLOREND},${GREEN}$ahead_num${COLOREND}"
elif [[ "$remote_state" == *ahead* ]]; then
ahead_num=$(echo "$remote_state" | grep -ohP "ahead \d*" | grep -ohP "\d*$")
out="$out${GREEN}$ahead_num${COLOREND}"
elif [[ "$remote_state" == *behind* ]]; then
behind_num=$(echo "$remote_state" | grep -ohP "behind \d*" | grep -ohP "\d*$")
out="$out${RED}$behind_num${COLOREND}"
fi
out="$out${BLUE}]${COLOREND}"
echo "$out "
fi
}
prompt() {
if [[ $? -eq 0 ]]; then
exit_status="${BLUE}▸${COLOREND} "
else
exit_status="${RED}▸${COLOREND} "
fi
PS1="$(working_directory)$(parse_git_branch)$(parse_remote_state)$exit_status"
}
PROMPT_COMMAND=prompt
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment