-
Notifications
You must be signed in to change notification settings - Fork 0
/
bashrc
263 lines (215 loc) · 6.01 KB
/
bashrc
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
255
256
257
258
259
260
261
262
263
SAVE_CURSOR="\[\e[s\]"
CURSOR_TO_TOP="\[\e[H\]"
CURSOR_UP_DOWN="\[\e[1A\e[1B\]"
CLEAR_LINE="\[\e[K\]"
RESTORE_CURSOR="\[\e[u\]"
BOLD="\[\e[1m\]"
NORMAL="\[\e[22m\]"
BLACK_FOREGROUND="\[\e[30m\]"
WHITE_BACKGROUND="\[\e[47m\]"
RESET_COLORS="\[\e[0m\]"
# get current branch in git repo
function parse_git_branch() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
OUTPUT=""
if [ ! "${BRANCH}" == "" ]
then
STAT=`parse_git_dirty`
OUTPUT="${BRANCH}${STAT}"
else
OUTPUT=""
fi
echo "$OUTPUT"
}
# get current status of git repo
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ]; then
bits=">${bits}"
fi
if [ "${ahead}" == "0" ]; then
bits="*${bits}"
fi
if [ "${newfile}" == "0" ]; then
bits="+${bits}"
fi
if [ "${untracked}" == "0" ]; then
bits="?${bits}"
fi
if [ "${deleted}" == "0" ]; then
bits="x${bits}"
fi
if [ "${dirty}" == "0" ]; then
bits="!${bits}"
fi
if [ ! "${bits}" == "" ]; then
echo "${bits}"
else
echo ""
fi
}
function printTitlebar() {
echo -e "${SAVE_CURSOR}${CURSOR_TO_TOP}${CLEAR_LINE}${BLACK_FOREGROUND}${WHITE_BACKGROUND}\\w${CLEAR_LINE}${RESET_COLORS}${RESTORE_CURSOR}${CURSOR_UP_DOWN}"
}
function printBar() {
PREVIOUS_EXIT_CODE=$1
HOST=$(hostname -s)
GIT=$(parse_git_branch)
COLOR_DIRECTORY=33
COLOR_GIT=99
COLOR_BAD=196
COLOR_GOOD=34
TEXT_COLOR_DIRECTORY=195
TEXT_COLOR_GIT=225
TEXT_COLOR_BAD=224
TEXT_COLOR_GOOD=194
SEPARATOR_COLOR_DIRECTORY=255
SEPARATOR_COLOR_GIT=255
SEPARATOR_COLOR_BAD=255
SEPARATOR_COLOR_GOOD=255
COLOR_HOST=234
COLOR_DIRECTORY=236
COLOR_GIT=238
COLOR_BAD=88
COLOR_GOOD=240
TEXT_COLOR_HOST=64
TEXT_COLOR_DIRECTORY=69
TEXT_COLOR_GIT=176
TEXT_COLOR_BAD=218
TEXT_COLOR_GOOD=255
SEPARATOR_COLOR_HOST=-1
SEPARATOR_COLOR_DIRECTORY=$((COLOR_DIRECTORY + 1))
SEPARATOR_COLOR_GIT=$((COLOR_GIT + 1))
SEPARATOR_COLOR_BAD=124
SEPARATOR_COLOR_GOOD=$((COLOR_GOOD + 1))
COLOR_HOST=58
COLOR_HOST=22
COLOR_DIRECTORY=18
COLOR_GIT=53
COLOR_BAD=124
COLOR_GOOD=236
TEXT_COLOR_HOST=142
TEXT_COLOR_HOST=70
TEXT_COLOR_DIRECTORY=69
TEXT_COLOR_GIT=176
TEXT_COLOR_BAD=224
TEXT_COLOR_GOOD=250
SEPARATOR_COLOR_HOST=-1
SEPARATOR_COLOR_DIRECTORY=-1
SEPARATOR_COLOR_GIT=-1
SEPARATOR_COLOR_BAD=-1
SEPARATOR_COLOR_GOOD=-1
LATEST_COLOR=$COLOR_HOST
BAR=""
#BAR+="\[\e[$LINES;1H\]"
BAR+="\n"
if [[ "$HOST" == "sherpa" || "$HOST" == "coinflipper" ]]; then
BAR+="$(separator -1 $SEPARATOR_COLOR_HOST $COLOR_HOST)"
BAR+="$(terminalColor $TEXT_COLOR_HOST $COLOR_HOST)$BOLD$HOST"
BAR+="$(separator $COLOR_HOST $SEPARATOR_COLOR_DIRECTORY $COLOR_DIRECTORY)"
else
BAR+="$(separator -1 -1 $COLOR_DIRECTORY)"
fi
LATEST_COLOR=$COLOR_DIRECTORY
BAR+="$(terminalColor $TEXT_COLOR_DIRECTORY $COLOR_DIRECTORY)$BOLD$(workingDirectory)"
if [[ -n "$GIT" ]]; then
LATEST_COLOR=$COLOR_GIT
BAR+="$(separator $COLOR_DIRECTORY $SEPARATOR_COLOR_GIT $COLOR_GIT)"
BAR+="$(terminalColor $TEXT_COLOR_GIT $COLOR_GIT)$GIT"
fi
BAR+="${RESET_COLORS}"
if (( $PREVIOUS_EXIT_CODE != 0 )); then
BAR+="$(separator $LATEST_COLOR $SEPARATOR_COLOR_BAD $COLOR_BAD)"
BAR+="$(terminalColor $TEXT_COLOR_BAD $COLOR_BAD)"
LATEST_COLOR=$COLOR_BAD
else
BAR+="$(separator $LATEST_COLOR $SEPARATOR_COLOR_GOOD $COLOR_GOOD)"
BAR+="$(terminalColor $TEXT_COLOR_GOOD $COLOR_GOOD)"
LATEST_COLOR=$COLOR_GOOD
fi
BAR+="\\$"
#BAR+="$(terminalColor $SEPARATOR_COLOR $LATEST_COLOR)▐"
BAR+="$(separator $LATEST_COLOR -1 -1) "
echo -e "$BAR"
}
function printPrompt() {
#COOL_CHARS=(▘ ▝ ▀ ▖ ▍ ▞ ▛ ▗ ▚ ▐ ▜ ▃ ▙ ▟ ▉)
SEPARATOR="◿◸"
PROMPT=""
PROMPT+="$(terminalColor 32)$BOLD$(workingDirectory)$RESET_COLORS"
GIT=$(parse_git_branch)
if [[ -n "$GIT" ]]; then
PROMPT+="$(terminalColor 245) $SEPARATOR $(terminalColor 40)${BOLD}$GIT$RESET_COLORS"
fi
#PROMPT+="$BOLD$(terminalColor 0 32) $(workingDirectory) $(terminalColor 32 40)▆$(terminalColor 40 32)▀$RESET_COLORS$(terminalColor 32 40)▂$(terminalColor 40 0)█$(terminalColor 0 40)master $RESET_COLORS"
#➔ ${BOLD}mas${NORMAL}ter"
echo -e "$PROMPT "
}
function separator() {
SEPARATOR_LEFT="█▛"
SEPARATOR_RIGHT="▟█"
if [[ "$1" -ne "-1" ]]; then
OUTPUT+="$(terminalColor $1 $2)$SEPARATOR_LEFT"
fi
if [[ "$3" -ne "-1" ]]; then
OUTPUT+="$(terminalColor $3 $2)$SEPARATOR_RIGHT"
fi
echo $OUTPUT
}
function terminalColor() {
FOREGROUND="\[\e[38;5;${1}m\]"
BACKGROUND="\[\e[48;5;${2}m\]"
if [[ -z "${1}" ]]; then
FOREGROUND="\[\e[38;5;15m\]"
fi
if [[ -z "${2}" || "${2}" -eq "-1" ]]; then
BACKGROUND=""
fi
echo ${RESET_COLORS}${FOREGROUND}${BACKGROUND}
}
function workingDirectory() {
HIERARCHY=($(echo $PWD \
| sed -e "s|^$HOME|~|" \
| sed -e "s| |!SPACE!|g" \
| sed -e "s|/| |g"
))
OUTPUT=""
if (( ${#HIERARCHY[@]} > 0 )); then
for i in $(seq 0 $((${#HIERARCHY[@]} - 1))); do
if [[ $i -eq 0 && "${HIERARCHY[$i]}" != "~" ]]; then
OUTPUT+="/"
fi
if (( $i > 0 )); then
OUTPUT+="/"
fi
if (( $i < $((${#HIERARCHY[@]} - 1)) )); then
if [[ ${HIERARCHY[$i]:0:1} =~ [^A-Za-z0-9] ]]; then
OUTPUT+="${HIERARCHY[$i]:0:2}"
else
OUTPUT+="${HIERARCHY[$i]:0:1}"
fi
else
OUTPUT+="${HIERARCHY[$i]/!SPACE!/ }"
fi
done
else
OUTPUT+="/"
fi
echo -e "$OUTPUT"
}
function set_bash_prompt() {
#PS1="\w \$ \$(parse_git_branch"
#PS1="\w $(parse_git_branch)\\$ $(printTitlebar)"
#PS1="$(printTitlebar)$(parse_git_branch)\\$ "
PS1="$(printBar $?)$RESET_COLORS"
#PS1="$(printPrompt $?)$BOLD\$$RESET_COLORS "
PS0="\n"
}
PROMPT_COMMAND=set_bash_prompt