-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_ps1
254 lines (221 loc) · 5.97 KB
/
.bash_ps1
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash
# ------------------------------------------------------------------------------
# Colors and formatting options
#
# only assign colors if terminal is present
if $(tty -s); then
resetFormatting="$(tput sgr0)"
bold="$(tput bold)"
# regular colors
black="$(tput setaf 0)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
blue="$(tput setaf 4)"
magenta="$(tput setaf 5)"
cyan="$(tput setaf 6)"
white="$(tput setaf 7)"
# High intensity colors
blackH="${bold}${black}"
redH="${bold}${red}"
greenH="${bold}${green}"
yellowH="${bold}${yellow}"
blueH="${bold}${blue}"
magentaH="${bold}${magenta}"
cyanH="${bold}${cyan}"
whiteH="${bold}${white}"
# background colors
blackB="$(tput setab 0)"
redB="$(tput setab 1)"
greenB="$(tput setab 2)"
yellowB="$(tput setab 3)"
blueB="$(tput setab 4)"
magentaB="$(tput setab 5)"
cyanB="$(tput setab 6)"
whiteB="$(tput setab 7)"
fi
# ------------------------------------------------------------------------------
# Aliases for host and user names
#
PRIVATES="$HOME/.bash_privates"
if [[ -f "$PRIVATES" && -r "$PRIVATES" ]]; then
source "$PRIVATES" && USE_PRIVATES=true
else
USE_PRIVATES=false
fi
# re-implement the following in $PRIVATES for sensitive data
if [[ $USE_PRIVATES == false ]]; then
# Provide user name alias for prompt
__user_alias() {
case `whoami` in
root)
echo -n "###"
;;
foo)
echo -n "bar"
;;
*)
echo -n ""
;;
esac
}
# Provide host tags
case `hostname` in
*.foo.bar)
HOST_TAGS=":server:"
;;
larry* | curly* | moe*)
HOST_TAGS=":workstation:"
;;
*)
HOST_TAGS=""
;;
esac
export HOST_TAGS
# Provide host alias names
case `hostname` in
foo)
HOST_ALIAS="bar"
;;
baz)
HOST_ALIAS="blah"
;;
esac
[ -n "${HOST_ALIAS}" ] && export HOST_ALIAS
# Select host name color based on HOST_TAGS environment variable
case ${HOST_TAGS} in
*:server:*)
HOST_COLOR=${black}${redB}
;;
*:workstation:*)
HOST_COLOR=${blue}
;;
*)
HOST_COLOR=${white}${blackB}
;;
esac
fi
# ------------------------------------------------------------------------------
# Bash PROMPT
#
function __prompt_date {
echo -n `date +%H:%M`
}
# Username or alias
function __prompt_username {
if [[ "$(type -t __user_alias)" == "function" ]]; then
local user_alias=$(__user_alias)
fi
if [[ -n "$user_alias" ]]; then
echo -n "$user_alias"
else
echo -n "$(whoami | cut -c1-8)"
fi
}
# Hostname or alias
function __prompt_hostname {
if [[ -n ${HOST_ALIAS} ]]; then
echo -n ${HOST_ALIAS}
else
echo -n "$(hostname -s | cut -c1-16)"
fi
}
# SSH agent indicator
function __prompt_ssh_agent {
if [[ -n "${SSH_CLIENT}" ]] && [[ -n "${SSH_AUTH_SOCK}" ]] && [[ -e "${SSH_AUTH_SOCK}" ]]; then
echo -n "@"
fi
}
# SSH session session indicator
function __prompt_ssh {
if [[ -n "$SSH_CLIENT" ]]; then
echo -n 'ssh'
fi
}
# Show version controlled repository status.
# vcprompt is used if installed, otherwise __git_ps1 will be tried as well.
# Install vcsprompt -> hg clone https://bitbucket.org/mitsuhiko/vcprompt
export GIT_PS1_SHOWDIRTYSTATE="yes"
export GIT_PS1_SHOWUPSTREAM="no"
export GIT_PS1_SHOWUNTRACKEDFILES="yes"
function __prompt_vcs {
if [[ $(which vcprompt 2>/dev/null) ]]; then
vcprompt -f " (%n:%b%m%u)"
elif [[ $(type -t __git_ps1) == "function" ]]; then
__git_ps1 " (%s)"
fi
}
# Support function to compactify a path
# copied: http://stackoverflow.com/questions/3497885/code-challenge-bash-prompt-path-shortener
function __dir_chomp {
local p=${1/#$HOME/\~} b s
# Remove [ and ] from strings
# (also, regular expression matching on [ ] below creates infinite recursion.)
p=${p//[/ }
p=${p//]/ }
# Remove multiple spaces, don't need them
p=${p// / }
s=${#p}
while [[ $p != ${p//\/} ]] && (($s>$2))
do
p=${p#/}
[[ $p =~ \.?. ]]
b=${b:+${b}/}${BASH_REMATCH[0]}
p=${p#*/}
((s=${#b}+${#p}))
done
#echo ${b/\/~/\~}${b+/}$p
echo ${b/\/~/\~}${b:+/}$p
}
# Compact version of current working directory
function __prompt_pwd {
echo -n $(__dir_chomp "$(pwd)" 40)
}
# Visual indicator of last exit code
function __prompt_last {
if [[ $EUID -eq 0 ]]; then
if [[ ${RET} = "0" ]]; then
echo -n "#"
else
echo -n '!!! #'
fi
else
if [[ ${RET} = "0" ]]; then
echo -n "\$"
else
echo -n '$%&!'
fi
fi
}
# Append the storage of exit code to prompt command
if [[ $PROMPT_COMMAND =~ ^.*\;[[:space:]$'\n'$'\r']*$ ]]; then
PROMPT_COMMAND="${PROMPT_COMMAND%;*}"
fi
export PROMPT_COMMAND="export RET=\$?${PROMPT_COMMAND:+;${PROMPT_COMMAND}}"
# Set up prompt
function __prompt_activate {
# Set title in xterm*
case $TERM in
xterm*|rxvt*)
TITLEBAR='\[\033]0;\u@\h:\w\007\]'
;;
*)
TITLEBAR=""
;;
esac
# Set the prompt
PS1="${TITLEBAR}\
\[${redH}\]\$(__prompt_date)\[${resetFormatting}\] \
\[${yellowH}\]\$(__prompt_username)\[${resetFormatting}\]\
\[${magentaH}\]@\[${resetFormatting}\]\
\[${HOST_COLOR}\]\$(__prompt_hostname)\[${resetFormatting}\]\
\[${white}${magentaB}\]\$(__prompt_ssh_agent)\[${resetFormatting}\]\
\[${black}${magentaB}\]\$(__prompt_ssh)\[${resetFormatting}\] \
\[${blueH}\]\$(__prompt_pwd)\[${resetFormatting}\]\
\[${greenH}\]\$(__prompt_vcs)\[${resetFormatting}\]\
\[${magenta}\]\$(__prompt_last)\[${resetFormatting}\] "
PS2='> '
PS4='+ '
}
# Activate the prompt code
__prompt_activate