forked from undu/bash-powerline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerline.bash
320 lines (271 loc) · 7.4 KB
/
powerline.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env bash
__powerline() {
# User config variables,
# it's recommended to override those variables through .bashrc or similar
#
# Use powerline mode
# readonly POWERLINE_FONT=''
#
# Always show user in the prompt
# readonly SHOW_USER=''
#
# Never show a default user
# readonly DEFAULT_USER='user'
# Default background and foreground ANSI colours
readonly DEFAULT_BG=0
readonly DEFAULT_FG=7
# Max length of full path
readonly MAX_PATH_LENGTH=30
# Unicode symbols
if [ -z "${POWERLINE_FONT+x}" ]; then
readonly GIT_BRANCH_SYMBOL='⑂'
else
readonly GIT_BRANCH_SYMBOL=''
fi
readonly GIT_BRANCH_CHANGED_SYMBOL='Δ'
readonly GIT_NEED_PUSH_SYMBOL='↑'
readonly GIT_NEED_PULL_SYMBOL='↓'
# Powerline symbols
readonly BLOCK_START=''
# ANSI Colours
readonly BLACK=0
readonly RED=1
readonly GREEN=2
readonly YELLOW=3
readonly BLUE=4
readonly MAGENTA=5
readonly CYAN=6
readonly WHITE=7
readonly BLACK_BRIGHT=8
readonly RED_BRIGHT=9
readonly GREEN_BRIGHT=10
readonly YELLOW_BRIGHT=11
readonly BLUE_BRIGHT=12
readonly MAGENTA_BRIGHT=13
readonly CYAN_BRIGHT=14
readonly WHITE_BRIGHT=15
# Font effects
readonly DIM="\[$(tput dim)\]"
readonly REVERSE="\[$(tput rev)\]"
readonly RESET="\[$(tput sgr0)\]"
readonly BOLD="\[$(tput bold)\]"
# Generate terminal colour codes
# $1 is an int (a colour) and $2 must be 'fg' or 'bg'
__colour() {
case "$2" in
'fg'*)
echo "\[$(tput setaf "$1")\]"
;;
'bg'*)
echo "\[$(tput setab "$1")\]"
;;
*)
echo "\[$(tput setab "$1")\]"
;;
esac
}
# Generate a single-coloured block for the prompt
__prompt_block() {
local bg; local fg
if [ ! -z "${1+x}" ]; then
bg=$1
else
if [ ! -z "$last_bg" ]; then
bg=$last_bg
else
bg=$DEFAULT_BG
fi
fi
if [ ! -z "${2+x}" ]; then
fg=$2
else
fg=$DEFAULT_FG
fi
local block
# Need to generate a separator if the background changes
if [[ ! -z "$last_bg" && "$bg" != "$last_bg" && ! -z "${POWERLINE_FONT+x}" ]]; then
block+="$(__colour "$bg" 'bg')"
block+="$(__colour "$last_bg" 'fg')"
block+="$BLOCK_START $RESET"
block+="$(__colour "$bg" 'bg')"
block+="$(__colour "$fg" 'fg')"
else
block+="$(__colour "$bg" 'bg')"
block+="$(__colour "$fg" 'fg')"
block+=" "
fi
if [ ! -z "${3+x}" ]; then
block+="$3 $RESET"
fi
last_bg=$bg
__block_text="$block"
}
function __end_block() {
__block_text=''
if [ ! -z "$last_bg" ]; then
if [ ! -z "${POWERLINE_FONT+x}" ]; then
__block_text+="$(__colour $DEFAULT_BG 'bg')"
__block_text+="$(__colour "$last_bg" 'fg')"
__block_text+="$BLOCK_START$RESET"
__block_text+="$(__colour $DEFAULT_BG 'bg')"
__block_text+="$(__colour "$DEFAULT_FG" 'fg')"
else
__block_text+="$(__colour $DEFAULT_BG 'bg')"
__block_text+="$(__colour "$DEFAULT_FG" 'fg')"
fi
fi
__block_text+=' '
}
### Prompt components
__git_block() {
if [ ! hash git 2> /dev/null ]; then
# git not found
__block_text=''
return
fi
# force git output in English to make our work easier
local git_eng="env LANG=C git"
# check if pwd is under git
git rev-parse 2> /dev/null
if [ $? != 0 ]; then
# not in a git repo, bail out
__block_text=''
return
fi
# get current branch name or short SHA1 hash for detached head
local branch; local ref_symbol
branch="$($git_eng symbolic-ref --short HEAD 2>/dev/null)"
if [ $? != 0 ]; then
branch="$($git_eng describe --tags --always 2>/dev/null)"
ref_symbol='➦'
else
ref_symbol=$GIT_BRANCH_SYMBOL
fi
ref="$ref_symbol $branch "
local marks
# check if HEAD is dirty
if [ -n "$($git_eng status --porcelain 2>/dev/null)" ]; then
dirty='y'
marks+=" $GIT_BRANCH_CHANGED_SYMBOL"
fi
# how many commits local branch is ahead/behind of remote?
local stat; local aheadN; local behindN
stat="$($git_eng status --porcelain --branch 2>/dev/null | grep '^##' | grep -o '\[.\+\]$')"
aheadN="$(echo "$stat" | grep -o 'ahead [[:digit:]]\+' | grep -o '[[:digit:]]\+')"
behindN="$(echo "$stat" | grep -o 'behind [[:digit:]]\+' | grep -o '[[:digit:]]\+')"
[ -n "$aheadN" ] && marks+=" $GIT_NEED_PUSH_SYMBOL$aheadN"
[ -n "$behindN" ] && marks+=" $GIT_NEED_PULL_SYMBOL$behindN"
local bg; local fg
fg=$BLACK
if [ -z "$dirty" ]; then
bg=$GREEN
else
bg=$YELLOW
fi
__prompt_block $bg $fg "$ref$marks"
}
__virtualenv_block() {
# Copied from Python virtualenv's activate.sh script.
# https://github.com/pypa/virtualenv/blob/a9b4e673559a5beb24bac1a8fb81446dd84ec6ed/virtualenv_embedded/activate.sh#L62
# License: MIT
if [ -n "$VIRTUAL_ENV" ]; then
local text
if [ "$(basename \""$VIRTUAL_ENV"\")" == "__" ]; then
# special case for Aspen magic directories
# see http://www.zetadev.com/software/aspen/
text="[$(basename \$\(dirname \""$VIRTUAL_ENV"\"\))]"
else
text="($(basename \""$VIRTUAL_ENV"\"))"
fi
__prompt_block $WHITE $BLACK "$text"
else
__block_text=''
fi
}
__pwd_block() {
# Use ~ to represent $HOME prefix
local pwd; pwd=$(pwd | sed -e "s|^$HOME|~|")
if [[ ( $pwd = ~\/*\/* || $pwd = \/*\/*/* ) && ${#pwd} -gt $MAX_PATH_LENGTH ]]; then
local IFS='/'
read -ra split <<< "$pwd"
if [[ $pwd = ~* ]]; then
pwd="~/${split[1]}/.../${split[*]:(-2):1}/${split[*]:(-1)}"
else
pwd="/${split[1]}/.../${split[*]:(-2):1}/${split[*]:(-1)}"
fi
fi
__prompt_block $BLACK_BRIGHT $WHITE_BRIGHT "$pwd"
}
# superuser or not, here I go!
__user_block() {
# Colours to use
local fg=$WHITE_BRIGHT
local bg=$BLUE
if [[ ! -z "$SSH_CLIENT" ]]; then
local show_host="y"
bg=$CYAN
fi
if [ -z "$(id -u "$USER")" ]; then
bg=$RED
fi
if [[ ! -z "${SHOW_USER+x}" || ( ! -z "${DEFAULT_USER+x}" && "$DEFAULT_USER" != "$(whoami)" ) ]]; then
local show_user="y"
fi
local text
if [ ! -z ${show_user+x} ]; then
text+="$BOLD$(whoami)"
fi
if [ ! -z ${show_host+x} ]; then
if [ ! -z ${text+x} ]; then
text+="@"
fi
text+="\h"
fi
if [ ! -z ${text+x} ]; then
__prompt_block $bg $fg $text
fi
}
__status_block() {
local text
if [ $exit_code != 0 ]; then
__prompt_block $BLACK $RED '✘'
text+=$__block_text
fi
if [ "$(id -u "$USER")" == 0 ]; then
__prompt_block $BLACK $YELLOW '⚡'
text+=$__block_text
fi
if [ "$(jobs -l | wc -l)" != 0 ]; then
__prompt_block $BLACK $CYAN '⚙'
text+=$__block_text
fi
if [ ! -z "$text" ]; then
__block_text=$text
else
__block_text=''
fi
}
# Build the prompt
prompt() {
# I don't like bash; execute first to capture correct status code
local exit_code=$?
$(history -a ; history -n)
last_bg=''
PS1=''
__status_block
PS1+=$__block_text
__virtualenv_block
PS1+=$__block_text
__user_block
PS1+=$__block_text
__pwd_block
PS1+=$__block_text
__git_block
PS1+=$__block_text
__end_block
PS1+=$__block_text
}
PROMPT_COMMAND=prompt
}
__powerline
unset __powerline