Restructure bash git prompt

This commit is contained in:
Talia 2018-11-26 15:14:41 +01:00
parent 4128670de2
commit 1a469f9e35

49
bashrc
View file

@ -29,11 +29,13 @@ git__prompt () {
git rev-parse --show-toplevel > /dev/null 2>&1
if [ $? = 0 ]
then
status=`git status --short 2>/dev/null`
branch=`git branch | grep -Po '(?<=\* )[[:alnum:]]*'`
modif=`git status --short 2>/dev/null | grep -Po '^\s*M' | wc -l`
untracked=`git status --short 2>/dev/null | grep -Po '^\?\?' | wc -l`
added=`git status --short 2>/dev/null | grep -Po '^\s*A' | wc -l`
deleted=`git status --short 2>/dev/null | grep -Po '^\s*D' | wc -l`
modif=`echo "$status" | grep -Po '^\s*M' | wc -l`
untracked=`echo "$status" | grep -Po '^\?\?' | wc -l`
added=`echo "$status" | grep -Po '^\s*[A]' | wc -l`
deleted=`echo "$status" | grep -Po '^\s*[D]' | wc -l`
renamed=`echo "$status" | grep -Eo '^\s*R' | wc -l`
stat=`git branch -vv | grep -P '^\*' | grep -Po '\[.*\]'`
ahead=`echo $stat | grep -Po '(?<=ahead )\d*'`
behind=`echo $stat | grep -Po '(?<=behind )\d*'`
@ -43,6 +45,21 @@ git__prompt () {
red='\033[01;31m'
green='\033[01;32m'
# SYNC
if [ -z $ahead ] && [ -z $behind ]
then
echo -ne "" # Nothing to do here
elif [ -z $ahead ]
then
echo -ne " ${yellow}${behind}"
elif [ -z $behind ]
then
echo -ne " ${green}${ahead}"
else
echo -ne " ${red}${behind}${yellow}${ahead}"
fi
# BRANCH
if [ -z $branch ]
then
branch='#'`git rev-parse --short HEAD`
@ -57,11 +74,12 @@ git__prompt () {
echo -ne " $yellow$branch"
fi
# CHANGES
if [ $modif = 0 ]
then
echo -ne # "$gray:\033[01;36m$modif" # No modified files
echo -ne # "${gray}:\033[01;36m$modif" # No modified files
else
echo -ne "$gray:\033[01;33m$modif" # Modified files
echo -ne "${gray}:\033[01;33m$modif" # Modified files
fi
if [ $added -ne 0 ]
then
@ -71,22 +89,13 @@ git__prompt () {
then
echo -ne " ${red}-$deleted"
fi
if [ $renamed -ne 0 ]
then
echo -ne " ${yellow}$renamed$renamed"
fi
if [ $untracked -ne 0 ]
then
echo -ne "${red}*"
fi
if [ -z $ahead ] && [ -z $behind ]
then
echo -ne "" # Nothing to do here
elif [ -z $ahead ]
then
echo -ne "${gray}:${yellow}${behind}"
elif [ -z $behind ]
then
echo -ne "${gray}:${green}${ahead}"
else
echo -ne "${gray}:${red}${behind}${yellow}${ahead}"
echo -ne " ${red}+$untracked"
fi
fi
}