-
Notifications
You must be signed in to change notification settings - Fork 2
/
bashline.sh
435 lines (394 loc) · 13.9 KB
/
bashline.sh
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# My bash prompt (heavily modifed from PureLine)...
# -----------------------------------------------------------------------------
# returns a string with the powerline symbol for a section end
# arg: $1 is foreground color of the next section
# arg: $2 is background color of the next section
function section_end {
if [ "$__last_color" == "$2" ]; then
# Section colors are the same, use a foreground separator
local end_char="${PL_SYMBOLS[soft_separator]}"
local fg="$1"
else
# section colors are different, use a background separator
local end_char="${PL_SYMBOLS[hard_separator]}"
local fg="$__last_color"
fi
if [ -n "$__last_color" ]; then
echo "${PL_COLORS[$fg]}${PL_COLORS[On_$2]}$end_char"
fi
}
# -----------------------------------------------------------------------------
# returns a string with background and foreground colours set
# arg: $1 foreground color
# arg: $2 background color
# arg: $3 content
function section_content {
echo "${PL_COLORS[$1]}${PL_COLORS[On_$2]}$3"
}
#------------------------------------------------------------------------------
# Helper function for User & ssh modules
function ip_address {
echo "$(ip route get 1 | tr -s ' ' | cut -d' ' -f7)"
}
#------------------------------------------------------------------------------
# Helper function to return normal or super user prompt character
function prompt_char {
[[ ${EUID} -eq 0 ]] && echo "#" || echo "$"
}
# -----------------------------------------------------------------------------
# append to prompt: current time
# arg: $1 foreground color
# arg: $2 background color
# optional variables;
# PL_TIME_SHOW_SECONDS: true/false for hh:mm:ss / hh:mm
function time_module {
local bg_color="$1"
local fg_color="$2"
if [ "$PL_TIME_SHOW_SECONDS" = true ]; then
local content="\t"
else
local content="\A"
fi
PS1+="$(section_end $fg_color $bg_color)"
PS1+="$(section_content $fg_color $bg_color " $content ")"
__last_color="$bg_color"
}
#------------------------------------------------------------------------------
# append to prompt: user@host or user or root@host
# arg: $1 foreground color
# arg: $2 background color
# option variables;
# PL_USER_SHOW_HOST: true/false to show host name/ip
# PL_USER_USE_IP: true/false to show IP instead of hostname
function user_module {
local bg_color="$1"
local fg_color="$2"
local content="\u"
# Show host if true or when user is remote/root
if [ "$PL_USER_SHOW_HOST" = true ]; then
if [ "$PL_USER_USE_IP" = true ]; then
content+="@$(ip_address)"
else
content+="@\h"
fi
fi
PS1+="$(section_end $fg_color $bg_color)"
PS1+="$(section_content $fg_color $bg_color " $content ")"
__last_color="$bg_color"
}
function host_module {
local bg_color="$1"
local fg_color="$2"
content="\h"
PS1+="$(section_end $fg_color $bg_color)"
PS1+="$(section_content $fg_color $bg_color " $content ")"
__last_color="$bg_color"
}
# -----------------------------------------------------------------------------
# append to prompt: indicate if SSH session
# arg: $1 foreground color
# arg: $2 background color
# option variables;
# PL_SSH_SHOW_HOST: true/false to show host name/ip
# PL_SSH_USE_IP: true/false to show IP instead of hostname
function ssh_module {
if [[ "${SSH_CLIENT}" || "${SSH_TTY}" ]]; then
local bg_color="$1"
local fg_color="$2"
local content="${PL_SYMBOLS[ssh]}"
if [ "$PL_SSH_SHOW_HOST" = true ]; then
if [ "$PL_SSH_USE_IP" = true ]; then
content+=" $(ip_address)"
else
content+=" \h"
fi
fi
PS1+="$(section_end $fg_color $bg_color)"
PS1+="$(section_content $fg_color $bg_color " $content ")"
__last_color="$bg_color"
fi
}
# -----------------------------------------------------------------------------
# append to prompt: current directory
# arg: $1 foreground color
# arg; $2 background color
# option variables;
# PL_PATH_TRIM: 0—fullpath, 1—current dir, [x]—trim to x number of dir
function path_module {
local bg_color="$1"
local fg_color="$2"
local content="\w"
if [ "$PL_PATH_TRIM" -eq 1 ]; then
local content="\W"
elif [ "$PL_PATH_TRIM" -gt 1 ]; then
PROMPT_DIRTRIM="$PL_PATH_TRIM"
fi
PS1+="$(section_end $fg_color $bg_color)"
PS1+="$(section_content $fg_color $bg_color " $content ")"
__last_color="$bg_color"
}
# -----------------------------------------------------------------------------
# append to prompt: the number of background jobs running
# arg: $1 foreground color
# arg; $2 background color
function background_jobs_module {
local bg_color="$1"
local fg_color="$2"
local number_jobs=$(jobs -p | wc -l)
if [ ! "$number_jobs" -eq 0 ]; then
PS1+="$(section_end $fg_color $bg_color)"
PS1+="$(section_content $fg_color $bg_color " ${PL_SYMBOLS[background_jobs]} $number_jobs ")"
__last_color="$bg_color"
fi
}
# -----------------------------------------------------------------------------
# append to prompt: git branch with indictors for;
# number of; modified files, staged files and conflicts
# arg: $1 foreground color
# arg; $2 background color
# option variables;
# PL_GIT_STASH: true/false
# PL_GIT_AHEAD: true/false
# PL_GIT_STAGED: true/false
# PL_GIT_CONFLICTS: true/false
# PL_GIT_MODIFIED: true/false
# PL_GIT_UNTRACKED: true/false
# PL_GIT_DIRTY_FG: <color>
# PL_GIT_DIRTY_BG: <color>
function git_module {
local git_branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
if [ -n "$git_branch" ]; then
local bg_color="$1"
local fg_color="$2"
local content="${PL_SYMBOLS[git_branch]} $(basename $(git rev-parse --show-toplevel))[$git_branch]"
if [ "$PL_GIT_STASH" = true ]; then
local number_stash="$(git stash list 2>/dev/null | wc -l)"
if [ ! "$number_stash" -eq 0 ]; then
content+="${PL_SYMBOLS[git_stash]}$number_stash"
fi
fi
if [ "$PL_GIT_AHEAD" = true ]; then
local number_behind_ahead="$(git rev-list --count --left-right '@{upstream}...HEAD' 2>/dev/null)"
local number_ahead="${number_behind_ahead#* }"
local number_behind="${number_behind_ahead% *}"
if [ ! "0$number_ahead" -eq 0 -o ! "0$number_behind" -eq 0 ]; then
if [ ! "$number_ahead" -eq 0 ]; then
content+="${PL_SYMBOLS[git_ahead]}$number_ahead"
fi
if [ ! "$number_behind" -eq 0 ]; then
content+="${PL_SYMBOLS[git_behind]}$number_behind"
fi
fi
fi
if [ "$PL_GIT_STAGED" = true ]; then
local number_staged="$(git diff --staged --name-only --diff-filter=AM 2> /dev/null | wc -l)"
if [ ! "$number_staged" -eq "0" ]; then
content+=" ${PL_SYMBOLS[soft_separator]} ${PL_SYMBOLS[git_staged]}$number_staged"
fi
fi
if [ "$PL_GIT_CONFLICTS" = true ]; then
local number_conflicts="$(git diff --name-only --diff-filter=U 2> /dev/null | wc -l)"
if [ ! "$number_conflicts" -eq "0" ]; then
content+=" ${PL_SYMBOLS[soft_separator]} ${PL_SYMBOLS[git_conflicts]}$number_conflicts"
fi
fi
if [ "$PL_GIT_MODIFIED" = true ]; then
local number_modified="$(git diff --name-only --diff-filter=M 2> /dev/null | wc -l )"
if [ ! "$number_modified" -eq "0" ]; then
content+=" ${PL_SYMBOLS[soft_separator]} ${PL_SYMBOLS[git_modified]}$number_modified"
fi
fi
if [ "$PL_GIT_UNTRACKED" = true ]; then
local number_untracked="$(git ls-files --other --exclude-standard | wc -l)"
if [ ! "$number_untracked" -eq "0" ]; then
content+=" ${PL_SYMBOLS[soft_separator]} ${PL_SYMBOLS[git_untracked]}$number_untracked"
fi
fi
if [ -n "$PL_GIT_DIRTY_FG" -o -n "$PL_GIT_DIRTY_BG" ]; then
if [ -n "$(git status --porcelain)" ]; then
if [ -n "$PL_GIT_DIRTY_FG" ]; then
fg_color="$PL_GIT_DIRTY_FG"
fi
if [ -n "$PL_GIT_DIRTY_BG" ]; then
bg_color="$PL_GIT_DIRTY_BG"
fi
fi
fi
PS1+="$(section_end $fg_color $bg_color)"
PS1+="$(section_content $fg_color $bg_color " $content ")"
__last_color="$bg_color"
fi
}
# -----------------------------------------------------------------------------
# append to prompt: append a '$' prompt with optional return code for previous command
# arg: $1 foreground color
# arg; $2 background color
function prompt_module {
local bg_color="$1"
local fg_color="$2"
local content=" $(prompt_char) "
PS1+="$(section_end $fg_color $bg_color)"
PS1+="$(section_content $fg_color $bg_color "$content")"
__last_color="$bg_color"
}
# -----------------------------------------------------------------------------
# append to prompt: append a '$' prompt with optional return code for previous command
# arg: $1 foreground color
# arg; $2 background color
# option variables;
# PL_SHOW_RETURN_CODE: true/false
function return_code_module {
if [ ! "$__return_code" -eq 0 ]; then
local bg_color="$1"
local fg_color="$2"
local content=" ${PL_SYMBOLS[return_code]} "
if [ "$PL_SHOW_RETURN_CODE" = true ]; then
content+="$__return_code "
fi
PS1+="$(section_end $fg_color $bg_color)"
PS1+="$(section_content $fg_color $bg_color "$content")"
__last_color="$bg_color"
fi
}
# -----------------------------------------------------------------------------
function bashline_ps1 {
__return_code=$? # save the return code
PS1="" # reset the command prompt
# load the modules
for module in "${!PL_MODULES[@]}"; do
${PL_MODULES[$module]}
done
# final end point
if [ -n "$__last_color" ]; then
PS1+="$(section_end $__last_color 'Default')"
else
# No modules loaded, set a basic prompt
PS1="PL | No Modules Loaded: $(prompt_char)"
fi
# cleanup
PS1+="${PL_COLORS[Color_Off]}"
if [ "$PL_ERASE_TO_EOL" = true ]; then
PS1+="\[\e[K\]"
fi
PS1+=" "
unset __last_color
unset __return_code
}
# -----------------------------------------------------------------------------
# most basic system colors
declare -A PL_COLORS=(
[Color_Off]='\[\e[0m\]' # Text Reset
# Foreground
[Default]='\[\e[0;39m\]'
[Black]='\[\e[0;30m\]'
[Red]='\[\e[0;31m\]'
[Green]='\[\e[0;32m\]'
[Yellow]='\[\e[0;33m\]'
[Blue]='\[\e[0;34m\]'
[Purple]='\[\e[0;35m\]'
[Cyan]='\[\e[0;36m\]'
[Orange]='\[\e[0;33m\]'
[White]='\[\e[0;37m\]'
[LightGrey]='\[\e[1;30m\]'
[DarkGrey]='\[\e[0;30m\]'
# Background
[On_Default]='\[\e[49m\]'
[On_Black]='\[\e[40m\]'
[On_Red]='\[\e[41m\]'
[On_Green]='\[\e[42m\]'
[On_Yellow]='\[\e[43m\]'
[On_Blue]='\[\e[44m\]'
[On_Purple]='\[\e[45m\]'
[On_Cyan]='\[\e[46m\]'
[On_Orange]='\[\e[43m\]'
[On_White]='\[\e[47m\]'
[On_LightGrey]='\[\e[1;40m\]'
[On_DarkGrey]='\[\e[40m\]'
)
function colors_gruvbox {
# Foreground
PL_COLORS[Black]='\[\e[38;5;235m\]'
PL_COLORS[Red]='\[\e[38;5;124m\]'
PL_COLORS[Green]='\[\e[38;5;106m\]'
PL_COLORS[Yellow]='\[\e[38;5;172m\]'
PL_COLORS[Blue]='\[\e[38;5;66m\]'
PL_COLORS[Purple]='\[\e[38;5;132m\]'
PL_COLORS[Cyan]='\[\e[38;5;45m\]'
PL_COLORS[Orange]='\[\e[38;5;166m\]'
PL_COLORS[White]='\[\e[38;5;229m\]'
PL_COLORS[LightGrey]='\[\e[38;5;248m\]'
PL_COLORS[DarkGrey]='\[\e[38;5;239m\]'
# Background
PL_COLORS[On_Black]='\[\e[48;5;235m\]'
PL_COLORS[On_Red]='\[\e[48;5;124m\]'
PL_COLORS[On_Green]='\[\e[48;5;106m\]'
PL_COLORS[On_Yellow]='\[\e[48;5;172m\]'
PL_COLORS[On_Blue]='\[\e[48;5;66m\]'
PL_COLORS[On_Purple]='\[\e[48;5;132m\]'
PL_COLORS[On_Cyan]='\[\e[48;5;45m\]'
PL_COLORS[On_Orange]='\[\e[48;5;166m\]'
PL_COLORS[On_White]='\[\e[48;5;229m\]'
PL_COLORS[On_LightGrey]='\[\e[48;5;248m\]'
PL_COLORS[On_DarkGrey]='\[\e[48;5;239m\]'
}
if [ $(tput colors) -eq 256 ]; then
colors_gruvbox
#colors_nord
fi
# most basic symbols for 'out-of-the-box' compatibility
declare -A PL_SYMBOLS=(
#[hard_separator]="▶"
#[soft_separator]="│"
[hard_separator]=""
[soft_separator]=""
#[git_branch]="╬"
#[git_untracked]="?"
#[git_stash]="§"
#[git_ahead]="↑"
#[git_behind]="↓"
#[git_modified]="+"
#[git_staged]="•"
#[git_conflicts]="*"
[git_branch]=""
[git_untracked]="↔"
[git_stash]="§"
[git_ahead]="↑"
[git_behind]="↓"
[git_modified]="✚ "
[git_staged]="✔ "
[git_conflicts]="✘ "
#[ssh]="╤"
[ssh]=" "
#[return_code]="x"
[return_code]="⚑"
#[background_jobs]="↔"
[background_jobs]="⏎"
)
# All modules are enabled. Uncomment/comment to enable/disable a module
declare -a PL_MODULES=(
# Module Background Foreground
'time_module Purple Black'
'host_module Blue Black'
'ssh_module Yellow Black'
'git_module Orange Black'
'path_module DarkGrey LightGrey'
'background_jobs_module Purple White'
'return_code_module Red White'
#'prompt_module DarkGrey White'
)
# Module Options
PL_ERASE_TO_EOL=false
PL_PATH_TRIM=3 # 0 Full path, 1, Current, 2+ trim level
PL_TIME_SHOW_SECONDS=false
PL_USER_SHOW_HOST=true
PL_USER_USE_IP=false
PL_SSH_SHOW_HOST=true
PL_SSH_USE_IP=false
PL_GIT_STASH=false
PL_GIT_AHEAD=false
PL_GIT_STAGED=false
PL_GIT_CONFLICTS=false
PL_GIT_MODIFIED=false
PL_GIT_UNTRACKED=false
PROMPT_COMMAND="bashline_ps1"
# vim:ft=sh