forked from jessfraz/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bash_prompt
226 lines (197 loc) · 5.94 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
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
#!/bin/bash
# vim: set filetype=sh :
if [[ ${COLORTERM} = gnome-* && ${TERM} = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then
export TERM='gnome-256color'
elif infocmp xterm-256color >/dev/null 2>&1; then
export TERM='xterm-256color'
fi
if tput setaf 1 &> /dev/null; then
## Formatting
tput sgr0 # reset colors
bold=$(tput bold)
reset=$(tput sgr0)
declare -A fg_colours=(
["black"]=$(tput setaf 0)
["blue"]=$(tput setaf 33)
["cyan"]=$(tput setaf 37)
["green"]=$(tput setaf 64)
["orange"]=$(tput setaf 166)
["purple"]=$(tput setaf 125)
["red"]=$(tput setaf 124)
["violet"]=$(tput setaf 61)
["white"]=$(tput setaf 15)
["yellow"]=$(tput setaf 136)
)
declare -A bg_colours=(
["black"]=$(tput setab 0)
["blue"]=$(tput setab 33)
["cyan"]=$(tput setab 37)
["green"]=$(tput setab 64)
["orange"]=$(tput setab 166)
["purple"]=$(tput setab 125)
["red"]=$(tput setab 124)
["violet"]=$(tput setab 61)
["white"]=$(tput setab 15)
["yellow"]=$(tput setab 136)
)
else
## Formatting
bold=''
reset="\\e[0m"
declare -A fg_colours=(
["black"]="\\e[30m"
["blue"]="\\e[34m"
["cyan"]="\\e[36m"
["green"]="\\e[32m"
["orange"]="\\e[33m"
["purple"]="\\e[35m"
["red"]="\\e[31m"
["violet"]="\\e[35m"
["white"]="\\e[37m"
["yellow"]="\\e[33m"
)
declare -A bg_colours=(
["black"]="\\e[40m"
["blue"]="\\e[44m"
["cyan"]="\\e[46m"
["green"]="\\e[42m"
["orange"]="\\e[43m"
["purple"]="\\e[45m"
["red"]="\\e[41m"
["violet"]="\\e[45m"
["white"]="\\e[47m"
["yellow"]="\\e[43m"
)
fi
PROMPT_LEFT_PROMPT_ELEMENTS=(os_icon context_joined dir vcs)
PROMPT_RIGHT_PROMPT_ELEMENTS=(status background_jobs)
PROMPT_LEFT_SEGMENT_SEPARATOR=''
PROMPT_RIGHT_SEGMENT_SEPARATOR=''
PROMPT_MULTILINE_FIRST_PROMPT_PREFIX=''
PROMPT_MULTILINE_LAST_PROMPT_PREFIX="\033[${fg_colours["cyan"]}╰❱❱❱"
PROMPT_PREVIOUS_ELEMENT=
prompt_git() {
local s=''
local branchName=''
# Check if the current directory is in a Git repository.
if [ "$(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}")" == '0' ]; then
# check if the current directory is in .git before running git checks
if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == 'false' ]; then
if [[ -O "$(git rev-parse --show-toplevel)/.git/index" ]]; then
git update-index --really-refresh -q &> /dev/null
fi
# Check for uncommitted changes in the index.
if ! git diff --quiet --ignore-submodules --cached; then
s+=' '
fi
# Check for unstaged changes.
if ! git diff-files --quiet --ignore-submodules --; then
s+=' '
fi
# Check for untracked files.
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
s+=' '
fi
# Check for stashed files.
if git rev-parse --verify refs/stash &>/dev/null; then
s+=' '
fi
fi
# Get the short symbolic ref.
# If HEAD isn’t a symbolic ref, get the short SHA for the latest commit
# Otherwise, just give up.
branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
git rev-parse --short HEAD 2> /dev/null || \
echo '(unknown)')"
[ -n "${s}" ] && s=" ${s}"
echo -e "${branchName}${s}"
else
echo -e 'none'
fi
}
context_prompt() {
# Highlight the user name when logged in as root.
if [[ "${USER}" == "root" ]]; then
user_name="\033[${fg_colours["red"]}\\u\033[${fg_colours["${current_fg_colour}"]}"
else
user_name="\\u"
fi
local cloud=""
if [[ -f /proc/cpuinfo ]] && grep -q "^flags.* hypervisor" /proc/cpuinfo && [[ ! -d "/mnt/c/Windows/" ]]; then
cloud="☁️ "
fi
# Highlight the hostname when connected via SSH.
if [[ "${SSH_TTY}" ]]; then
host_name="\033[${bold}${cloud}\\h\033[${reset_bold}"
else
host_name="${cloud}\\h"
fi
echo -e "${user_name}@${host_name}"
}
declare -A segment_colour_map=(
["os_icon_fg"]="white"
["os_icon_bg"]="black"
["context_fg"]="yellow"
["context_bg"]="black"
["dir_fg"]="black"
["dir_bg"]="blue"
["vcs_fg"]="black"
["vcs_bg"]="green"
["line_end_fg"]="white"
["line_end_bg"]="black"
)
declare -A segment_content_map=(
["os_icon"]=''
["context"]="$(context_prompt)"
["dir"]="\\w"
["vcs"]="\$(prompt_git)"
)
declare -A segment_logo_map=(
["os_icon"]=''
["context"]=""
["dir"]=" "
["vcs"]=""
)
segment_generate() {
local joined="${2}"
local current_segment="${1}"
local segment_output=''
if [ -n "${PROMPT_PREVIOUS_ELEMENT}" ]; then
if [ ! "${joined}" ]; then
local fg_colour="${segment_colour_map["${PROMPT_PREVIOUS_ELEMENT}_bg"]}"
local bg_colour="${segment_colour_map["${current_segment}_bg"]}"
segment_output+="\033[${fg_colours["${fg_colour}"]}${bg_colours["${bg_colour}"]}"
segment_output+="${PROMPT_LEFT_SEGMENT_SEPARATOR}"
unset fg_colour bg_colour
fi
fi
local fg_colour="${segment_colour_map["${current_segment}_fg"]}"
local bg_colour="${segment_colour_map["${current_segment}_bg"]}"
segment_output+="\033[${fg_colours["${fg_colour}"]}${bg_colours["${bg_colour}"]}"
unset fg_colour bg_colour
[ -n "${segment_logo_map["${current_segment}"]}" ] && segment_output+=" ${segment_logo_map["${current_segment}"]} "
segment_output+="${segment_content_map["${current_segment}"]}"
PS1+="${segment_output}"
PROMPT_PREVIOUS_ELEMENT="${current_segment}"
}
segment_line_end() {
local current_segment="${FUNCNAME[0]:8}"
local segment_output=
local fg_colour="${segment_colour_map["${PROMPT_PREVIOUS_ELEMENT}_bg"]}"
local bg_colour="${segment_colour_map["${current_segment}_bg"]}"
segment_output+=" \033[${fg_colours["${fg_colour}"]}${bg_colours["${bg_colour}"]}"
segment_output+="${PROMPT_LEFT_SEGMENT_SEPARATOR} "
unset fg_colour bg_colour
PS1+="${segment_output}"
PROMPT_PREVIOUS_ELEMENT="${current_segment}"
}
PS1="\\n" # newline
for segment in "${PROMPT_LEFT_PROMPT_ELEMENTS[@]}"; do
segment_generate ${segment/_joined/ joined}
done
segment_line_end
PS1+="\033[${reset}\\n"
PS1+="${PROMPT_MULTILINE_LAST_PROMPT_PREFIX}\033[${reset} " # Prompt prefix (and reset color/formatting)
export PS1
PS2="\033[${fg_colours["yellow"]}→ \033[${reset}"
export PS2