darkrc/shell/prompt

152 lines
3.3 KiB
Text
Raw Permalink 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
2021-10-05 12:15:38 +00:00
if [ -n "$SHELL_AUTOFETCH_TIMEOUT" ]; then
timeout=$SHELL_AUTOFETCH_TIMEOUT
2020-01-08 13:45:27 +00:00
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
2021-10-05 12:16:54 +00:00
if [ "$SHELL_AUTOFETCH_TIMEOUT" -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
if [ -z "$git_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"
elif [ "$git_branch" = 'main' ]
then
/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
}