|
@@ -0,0 +1,357 @@
|
|
1
|
+#!/usr/bin/env bash
|
|
2
|
+
|
|
3
|
+# Based on https://github.com/undu/bash-powerline
|
|
4
|
+
|
|
5
|
+__powerline() {
|
|
6
|
+
|
|
7
|
+ # User config variables,
|
|
8
|
+ # it's recommended to override those variables through .bashrc or similar
|
|
9
|
+ #
|
|
10
|
+ # Use powerline mode
|
|
11
|
+ # readonly POWERLINE_FONT=''
|
|
12
|
+ #
|
|
13
|
+ # Always show user in the prompt
|
|
14
|
+ # readonly SHOW_USER=''
|
|
15
|
+ #
|
|
16
|
+ # Never show a default user
|
|
17
|
+ # readonly DEFAULT_USER='user'
|
|
18
|
+
|
|
19
|
+ # Default background and foreground ANSI colours
|
|
20
|
+ readonly DEFAULT_BG=0
|
|
21
|
+ readonly DEFAULT_FG=7
|
|
22
|
+
|
|
23
|
+ # Max length of full path
|
|
24
|
+ readonly MAX_PATH_LENGTH=30
|
|
25
|
+
|
|
26
|
+ # Unicode symbols
|
|
27
|
+ if [ -z "${POWERLINE_FONT+x}" ]; then
|
|
28
|
+ readonly GIT_BRANCH_SYMBOL='⑂'
|
|
29
|
+ else
|
|
30
|
+ readonly GIT_BRANCH_SYMBOL=''
|
|
31
|
+ fi
|
|
32
|
+ readonly GIT_BRANCH_CHANGED_SYMBOL='Δ'
|
|
33
|
+ readonly GIT_NEED_PUSH_SYMBOL='↑'
|
|
34
|
+ readonly GIT_NEED_PULL_SYMBOL='↓'
|
|
35
|
+
|
|
36
|
+ # Powerline symbols
|
|
37
|
+ readonly BLOCK_START=''
|
|
38
|
+
|
|
39
|
+ # ANSI Colours
|
|
40
|
+ readonly BLACK=0
|
|
41
|
+ readonly RED=1
|
|
42
|
+ readonly GREEN=2
|
|
43
|
+ readonly YELLOW=3
|
|
44
|
+ readonly BLUE=4
|
|
45
|
+ readonly MAGENTA=5
|
|
46
|
+ readonly CYAN=6
|
|
47
|
+ readonly WHITE=7
|
|
48
|
+
|
|
49
|
+ readonly BLACK_BRIGHT=8
|
|
50
|
+ readonly RED_BRIGHT=9
|
|
51
|
+ readonly GREEN_BRIGHT=10
|
|
52
|
+ readonly YELLOW_BRIGHT=11
|
|
53
|
+ readonly BLUE_BRIGHT=12
|
|
54
|
+ readonly MAGENTA_BRIGHT=13
|
|
55
|
+ readonly CYAN_BRIGHT=14
|
|
56
|
+ readonly WHITE_BRIGHT=15
|
|
57
|
+
|
|
58
|
+ # Font effects
|
|
59
|
+ readonly DIM="\[$(tput dim)\]"
|
|
60
|
+ readonly REVERSE="\[$(tput rev)\]"
|
|
61
|
+ readonly RESET="\[$(tput sgr0)\]"
|
|
62
|
+ readonly BOLD="\[$(tput bold)\]"
|
|
63
|
+
|
|
64
|
+ # Generate terminal colour codes
|
|
65
|
+ # $1 is an int (a colour) and $2 must be 'fg' or 'bg'
|
|
66
|
+ __colour() {
|
|
67
|
+ case "$2" in
|
|
68
|
+ 'fg'*)
|
|
69
|
+ echo "\[$(tput setaf "$1")\]"
|
|
70
|
+ ;;
|
|
71
|
+ 'bg'*)
|
|
72
|
+ echo "\[$(tput setab "$1")\]"
|
|
73
|
+ ;;
|
|
74
|
+ *)
|
|
75
|
+ echo "\[$(tput setab "$1")\]"
|
|
76
|
+ ;;
|
|
77
|
+ esac
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ # Generate a single-coloured block for the prompt
|
|
81
|
+ __prompt_block() {
|
|
82
|
+ local bg; local fg
|
|
83
|
+ if [ ! -z "${1+x}" ]; then
|
|
84
|
+ bg=$1
|
|
85
|
+ else
|
|
86
|
+ if [ ! -z "$last_bg" ]; then
|
|
87
|
+ bg=$last_bg
|
|
88
|
+ else
|
|
89
|
+ bg=$DEFAULT_BG
|
|
90
|
+ fi
|
|
91
|
+ fi
|
|
92
|
+ if [ ! -z "${2+x}" ]; then
|
|
93
|
+ fg=$2
|
|
94
|
+ else
|
|
95
|
+ fg=$DEFAULT_FG
|
|
96
|
+ fi
|
|
97
|
+
|
|
98
|
+ local block
|
|
99
|
+
|
|
100
|
+ # Need to generate a separator if the background changes
|
|
101
|
+ if [[ ! -z "$last_bg" && "$bg" != "$last_bg" && ! -z "${POWERLINE_FONT+x}" ]]; then
|
|
102
|
+ block+="$(__colour "$bg" 'bg')"
|
|
103
|
+ block+="$(__colour "$last_bg" 'fg')"
|
|
104
|
+ block+="$BLOCK_START $RESET"
|
|
105
|
+ block+="$(__colour "$bg" 'bg')"
|
|
106
|
+ block+="$(__colour "$fg" 'fg')"
|
|
107
|
+ else
|
|
108
|
+ block+="$(__colour "$bg" 'bg')"
|
|
109
|
+ block+="$(__colour "$fg" 'fg')"
|
|
110
|
+ block+=" "
|
|
111
|
+ fi
|
|
112
|
+
|
|
113
|
+ if [ ! -z "${3+x}" ]; then
|
|
114
|
+ block+="$3 $RESET"
|
|
115
|
+ fi
|
|
116
|
+
|
|
117
|
+ last_bg=$bg
|
|
118
|
+
|
|
119
|
+ __block_text="$block"
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ function __end_block() {
|
|
123
|
+ __block_text=''
|
|
124
|
+ if [ ! -z "$last_bg" ]; then
|
|
125
|
+ if [ ! -z "${POWERLINE_FONT+x}" ]; then
|
|
126
|
+ __block_text+="$(__colour $DEFAULT_BG 'bg')"
|
|
127
|
+ __block_text+="$(__colour "$last_bg" 'fg')"
|
|
128
|
+ __block_text+="$BLOCK_START$RESET"
|
|
129
|
+ __block_text+="$(__colour $DEFAULT_BG 'bg')"
|
|
130
|
+ __block_text+="$(__colour "$DEFAULT_FG" 'fg')"
|
|
131
|
+ else
|
|
132
|
+ __block_text+="$(__colour $DEFAULT_BG 'bg')"
|
|
133
|
+ __block_text+="$(__colour "$DEFAULT_FG" 'fg')"
|
|
134
|
+ fi
|
|
135
|
+ fi
|
|
136
|
+ __block_text+=' '
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ ### Prompt components
|
|
140
|
+
|
|
141
|
+ __git_block() {
|
|
142
|
+ if ! command -V git > /dev/null; then
|
|
143
|
+ # git not found
|
|
144
|
+ __block_text=''
|
|
145
|
+ return
|
|
146
|
+ fi
|
|
147
|
+ # force git output in English to make our work easier
|
|
148
|
+ local git_eng="env LANG=C git"
|
|
149
|
+
|
|
150
|
+ # check if pwd is under git
|
|
151
|
+ if ! git rev-parse --is-inside-git-dir > /dev/null 2> /dev/null; then
|
|
152
|
+ # not in a git repo, bail out
|
|
153
|
+ __block_text=''
|
|
154
|
+ return
|
|
155
|
+ fi
|
|
156
|
+
|
|
157
|
+ # get current branch name or short SHA1 hash for detached head
|
|
158
|
+ local branch; local ref_symbol
|
|
159
|
+ branch="$($git_eng symbolic-ref --short HEAD 2>/dev/null)"
|
|
160
|
+ # shellcheck disable=SC2181
|
|
161
|
+ if [ $? != 0 ]; then
|
|
162
|
+ branch="$($git_eng describe --tags --always 2>/dev/null)"
|
|
163
|
+ ref_symbol='➦'
|
|
164
|
+ else
|
|
165
|
+ ref_symbol=$GIT_BRANCH_SYMBOL
|
|
166
|
+ fi
|
|
167
|
+
|
|
168
|
+ # In pcmode (and only pcmode) the contents of
|
|
169
|
+ # $gitstring are subject to expansion by the shell.
|
|
170
|
+ # Avoid putting the raw ref name in the prompt to
|
|
171
|
+ # protect the user from arbitrary code execution via
|
|
172
|
+ # specially crafted ref names (e.g., a ref named
|
|
173
|
+ # '$(IFS=_;cmd=sudo_rm_-rf_/;$cmd)' would execute
|
|
174
|
+ # 'sudo rm -rf /' when the prompt is drawn). Instead,
|
|
175
|
+ # put the ref name in a new global variable (in the
|
|
176
|
+ # __git_ps1_* namespace to avoid colliding with the
|
|
177
|
+ # user's environment) and reference that variable from
|
|
178
|
+ # PS1.
|
|
179
|
+ # note that the $ is escaped -- the variable will be
|
|
180
|
+ # expanded later (when it's time to draw the prompt)
|
|
181
|
+ if shopt -q promptvars; then
|
|
182
|
+ export __git_ps1_block="$branch"
|
|
183
|
+ ref="$ref_symbol \${__git_ps1_block}"
|
|
184
|
+ else
|
|
185
|
+ ref="$ref_symbol $branch"
|
|
186
|
+ fi
|
|
187
|
+
|
|
188
|
+ local marks
|
|
189
|
+
|
|
190
|
+ # check if HEAD is dirty
|
|
191
|
+ if [ -n "$($git_eng status --porcelain 2>/dev/null)" ]; then
|
|
192
|
+ dirty='y'
|
|
193
|
+ marks+=" $GIT_BRANCH_CHANGED_SYMBOL"
|
|
194
|
+ fi
|
|
195
|
+
|
|
196
|
+ # how many commits local branch is ahead/behind of remote?
|
|
197
|
+ local stat; local aheadN; local behindN
|
|
198
|
+ stat="$($git_eng status --porcelain --branch 2>/dev/null | grep '^##' | grep -o '\[.\+\]$')"
|
|
199
|
+ aheadN="$(echo "$stat" | grep -o 'ahead [[:digit:]]\+' | grep -o '[[:digit:]]\+')"
|
|
200
|
+ behindN="$(echo "$stat" | grep -o 'behind [[:digit:]]\+' | grep -o '[[:digit:]]\+')"
|
|
201
|
+ [ -n "$aheadN" ] && marks+=" $GIT_NEED_PUSH_SYMBOL$aheadN"
|
|
202
|
+ [ -n "$behindN" ] && marks+=" $GIT_NEED_PULL_SYMBOL$behindN"
|
|
203
|
+
|
|
204
|
+ local bg; local fg
|
|
205
|
+ fg=$BLACK
|
|
206
|
+ if [ -z "$dirty" ]; then
|
|
207
|
+ bg=$GREEN
|
|
208
|
+ else
|
|
209
|
+ bg=$YELLOW
|
|
210
|
+ fi
|
|
211
|
+
|
|
212
|
+ __prompt_block $bg $fg "$ref$marks"
|
|
213
|
+ }
|
|
214
|
+
|
|
215
|
+ __virtualenv_block() {
|
|
216
|
+ # Copied from Python virtualenv's activate.sh script.
|
|
217
|
+ # https://github.com/pypa/virtualenv/blob/a9b4e673559a5beb24bac1a8fb81446dd84ec6ed/virtualenv_embedded/activate.sh#L62
|
|
218
|
+ # License: MIT
|
|
219
|
+ if [ -n "$VIRTUAL_ENV" ]; then
|
|
220
|
+ local venv
|
|
221
|
+ # In pcmode (and only pcmode) the contents of
|
|
222
|
+ # $gitstring are subject to expansion by the shell.
|
|
223
|
+ # Avoid putting the raw ref name in the prompt to
|
|
224
|
+ # protect the user from arbitrary code execution via
|
|
225
|
+ # specially crafted ref names (e.g., a ref named
|
|
226
|
+ # '$(IFS=_;cmd=sudo_rm_-rf_/;$cmd)' would execute
|
|
227
|
+ # 'sudo rm -rf /' when the prompt is drawn). Instead,
|
|
228
|
+ # put the ref name in a new global variable (in the
|
|
229
|
+ # __git_ps1_* namespace to avoid colliding with the
|
|
230
|
+ # user's environment) and reference that variable from
|
|
231
|
+ # PS1.
|
|
232
|
+ # note that the $ is escaped -- the variable will be
|
|
233
|
+ # expanded later (when it's time to draw the prompt)
|
|
234
|
+ if shopt -q promptvars; then
|
|
235
|
+ export __venv_ps1_block
|
|
236
|
+ __venv_ps1_block=$(basename "$VIRTUAL_ENV")
|
|
237
|
+ venv="$ref_symbol \${__venv_ps1_block}"
|
|
238
|
+ else
|
|
239
|
+ venv="$(basename "$VIRTUAL_ENV")"
|
|
240
|
+ fi
|
|
241
|
+ __prompt_block $WHITE $BLACK "$venv"
|
|
242
|
+ else
|
|
243
|
+ __block_text=''
|
|
244
|
+ fi
|
|
245
|
+ }
|
|
246
|
+
|
|
247
|
+ __pwd_block() {
|
|
248
|
+ # Use ~ to represent $HOME prefix
|
|
249
|
+ local pwd; pwd=$(pwd | sed -e "s|^$HOME|~|")
|
|
250
|
+ # shellcheck disable=SC1001,SC2088
|
|
251
|
+ if [[ ( $pwd = ~\/*\/* || $pwd = \/*\/*/* ) && ${#pwd} -gt $MAX_PATH_LENGTH ]]; then
|
|
252
|
+ local IFS='/'
|
|
253
|
+ read -ra split <<< "$pwd"
|
|
254
|
+ if [[ $pwd = ~* ]]; then
|
|
255
|
+ pwd="~/${split[1]}/.../${split[*]:(-2):1}/${split[*]:(-1)}"
|
|
256
|
+ else
|
|
257
|
+ pwd="/${split[1]}/.../${split[*]:(-2):1}/${split[*]:(-1)}"
|
|
258
|
+ fi
|
|
259
|
+ fi
|
|
260
|
+ __prompt_block $BLACK_BRIGHT $WHITE_BRIGHT "$pwd"
|
|
261
|
+ }
|
|
262
|
+
|
|
263
|
+ # superuser or not, here I go!
|
|
264
|
+ __user_block() {
|
|
265
|
+ # Colours to use
|
|
266
|
+ local fg=$WHITE_BRIGHT
|
|
267
|
+ local bg=$BLUE
|
|
268
|
+
|
|
269
|
+ if [[ ! -z "$SSH_CLIENT" ]]; then
|
|
270
|
+ local show_host="y"
|
|
271
|
+ bg=$CYAN
|
|
272
|
+ fi
|
|
273
|
+
|
|
274
|
+ if [ -z "$(id -u "$USER")" ]; then
|
|
275
|
+ bg=$RED
|
|
276
|
+ fi
|
|
277
|
+
|
|
278
|
+ # shellcheck disable=SC2153
|
|
279
|
+ if [[ ! -z "${SHOW_USER+x}" || ( ! -z "${DEFAULT_USER+x}" && "$DEFAULT_USER" != "$(whoami)" ) ]]; then
|
|
280
|
+ local show_user="y"
|
|
281
|
+ fi
|
|
282
|
+
|
|
283
|
+ local text
|
|
284
|
+ if [ ! -z ${show_user+x} ]; then
|
|
285
|
+ text+="$BOLD$(whoami)"
|
|
286
|
+ fi
|
|
287
|
+ if [ ! -z ${show_host+x} ]; then
|
|
288
|
+ if [ ! -z "${text+x}" ]; then
|
|
289
|
+ text+="@"
|
|
290
|
+ fi
|
|
291
|
+ text+="\h"
|
|
292
|
+ fi
|
|
293
|
+
|
|
294
|
+ if [ ! -z ${text+x} ]; then
|
|
295
|
+ __prompt_block $bg $fg $text
|
|
296
|
+ fi
|
|
297
|
+ }
|
|
298
|
+
|
|
299
|
+ __status_block() {
|
|
300
|
+ local text
|
|
301
|
+ if [ "$exit_code" != 0 ]; then
|
|
302
|
+ __prompt_block $BLACK $RED '✘'
|
|
303
|
+ text+=$__block_text
|
|
304
|
+ fi
|
|
305
|
+
|
|
306
|
+ if [ "$(id -u "$USER")" == 0 ]; then
|
|
307
|
+ __prompt_block $BLACK $YELLOW '⚡'
|
|
308
|
+ text+=$__block_text
|
|
309
|
+ fi
|
|
310
|
+
|
|
311
|
+ if [ "$(jobs -l | wc -l)" != 0 ]; then
|
|
312
|
+ __prompt_block $BLACK $CYAN '⚙'
|
|
313
|
+ text+=$__block_text
|
|
314
|
+ fi
|
|
315
|
+
|
|
316
|
+ if [ ! -z "$text" ]; then
|
|
317
|
+ __block_text=$text
|
|
318
|
+ else
|
|
319
|
+ __block_text=''
|
|
320
|
+ fi
|
|
321
|
+ }
|
|
322
|
+
|
|
323
|
+ # Build the prompt
|
|
324
|
+ prompt() {
|
|
325
|
+ # I don't like bash; execute first to capture correct status code
|
|
326
|
+ local exit_code=$?
|
|
327
|
+ # shellcheck disable=SC2091
|
|
328
|
+ $(history -a ; history -n)
|
|
329
|
+
|
|
330
|
+ last_bg=''
|
|
331
|
+
|
|
332
|
+ PS1=''
|
|
333
|
+
|
|
334
|
+ __status_block
|
|
335
|
+ PS1+=$__block_text
|
|
336
|
+
|
|
337
|
+ __virtualenv_block
|
|
338
|
+ PS1+=$__block_text
|
|
339
|
+
|
|
340
|
+ __user_block
|
|
341
|
+ PS1+=$__block_text
|
|
342
|
+
|
|
343
|
+ __pwd_block
|
|
344
|
+ PS1+=$__block_text
|
|
345
|
+
|
|
346
|
+ __git_block
|
|
347
|
+ PS1+=$__block_text
|
|
348
|
+
|
|
349
|
+ __end_block
|
|
350
|
+ PS1+=$__block_text
|
|
351
|
+ }
|
|
352
|
+
|
|
353
|
+ PROMPT_COMMAND=prompt
|
|
354
|
+}
|
|
355
|
+
|
|
356
|
+__powerline
|
|
357
|
+unset __powerline
|