-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbashprompt4devops.sh
More file actions
executable file
Β·237 lines (205 loc) Β· 6.38 KB
/
Copy pathbashprompt4devops.sh
File metadata and controls
executable file
Β·237 lines (205 loc) Β· 6.38 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/bin/bash
###########################################################
#
# bash-prompt4devops.sh
#
# This little bash script make your bash much more pretty
# and show you some information about kubelet, git and
# much more.
#
# Missing some fonts:
# sudo apt-get install ttf-ancient-fonts
#
# ___ __
# / _ \ / /
# __| (_) |_ __ ___ / /
# / __> _ <| '_ ` _ \| '_ \
# | (_| (_) | | | | | | (_) |
# \___\___/|_| |_| |_|\___/
#
# https://github.com/c8m6/
#
# Configure with environment variables:
# export BP_DISABLE_CLOCK=true
# export BP_DISABLE_EXITSTATUS=true
###########################################################
# color definitions
if [ "${TERM}" == 'xterm-256color' ] ; then
grey='\e[38;5;235m'
else
grey='\e[90m'
fi
green='\e[32m'
red='\e[31m'
cyan='\e[36m'
reset='\e[0m'
yellow='\e[33m'
blue='\e[35m'
# functions
function _bp_get_ttywidth () {
export TERM_WIDTH=$(stty size | awk '{print $2}')
}
function _bp_cmd_time_start () {
timer=${timer:-$SECONDS}
}
function _bp_cmd_time_stop {
execution_time=$(($SECONDS - $timer))
if [ $execution_time -lt 10 ] ; then
cmd_runtime="$(date -d@${execution_time} -u +%ss)"
elif [ $execution_time -lt 600 ] ; then
cmd_runtime="$(date -d@${execution_time} -u '+%mm %Ss')"
elif [ $execution_time -lt 3600 ] ; then
cmd_runtime="$(date -d@${execution_time} -u '+%Mm %Ss')"
else
cmd_runtime="$(date -d@${execution_time} -u '+%Hh %Mm %Ss')"
fi
unset timer
}
trap '_bp_cmd_time_start' DEBUG
PROMPT_COMMAND="_bp_cmd_time_stop ; history -a ; _bp_get_ttywidth"
function _bp_lastcmdstat () {
if [ -z $BP_DISABLE_EXITSTATUS ] ; then
if [ ! $1 -eq 0 ] ; then
local error_color=$red
local error_sign='π±'
local error_code=" ${1} "
else
local error_color=''
local error_sign=''
fi
if [ $execution_time -gt -0 ] ; then
local msg_time="${grey}β${cmd_runtime}${reset}"
fi
echo -ne "${error_color}${error_sign}${error_code}${msg_time}"
fi
}
function _bp_pwd () {
local path_element
local path_part
local current_dir=$(dirs +0)
local last_dir=$(basename ${current_dir})
local path_maxlength=$(echo "$TERM_WIDTH / 3" | bc | cut -d '.' -f 1)
local current_repo=$(git rev-parse --show-toplevel 2> /dev/null)
current_repo=${current_repo/$HOME/\~}
current_repo_name=${current_repo##*/}
oIFS="$IFS"
IFS='/'
for path_element in $current_dir ; do
if [ ! "x${path_element}x" == "xx" ] ; then
if [ "${path_element}" == "${current_repo_name}" ] ; then
path_part="${path_part}/${blue}${path_element}${cyan}"
elif [ "${path_element}" == '~' ] ; then
path_part='~'
else
if [[ ${#current_dir} -gt $path_maxlength && "${path_element}" != "${last_dir}" ]]; then
path_part="${path_part}/${path_element:0:1}"
else
path_part="${path_part}/${path_element}"
fi
fi
fi
done
IFS=$oIFS
local dir_msg="${cyan}${path_part}/${reset}"
echo -ne "${dir_msg}"
}
function _bp_kubectl () {
local current_context=$(oc config current-context 2> /dev/null || kubectl config current-context 2> /dev/null || /bin/false)
if [ $current_context ] ; then
local current_context_color
case $current_context in
*Prod*|*prod* ) current_context_color=$red ;;
*) current_context_color=$green ;;
esac
echo -ne "${grey}|${current_context_color}βΈ ${current_context}${reset}"
fi
}
function _bp_clock () {
if [ -z $BP_DISABLE_CLOCK ] ; then
echo -ne "${grey}|${blue}β$(date +%H:%M)${reset}"
fi
}
function _bp_userandhost () {
local user=$(whoami)
local host=$(hostname)
if [ $user == 'root' ] ; then
local user_color=$red
else
local user_color=$green
fi
local part_user="${user_color}${user}"
if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
local part_host="${yellow}@${host}"
else
local part_host="${green}@${host}"
fi
echo -ne "${part_user}${part_host}"
}
function _bp_gitstatus () {
local repo=$(git rev-parse --show-toplevel 2> /dev/null)
if [ ! "${repo}" == "" ] ; then
if test $(find ${repo}/.git/FETCH_HEAD -mmin +5 2> /dev/null) ; then
GIT_TERMINAL_PROMPT=0 git fetch --quiet &disown
fi
local branch
local fetch_rm
local changed=0
local conflicts=0
local untracked=0
local warn=0
local error=0
git status --porcelain=2 --branch | (
while read line ; do
case "${line}" in
'# branch.head'*) branch=$(echo $line | cut -d" " -f3) ; ;;
'# branch.ab'*) fetch_a=$(echo $line | cut -d" " -f3) ; fetch_b=$(echo $line | cut -d" " -f4) ; ;;
'u'*) ((conflicts++)) ; ;;
'1'*) ((changed++)) ; ;;
'2'*) ((changed++)) ; ;;
'?'*) ((untracked++)) ; ;;
esac
done
local forward=${fetch_a/+/}
local behind=${fetch_b/-/}
local warn=$((conflicts+changed+untracked+forward+behind))
local warn_st=$((conflicts+changed+untracked))
local error=$conflicts
if [ $warn -gt 0 ] ; then
local color=$yellow
elif [ $error -gt 0 ] ; then
local color=$red
else
local color=$green
local msg_clean="${green}β"
fi
if [ $warn_st -eq 0 ] && [ "${msg_clean}" == "" ] ; then
local msg_warn_st="β"
fi
if [ $changed -gt 0 ] ; then
local msg_changed="β${changed}"
fi
if [ $untracked -gt 0 ] ; then
local msg_untracked="β${untracked}"
fi
if [ $conflicts -gt 0 ] ; then
local msg_conflict="β ${conflicts}"
fi
if [ $forward -gt 0 2>/dev/null ] ; then
local msg_ahead="β${forward}"
fi
if [ $behind -gt 0 2>/dev/null ] ; then
local msg_behind="β${behind}"
fi
local branch_maxlength=$(echo "$TERM_WIDTH / 6" | bc | cut -d '.' -f 1)
if [ ${#branch} -gt $branch_maxlength ] ; then
local branch_name=$(printf "%.$[branch_maxlength-3]s..." "${branch}")
else
local branch_name=$branch
fi
local message="${color}ξ ${branch_name} ${msg_clean}${msg_warn_st}${msg_conflict}${msg_changed}${msg_untracked}${msg_behind}${msg_ahead}"
echo -ne "${grey}|${message}${reset}"
)
fi
}
set -m
export PS1="\$(_bp_lastcmdstat \$?)\n\$(_bp_userandhost):\$(_bp_pwd) \$(_bp_gitstatus)\$(_bp_clock)\$(_bp_kubectl)\n# "