-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path.bash_prompt
165 lines (137 loc) · 4.64 KB
/
.bash_prompt
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
SHOW_GIT_PROMPT=1
SHOW_HG_PROMPT=1
SHOW_K8S_PROMPT=1
#------------------------------------------////
# Colors:
#------------------------------------------////
COLOR_RESET="\033[0m"
COLOR_BLACK="\033[0;30m"
COLOR_BLUE="\033[0;34m"
COLOR_GREEN="\033[1;32m"
COLOR_CYAN="\033[0;36m"
COLOR_RED="\033[1;31m"
COLOR_PURPLE="\033[0;35m"
COLOR_BROWN="\033[0;33m"
COLOR_LIGHTGRAY="\033[0;37m"
COLOR_DARKGRAY="\033[1;30m"
COLOR_LIGHTBLUE="\033[1;34m"
COLOR_LIGHTGREEN="\033[1;32m"
COLOR_LIGHTCYAN="\033[1;36m"
COLOR_LIGHTRED="\033[1;31m"
COLOR_LIGHTPURPLE="\033[1;35m"
COLOR_YELLOW="\033[1;33m"
COLOR_WHITE="\033[1;37m"
# ------------------------------------------------------------------------------
# Prompt:
# ------------------------------------------------------------------------------
source ~/.hg_prompt
# Figure out what type of repo we're in
function prompt_char {
git branch >/dev/null 2>/dev/null && echo '±' && return
hg root >/dev/null 2>/dev/null && echo '☿' && return
echo '↪'
}
function git_prompt {
gitstatus=$(git status 1>/dev/null 2> /dev/null)
if [ $? == 0 ] ; then
echo " \[$COLOR_CYAN\]($(git_prompt_branch)$(git_prompt_status)\[$COLOR_RESET\]"
fi
}
function k8s_prompt {
k8sctx=$(kubectl config get-contexts | grep '*')
if [[ $? == 0 ]]; then
if [[ $k8sctx =~ @ ]]; then
k8sctx=$(echo -n "$k8sctx" | awk '{print $4}' | awk -F '@' '{print $NF}' | awk -F '.' '{print $1"."$2}')
elif [[ $k8sctx =~ : ]]; then
k8sctx=$(echo -n "$k8sctx" | awk '{print $3}' | awk -F ':' '{print $4"/"$6}' | awk -F '/' '{print $3"."$1}')
else
k8sctx=$(echo -n "$k8sctx" | awk '{print $3}')
fi
if [[ $k8sctx =~ uat ]]; then
echo " \[$COLOR_YELLOW\]{${k8sctx}\[$COLOR_YELLOW\]}\[$COLOR_RESET\]"
elif [[ $k8sctx =~ prod ]]; then
echo " \[$COLOR_RED\]{${k8sctx}\[$COLOR_RED\]}\[$COLOR_RESET\]"
else
echo " \[$COLOR_PURPLE\]{${k8sctx}\[$COLOR_PURPLE\]}\[$COLOR_RESET\]"
fi
fi
}
function git_prompt_branch {
gitprompt=""
branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
if [ $branch == "HEAD" ] ; then
rev=$(git rev-parse HEAD)
gitprompt="${gitprompt}${rev:0:7}..."
else
gitprompt="${gitprompt}${branch}"
fi
gitdir=$(git rev-parse --git-dir 2> /dev/null)
if [ $gitdir != "" ] ; then
if [ -d "$gitdir/rebase-apply" ]; then
if [ -f "$gitdir/rebase-apply/rebasing" ]; then
gitprompt="${gitprompt}|REBASE"
elif [ -f "$gitdir/rebase-apply/applying" ]; then
gitprompt="${gitprompt}|AM"
else
gitprompt="${gitprompt}|AM/REBASE"
fi
elif [ -f "$gitdir/MERGE_HEAD" ]; then
gitprompt="${gitprompt}|MERGING"
elif [ -f "$gitdir/CHERRY_PICK_HEAD" ]; then
gitprompt="${gitprompt}|CHERRY-PICKING"
elif [ -f "$gitdir/REVERT_HEAD" ]; then
gitprompt="${gitprompt}|REVERTING"
elif [ -f "$gitdir/BISECT_LOG" ]; then
gitprompt="${gitprompt}|BISECTING"
fi
fi
echo $gitprompt
}
function git_prompt_status {
gitstatus=$(git status 2> /dev/null)
gitprompt=""
if echo $gitstatus | grep -E -q "working (directory|tree) clean" ; then
gitprompt="${gitprompt}\[$COLOR_GREEN\] ✔ "
else
gitprompt="${gitprompt}\[$COLOR_YELLOW\] ⚡"
fi
remote_pattern="Your branch is (ahead|behind).*by ([[:digit:]]*) commit"
diverge_pattern="and have ([[:digit:]]*) and ([[:digit:]]*) different commits? each"
if [[ ${gitstatus} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
gitprompt="${gitprompt}\[$COLOR_CYAN\] ahead \[$COLOR_GREEN\]${BASH_REMATCH[2]}\[$COLOR_CYAN\])"
else
gitprompt="${gitprompt}\[$COLOR_CYAN\] behind \[$COLOR_RED\]${BASH_REMATCH[2]}\[$COLOR_CYAN\])"
fi
elif [[ ${gitstatus} =~ ${diverge_pattern} ]]; then
gitprompt="${gitprompt}\[$COLOR_CYAN\]ahead \[$COLOR_GREEN\]${BASH_REMATCH[1]}\[$COLOR_CYAN\], behind \[$COLOR_RED\]${BASH_REMATCH[2]}\[$COLOR_CYAN\])"
else
gitprompt="${gitprompt}\[$COLOR_CYAN\])"
fi
echo $gitprompt
}
# Set the window title to show the user@host and the current working directory
# (Don't expand $HOME)
function set_bash_prompt() {
case $TERM in
xterm*)
XTERM_TITLE='\[\033]0;\H $(pwd)\a\]'
;;
*)
XTERM_TITLE=''
;;
esac
PS1="${XTERM_TITLE}\[$COLOR_PURPLE\]\u\[$COLOR_RESET\]@\[$COLOR_RED\]\h\[$COLOR_RESET\] \w\[$COLOR_CYAN\]"
if [ $SHOW_K8S_PROMPT -eq 1 ]; then
PS1="${PS1}$(k8s_prompt)"
fi
if [ $SHOW_GIT_PROMPT -eq 1 ] ; then
PS1="${PS1}$(git_prompt)"
fi
if [ $SHOW_HG_PROMPT -eq 1 ] ; then
PS1="${PS1}$(hg_branch)"
fi
PS1="${PS1}\[$COLOR_RESET\]\n\[$COLOR_GREEN\]$(prompt_char) \[$COLOR_RESET\]"
}
PROMPT_COMMAND=set_bash_prompt
# vim: set filetype=bash: