forked from meadowface/bash-prompt-vcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-bash-prompt-vcs
executable file
·184 lines (153 loc) · 4.65 KB
/
install-bash-prompt-vcs
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
#!/usr/bin/env bash
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
# color echo http://www.faqs.org/docs/abs/HTML/colorizing.html
default="\033[0m"
black="\033[30m"
black_inverse="\033[7;30m"
red="\033[31m"
red_bold="\033[1;31m"
red_inverse="\033[7;31m"
green="\033[32m"
green_bold="\033[1;32m"
green_inverse="\033[7;32m"
yellow="\033[33m"
yellow_bold="\033[1;33m"
yellow_inverse="\033[7;33m"
blue="\033[34m"
blue_bold="\033[1;34m"
blue_inverse="\033[7;34m"
magenta="\033[35m"
magenta_bold="\033[1;35m"
magenta_inverse="\033[7;35m"
cyan="\033[36m"
cyan_bold="\033[1;36m"
cyan_inverse="\033[7;36m"
white="\033[37m"
white_bold="\033[1;37m"
white_inverse="\033[7;37m"
# Color-echo, Argument $1 = message, Argument $2 = color, Argument $3 = no-newline?
function cecho () {
local default_msg="No message passed."
# Defaults to default message.
message=${1:-$default_msg}
# Defaults to black, if not specified.
color=${2:-$default}
if [[ ${3+x} ]]
then
echo -en "$color$message"
else
echo -e "$color$message"
fi
# Reset text attributes to normal + without clearing screen.
tput sgr0
return
}
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
# https://github.com/tlatsas/bash-spinner
function _spinner() {
# $1 start/stop
#
# on start: $2 display message
# on stop : $2 process exit status
# $3 spinner function pid (supplied from stop_spinner)
local on_success="DONE"
local on_fail="FAIL"
local white="\e[1;37m"
local green="\e[1;32m"
local red="\e[1;31m"
local nc="\e[0m"
case $1 in
start)
# calculate the column where spinner and status msg will be displayed
let column=$(tput cols)-${#2}-8
# display message and position the cursor in $column column
echo -ne ${2}
printf "%${column}s"
#echo -ne "\t"
# start spinner
i=1
sp='\|/-'
delay=${SPINNER_DELAY:-0.15}
while :
do
printf "\b${sp:i++%${#sp}:1}"
sleep $delay
done
;;
stop)
if [[ -z ${3} ]]; then
echo "spinner is not running.."
exit 1
fi
kill $3 > /dev/null 2>&1
# inform the user uppon success or failure
echo -en "\b["
if [[ $2 -eq 0 ]]; then
echo -en "${green}${on_success}${nc}"
else
echo -en "${red}${on_fail}${nc}"
fi
echo -e "]"
;;
*)
echo "invalid argument, try {start/stop}"
exit 1
;;
esac
}
function start_spinner {
# $1 : msg to display
# $2 : cecho color
_spinner "start" "${1}" $2 &
# set global spinner pid
_sp_pid=$!
disown
}
function stop_spinner {
# $1 : command exit status
_spinner "stop" $1 $_sp_pid
unset _sp_pid
}
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
SHELL_CONFIG_FILE=""
function install-vcs-prompt() {
cecho ">>> Installing VCS Prompt" $cyan_bold
start_spinner "Download bash-prompt-vcs.bash from git project repository"
CMD=$(wget https://raw.githubusercontent.com/portrino/bash-prompt-vcs/master/bash-prompt-vcs.bash -O $HOME/.bash-prompt-vcs.bash -q)
sleep 1s
stop_spinner $?
if [ -f "$HOME/.profile" ]
then
SHELL_CONFIG_FILE=".profile"
fi
if [ -f "$HOME/.bash_profile" ]
then
SHELL_CONFIG_FILE=".bash_profile"
fi
if [ -f "$HOME/.bashrc" ]
then
SHELL_CONFIG_FILE=".bashrc"
fi
CHECK=$( cat $HOME/$SHELL_CONFIG_FILE | grep -c "# vcs prompt" )
if [ $CHECK -eq 0 ]
then
start_spinner "Update ~/$SHELL_CONFIG_FILE -> adds custom PS1 (prompt configuration string)"
cat >> $HOME/$SHELL_CONFIG_FILE <<'EOL'
# vcs prompt - begin
source $HOME/.bash-prompt-vcs.bash
PS1=$(echo $PS1 | sed -e 's/\\\$$/\$\(bpvcs_bash_prompt\)\\\$ /g')
# vcs prompt - end
EOL
sleep 1s
stop_spinner $?
else
cecho "Update ~/$SHELL_CONFIG_FILE -> adds custom PS1 (prompt configuration string)" $default 1;
cecho " - skipped, custom PS1 for vcs prompt already set!" $green_bold
fi
source $HOME/$SHELL_CONFIG_FILE
cecho " "
cecho "Now change to a directory with an initialized git|hg|svn repository!" $yellow
cecho " -> If the prompt does not change, you may need to source your shell configuration file manually again!" $cyan
cecho " Simply execute: source $HOME/$SHELL_CONFIG_FILE" $cyan
}
install-vcs-prompt