-
Notifications
You must be signed in to change notification settings - Fork 35
/
theme.bash
305 lines (257 loc) · 8.06 KB
/
theme.bash
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# Git bash for windows powerline theme
#
# Licensed under MIT
# Based on https://github.com/Bash-it/bash-it and https://github.com/Bash-it/bash-it/tree/master/themes/powerline
# Some ideas from https://github.com/speedenator/agnoster-bash
# Git code based on https://github.com/joeytwiddle/git-aware-prompt/blob/master/prompt.sh
# More info about color codes in https://en.wikipedia.org/wiki/ANSI_escape_code
PROMPT_CHAR=${POWERLINE_PROMPT_CHAR:=""}
POWERLINE_LEFT_SEPARATOR=" "
POWERLINE_PROMPT="last_status user_info cwd scm"
USER_INFO_SSH_CHAR=" "
USER_INFO_PROMPT_COLOR="C B"
SCM_GIT_CHAR=" "
SCM_PROMPT_CLEAN=""
SCM_PROMPT_DIRTY="*"
SCM_PROMPT_AHEAD="↑"
SCM_PROMPT_BEHIND="↓"
SCM_PROMPT_CLEAN_COLOR="G Bl"
SCM_PROMPT_DIRTY_COLOR="R Bl"
SCM_PROMPT_AHEAD_COLOR=""
SCM_PROMPT_BEHIND_COLOR=""
SCM_PROMPT_STAGED_COLOR="Y Bl"
SCM_PROMPT_UNSTAGED_COLOR="R Bl"
SCM_PROMPT_COLOR=${SCM_PROMPT_CLEAN_COLOR}
CWD_PROMPT_COLOR="B C"
STATUS_PROMPT_COLOR="Bl R B"
STATUS_PROMPT_ERROR="✘"
STATUS_PROMPT_ERROR_COLOR="Bl R B"
STATUS_PROMPT_ROOT="⚡"
STATUS_PROMPT_ROOT_COLOR="Bl Y B"
STATUS_PROMPT_JOBS="●"
STATUS_PROMPT_JOBS_COLOR="Bl Y B"
function __color {
local bg
local fg
local mod
case $1 in
'Bl') bg=40;;
'R') bg=41;;
'G') bg=42;;
'Y') bg=43;;
'B') bg=44;;
'M') bg=45;;
'C') bg=46;;
'W') bg=47;;
*) bg=49;;
esac
case $2 in
'Bl') fg=30;;
'R') fg=31;;
'G') fg=32;;
'Y') fg=33;;
'B') fg=34;;
'M') fg=35;;
'C') fg=36;;
'W') fg=37;;
*) fg=39;;
esac
case $3 in
'B') mod=1;;
*) mod=0;;
esac
# Control codes enclosed in \[\] to not polute PS1
# See http://unix.stackexchange.com/questions/71007/how-to-customize-ps1-properly
echo "\[\e[${mod};${fg};${bg}m\]"
}
function __powerline_user_info_prompt {
local user_info=""
local color=${USER_INFO_PROMPT_COLOR}
if [[ -n "${SSH_CLIENT}" ]]; then
user_info="${USER_INFO_SSH_CHAR}\u@\h"
else
user_info="\u@\h"
fi
[[ -n "${user_info}" ]] && echo "${user_info}|${color}"
}
function __powerline_cwd_prompt {
echo "\w|${CWD_PROMPT_COLOR}"
}
function __powerline_scm_prompt {
git_local_branch=""
git_branch=""
git_dirty=""
git_dirty_count=""
git_ahead_count=""
git_ahead=""
git_behind_count=""
git_behind=""
find_git_branch() {
# Based on: http://stackoverflow.com/a/13003854/170413
git_local_branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
if [[ -n "$git_local_branch" ]]; then
if [[ "$git_local_branch" == "HEAD" ]]; then
# Branc detached Could show the hash here
git_branch=$(git rev-parse --short HEAD 2>/dev/null)
else
git_branch=$git_local_branch
fi
else
git_branch=""
return 1
fi
}
find_git_dirty() {
# All dirty files (modified and untracked)
local status_count=$(git status --porcelain 2> /dev/null | wc -l)
if [[ "$status_count" != 0 ]]; then
git_dirty=true
git_dirty_count="$status_count"
else
git_dirty=''
git_dirty_count=''
fi
}
find_git_ahead_behind() {
if [[ -n "$git_local_branch" ]] && [[ "$git_branch" != "HEAD" ]]; then
local upstream_branch=$(git rev-parse --abbrev-ref "@{upstream}" 2> /dev/null)
# If we get back what we put in, then that means the upstream branch was not found. (This was observed on git 1.7.10.4 on Ubuntu)
[[ "$upstream_branch" = "@{upstream}" ]] && upstream_branch=''
# If the branch is not tracking a specific remote branch, then assume we are tracking origin/[this_branch_name]
[[ -z "$upstream_branch" ]] && upstream_branch="origin/$git_local_branch"
if [[ -n "$upstream_branch" ]]; then
git_ahead_count=$(git rev-list --left-right ${git_local_branch}...${upstream_branch} 2> /dev/null | grep -c '^<')
git_behind_count=$(git rev-list --left-right ${git_local_branch}...${upstream_branch} 2> /dev/null | grep -c '^>')
if [[ "$git_ahead_count" = 0 ]]; then
git_ahead_count=''
else
git_ahead=true
fi
if [[ "$git_behind_count" = 0 ]]; then
git_behind_count=''
else
git_behind=true
fi
fi
fi
}
local color
local scm_info
find_git_branch && find_git_dirty && find_git_ahead_behind
#not in Git repo
[[ -z "$git_branch" ]] && return
scm_info="${SCM_GIT_CHAR}${git_branch}"
[[ -n "$git_dirty" ]] && color=${SCM_PROMPT_DIRTY_COLOR} || color=${SCM_PROMPT_CLEAN_COLOR}
[[ -n "$git_behind" ]] && scm_info+="${SCM_PROMPT_BEHIND}${git_behind_count}"
[[ -n "$git_ahead" ]] && scm_info+="${SCM_PROMPT_AHEAD}${git_ahead_count}"
[[ -n "${scm_info}" ]] && echo "${scm_info}|${color}"
}
function __powerline_left_segment {
local OLD_IFS="${IFS}"; IFS="|"
local params=( $1 )
IFS="${OLD_IFS}"
local separator_char="${POWERLINE_LEFT_SEPARATOR}"
local separator=""
local styles=( ${params[1]} )
if [[ "${SEGMENTS_AT_LEFT}" -gt 0 ]]; then
styles[1]=${LAST_SEGMENT_COLOR}
styles[2]=""
separator="$(__color ${styles[@]})${separator_char}"
fi
styles=( ${params[1]} )
LEFT_PROMPT+="${separator}$(__color ${styles[@]})${params[0]}"
#Save last background for next segment
LAST_SEGMENT_COLOR=${styles[0]}
(( SEGMENTS_AT_LEFT += 1 ))
}
function __powerline_last_status_prompt {
local symbols=()
[[ $last_status -ne 0 ]] && symbols+="$(__color ${STATUS_PROMPT_ERROR_COLOR})${STATUS_PROMPT_ERROR}"
[[ $UID -eq 0 ]] && symbols+="$(__color ${STATUS_PROMPT_ROOT_COLOR})${STATUS_PROMPT_ROOT}"
[[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="$(__color ${STATUS_PROMPT_JOBS_COLOR})${STATUS_PROMPT_JOBS}"
[[ -n "$symbols" ]] && echo "$symbols|${STATUS_PROMPT_COLOR}"
}
function __powerline_prompt_command {
local last_status="$?" ## always the first
local separator_char="${POWERLINE_PROMPT_CHAR}"
LEFT_PROMPT=""
SEGMENTS_AT_LEFT=0
LAST_SEGMENT_COLOR=""
## left prompt ##
for segment in $POWERLINE_PROMPT; do
local info="$(__powerline_${segment}_prompt)"
[[ -n "${info}" ]] && __powerline_left_segment "${info}"
done
[[ -n "${LEFT_PROMPT}" ]] && LEFT_PROMPT+="$(__color - ${LAST_SEGMENT_COLOR})${separator_char}$(__color)"
PS1="${LEFT_PROMPT} "
## cleanup ##
unset LAST_SEGMENT_COLOR \
LEFT_PROMPT \
SEGMENTS_AT_LEFT
}
function safe_append_prompt_command {
local prompt_re
# Set OS dependent exact match regular expression
if [[ ${OSTYPE} == darwin* ]]; then
# macOS
prompt_re="[[:<:]]${1}[[:>:]]"
else
# Linux, FreeBSD, etc.
prompt_re="\<${1}\>"
fi
if [[ ${PROMPT_COMMAND} =~ ${prompt_re} ]]; then
return
elif [[ -z ${PROMPT_COMMAND} ]]; then
PROMPT_COMMAND="${1}"
else
PROMPT_COMMAND="${1};${PROMPT_COMMAND}"
fi
}
safe_append_prompt_command __powerline_prompt_command
__color_matrix() {
local buffer
declare -A colors=([0]=black [1]=red [2]=green [3]=yellow [4]=blue [5]=purple [6]=cyan [7]=white)
declare -A mods=([0]='' [1]=B [4]=U [5]=k [7]=N)
# Print foreground color names
echo -ne " "
for fgi in "${!colors[@]}"; do
local fg=`printf "%10s" "${colors[$fgi]}"`
#print color names
echo -ne "\e[m$fg "
done
echo
# Print modificators
echo -ne " "
for fgi in "${!colors[@]}"; do
for modi in "${!mods[@]}"; do
local mod=`printf "%1s" "${mods[$modi]}"`
buffer="${buffer}$mod "
done
# echo -ne "\e[m "
buffer="${buffer} "
done
echo -e "$buffer\e[m"
buffer=""
# Print color matrix
for bgi in "${!colors[@]}"; do
local bgn=$((bgi + 40))
local bg=`printf "%6s" "${colors[$bgi]}"`
#print color names
echo -ne "\e[m$bg "
for fgi in "${!colors[@]}"; do
local fgn=$((fgi + 30))
local fg=`printf "%7s" "${colors[$fgi]}"`
for modi in "${!mods[@]}"; do
buffer="${buffer}\e[${modi};${bgn};${fgn}m "
done
# echo -ne "\e[m "
buffer="${buffer}\e[m "
done
echo -e "$buffer\e[m"
buffer=""
done
}
__character_map () {
echo "powerline: ±●➦★⚡★ ✗✘✓✓✔✕✖✗← ↑ → ↓"
echo "other: ☺☻👨⚙⚒⚠⌛"
}