darkrc/shell/prompt

149 lines
3.2 KiB
Text
Raw Normal View History

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
}
2020-01-08 13:45:27 +00:00
git__fetch() {
if [ -f "$1/.git" ]
then dir="$(cat $top/.git | sed -e 's/^gitdir: //')"
else dir="$1/.git"
fi
2020-01-08 13:45:27 +00:00
if [ -n "$BASH_AUTOFETCH_TIMEOUT" ]; then
timeout=$BASH_AUTOFETCH_TIMEOUT
else
timeout=60
fi
2020-05-20 08:14:36 +00:00
if [ -f "$dir/FETCH_HEAD" ]; then
diff=$(($(date +%s) - $(stat -c %Y $dir/FETCH_HEAD)))
2020-01-08 13:45:27 +00:00
else
diff=9999
fi
2020-01-09 14:49:45 +00:00
if [ "$diff" -gt "$timeout" ]
2020-01-08 13:45:27 +00:00
then run=1
else run=0
fi
2020-01-09 14:49:45 +00:00
if [ "$run" -gt 0 ]; then
2020-05-20 08:14:36 +00:00
touch $dir/FETCH_HEAD
2020-01-08 13:45:27 +00:00
nohup git fetch > /dev/null 2>&1 &
fi
echo $run
}
gitprompt () {
2021-02-18 09:10:13 +00:00
top=$(timeout 1 git rev-parse --show-toplevel 2>/dev/null)
2020-01-08 13:45:27 +00:00
if [ -n "$top" ]
then
2020-01-09 14:49:45 +00:00
if [ "$BASH_AUTOFETCH" -gt 0 ]; then
2020-01-08 13:45:27 +00:00
autofetch=$(git__fetch $top)
f=$((2-$autofetch))
else
f=3
fi
2021-02-18 09:10:13 +00:00
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)
2020-01-09 14:49:45 +00:00
stat=$(git branch -vv | grep -P '^\*' | grep -Po '\[.*\]')
ahead=$(echo $stat | grep -Po '(?<=ahead )\d*')
behind=$(echo $stat | grep -Po '(?<=behind )\d*')
2020-02-04 14:27:13 +00:00
gray='\033[00;37m'
2020-01-08 13:45:27 +00:00
blue='\033[01;34m'
yellow='\033[01;33m'
red='\033[01;31m'
green='\033[01;32m'
purple='\033[01;35m'
2020-02-04 14:27:13 +00:00
/bin/echo -ne "\033[00;3${f}mδ\033[00;33m "
/bin/echo -ne "${purple}$(basename $top) "
2020-01-08 13:45:27 +00:00
# BRANCH
2020-01-09 14:49:45 +00:00
if [ -z "$branch" ]
2020-01-08 13:45:27 +00:00
then
2020-01-09 14:49:45 +00:00
head=$(git rev-parse --short HEAD 2>&1)
2020-01-15 13:35:35 +00:00
if [ "$(echo "$head" | grep '^fatal')" ]
2020-01-08 13:45:27 +00:00
then
2021-02-18 09:10:13 +00:00
git_branch='{no commits}'
2020-01-08 13:45:27 +00:00
else
2021-02-18 09:10:13 +00:00
git_branch='#'"$head"
2020-01-08 13:45:27 +00:00
fi
fi
2021-02-18 09:10:13 +00:00
if [ "$git_branch" = 'master' ]
2020-01-08 13:45:27 +00:00
then
2020-02-04 14:27:13 +00:00
/bin/echo -ne "$blue"
2021-02-18 09:10:13 +00:00
elif [ $(echo "$git_branch" | grep '^#' ) ]
2020-01-08 13:45:27 +00:00
then
2020-02-04 14:27:13 +00:00
/bin/echo -ne "$red"
2020-01-08 13:45:27 +00:00
else
2020-02-04 14:27:13 +00:00
/bin/echo -ne "$yellow"
fi
2021-02-18 09:10:13 +00:00
/bin/echo -ne "$git_branch\033[00;36m"
2020-02-04 14:27:13 +00:00
# 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}"
2020-01-08 13:45:27 +00:00
fi
# CHANGES
2020-01-09 14:49:45 +00:00
if [ "$modif" = 0 ]
2020-01-08 13:45:27 +00:00
then
2020-01-09 14:49:45 +00:00
/bin/echo -ne # "${gray}:\033[01;36m$modif" # No modified files
2020-01-08 13:45:27 +00:00
else
2020-02-05 08:41:58 +00:00
/bin/echo -ne " \033[01;36m≠$modif" # Modified files
2020-01-08 13:45:27 +00:00
fi
2020-01-09 14:49:45 +00:00
if [ "$added" -ne 0 ]
2020-01-08 13:45:27 +00:00
then
2020-02-04 14:27:13 +00:00
/bin/echo -ne " ${purple}+$added"
2020-01-08 13:45:27 +00:00
fi
2020-01-09 14:49:45 +00:00
if [ "$deleted" -ne 0 ]
2020-01-08 13:45:27 +00:00
then
2020-01-09 14:49:45 +00:00
/bin/echo -ne " ${red}-$deleted"
2020-01-08 13:45:27 +00:00
fi
2020-01-09 14:49:45 +00:00
if [ "$renamed" -ne 0 ]
2020-01-08 13:45:27 +00:00
then
2020-02-04 14:27:13 +00:00
/bin/echo -ne " ${purple}~$renamed"
2020-01-08 13:45:27 +00:00
fi
2020-01-09 14:49:45 +00:00
if [ "$untracked" -ne 0 ]
2020-01-08 13:45:27 +00:00
then
2020-02-05 08:41:58 +00:00
/bin/echo -ne " ${yellow}+$untracked"
2020-01-08 13:45:27 +00:00
fi
2021-02-18 09:10:13 +00:00
/bin/echo -ne "\033[00m"
else
exit 1
2020-01-08 13:45:27 +00:00
fi
}