-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.sh
executable file
·54 lines (48 loc) · 1.88 KB
/
prompt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env bash
# The bash version is deprecated, and is not compatible with the current system
SHELL_PS1="\e[1;30;43m$([ -n "$IN_NIX_SHELL" ] && if [ -n "$name" ]; then echo " $name "; else echo ' nix-shell '; fi)\e[m"
USER_PS1="\e[1;30;41m $USER \e[m"
HOST_PS1="\e[1;30;45m $HOSTNAME \e[m"
PATH_PS1="\e[1;30;42m $(pwd | sed "s#$HOME#~#g") \e[m"
if command -v git &> /dev/null && [ -n "$(git rev-parse --is-inside-work-tree 2> /dev/null)" ]
then
STATUS=$(git status --porcelain)
ORIGIN=$(git remote | head -n 1)
TAG=$(git describe --tag --exact-match 2>/dev/null)
BRANCH=$(git branch --show-current)
[ -z $BRANCH ] && BRANCH=$(git rev-parse HEAD | head -c 6)
GIT_PS1="\e[1;30;44m $BRANCH "
[ -n $TAG ] && GIT_PS1="$GIT_PS1$TAG";
if [ "$STATUS" != "" ]
then
STAGED="$(echo "$STATUS" | grep '^[^ ?]' | wc -l)"
[ "$STAGED" != 0 ] && GIT_PS1="$GIT_PS1$STAGED× "
CHANGES="$(echo "$STATUS" | grep '^.M' | wc -l)"
[ "$CHANGES" != 0 ] && GIT_PS1="$GIT_PS1$CHANGES¤ "
UNTRACKED="$(echo "$STATUS" | grep '^.?' | wc -l)"
[ "$UNTRACKED" != 0 ] && GIT_PS1="$GIT_PS1$UNTRACKED+ "
DELETED="$(echo "$STATUS" | grep '^.D' | wc -l)"
[ "$DELETED" != 0 ] && GIT_PS1="$GIT_PS1$DELETED- "
fi
if git branch -r | grep -q "$ORIGIN/$BRANCH"
then
AHEAD_BEHIND=$(git rev-list --left-right --count "$BRANCH...$ORIGIN/$BRANCH")
AHEAD=$(echo "$AHEAD_BEHIND" | pcregrep -o1 "^([0-9]+)\t([0-9]+)")
BEHIND=$(echo "$AHEAD_BEHIND" | pcregrep -o2 "^([0-9]+)\t([0-9]+)")
GIT_PS1="$GIT_PS1$([ "$AHEAD" != 0 ] && echo "$AHEAD↑ ")"
GIT_PS1="$GIT_PS1$([ "$BEHIND" != 0 ] && echo "$BEHIND↓ ")"
else
GIT_PS1="$GIT_PS1↟ "
fi
GIT_PS1="$GIT_PS1\e[m"
else
GIT_PS1=""
fi
if [ ! $1 = "0" ]
then
STATUS_PS1="\e[1;30;41m $1 \e[m"
else
STATUS_PS1=""
fi
echo -e "$SHELL_PS1$USER_PS1$HOST_PS1$PATH_PS1$GIT_PS1$STATUS_PS1"
PS1='\[\e[1m\]\$\[\e[m\] '