darkrc/shell/prompt

148 lines
3.2 KiB
Text

rlevel() {
if [ -n "$RANGER_LEVEL" ]
then if [ "$RANGER_LEVEL" -gt 1 ] && [ ! -z "$3" ]
then
/bin/echo -ne "$1$3$RANGER_LEVEL "
else
/bin/echo -ne "$1 "
fi
fi
if [ -n "$VIM_LEVEL" ]
then if [ "$VIM_LEVEL" -gt 1 ] && [ ! -z "$3" ]
then
/bin/echo -ne "$2$3$VIM_LEVEL "
else
/bin/echo -ne "$2 "
fi
fi
}
numjobs() {
jobs=$(jobs | wc -l)
if [ $jobs -gt 1 ] && [ ! -z "$2" ]
then echo "$1$2$jobs "
elif [ $jobs -gt 0 ]
then echo "$1 "
fi
}
git__fetch() {
if [ -f "$1/.git" ]
then dir="$(cat $top/.git | sed -e 's/^gitdir: //')"
else dir="$1/.git"
fi
if [ -n "$BASH_AUTOFETCH_TIMEOUT" ]; then
timeout=$BASH_AUTOFETCH_TIMEOUT
else
timeout=60
fi
if [ -f "$dir/FETCH_HEAD" ]; then
diff=$(($(date +%s) - $(stat -c %Y $dir/FETCH_HEAD)))
else
diff=9999
fi
if [ "$diff" -gt "$timeout" ]
then run=1
else run=0
fi
if [ "$run" -gt 0 ]; then
touch $dir/FETCH_HEAD
nohup git fetch > /dev/null 2>&1 &
fi
echo $run
}
gitprompt () {
top=$(timeout 1 git rev-parse --show-toplevel 2>/dev/null)
if [ -n "$top" ]
then
if [ "$BASH_AUTOFETCH" -gt 0 ]; then
autofetch=$(git__fetch $top)
f=$((2-$autofetch))
else
f=3
fi
git_status=$(timeout 1 git status --short 2>/dev/null)
git_branch=$(git branch | grep -Po '(?<=\* )[[:alnum:]_.-]*')
modif=$(echo "$git_status" | grep -Po '^\s*M' | wc -l)
untracked=$(echo "$git_status" | grep -Po '^\?\?' | wc -l)
added=$(echo "$git_status" | grep -Po '^\s*[A]' | wc -l)
deleted=$(echo "$git_status" | grep -Po '^\s*[D]' | wc -l)
renamed=$(echo "$git_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*')
gray='\033[00;37m'
blue='\033[01;34m'
yellow='\033[01;33m'
red='\033[01;31m'
green='\033[01;32m'
purple='\033[01;35m'
/bin/echo -ne "\033[00;3${f}mδ\033[00;33m "
/bin/echo -ne "${purple}$(basename $top) "
# BRANCH
if [ -z "$git_branch" ]
then
head=$(git rev-parse --short HEAD 2>&1)
if [ "$(echo "$head" | grep '^fatal')" ]
then
git_branch='{no commits}'
else
git_branch='#'"$head"
fi
fi
if [ "$git_branch" = 'master' ]
then
/bin/echo -ne "$blue"
elif [ $(echo "$git_branch" | grep '^#' ) ]
then
/bin/echo -ne "$red"
else
/bin/echo -ne "$yellow"
fi
/bin/echo -ne "$git_branch\033[00;36m"
# SYNC
if [ -z "$ahead" ] && [ -z "$behind" ]
then
/bin/echo -ne "" # Nothing to do here
elif [ -z "$ahead" ]
then
/bin/echo -ne " ${yellow}↓${behind}"
elif [ -z "$behind" ]
then
/bin/echo -ne " ${green}↑${ahead}"
else
/bin/echo -ne " ${red}↓${behind}${red}↑${ahead}"
fi
# CHANGES
if [ "$modif" = 0 ]
then
/bin/echo -ne # "${gray}:\033[01;36m$modif" # No modified files
else
/bin/echo -ne " \033[01;36m≠$modif" # Modified files
fi
if [ "$added" -ne 0 ]
then
/bin/echo -ne " ${purple}+$added"
fi
if [ "$deleted" -ne 0 ]
then
/bin/echo -ne " ${red}-$deleted"
fi
if [ "$renamed" -ne 0 ]
then
/bin/echo -ne " ${purple}~$renamed"
fi
if [ "$untracked" -ne 0 ]
then
/bin/echo -ne " ${yellow}+$untracked"
fi
/bin/echo -ne "\033[00m"
else
exit 1
fi
}