-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpureline
675 lines (600 loc) · 23.8 KB
/
pureline
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#!/usr/bin/env bash
# PureLine - A Pure Bash Powerline PS1 Command Prompt
# clear all variables and declare to prevent issues when re-sourcing
unset PL_SYMBOLS; declare -A PL_SYMBOLS # Hash table to reference symbols
unset PL_COLORS; declare -A PL_COLORS # Hash table to reference colors
unset PL_SEGMENTS; declare -a PL_SEGMENTS # Array to hold segments and their arguments
# -----------------------------------------------------------------------------
# returns a string with the powerline symbol for a segment end
# arg: $1 is foreground color of the next segment
# arg: $2 is background color of the next segment
function segment_end {
local end_char
local fg
if [ "$__last_color" == "$2" ]; then
# segment colors are the same, use a foreground separator
end_char="${PL_SYMBOLS[soft_separator]}"
fg="$1"
else
# segment colors are different, use a background separator
end_char="${PL_SYMBOLS[hard_separator]}"
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 segment_content {
__last_color="$2"
echo "${PL_COLORS[$1]}${PL_COLORS[On_$2]}$3"
}
#------------------------------------------------------------------------------
# Helper function for User segment - also used in external ssh segment
function ip_address {
local ip_address
local ip_loc
local ifconfig_loc
if ip_loc="$(type -p "ip")" || [[ -n $ip_loc ]]; then
ip_address="$(ip route get 1 | tr -s ' ' | cut -d' ' -f7)"
elif ifconfig_loc="$(type -p "ifconfig")" || [[ -n $ifconfig_loc ]]; then
while IFS=$': \t' read -ra _line ;do
[ -z "${_line%inet}" ] &&
_ip=${_line[${#_line[1]}>4?1:2]} &&
[ "${_ip#127.0.0.1}" ] && ip_address=$_ip
done< <(LANG=C /sbin/ifconfig)
else
ip_address="127.0.0.1"
fi
echo $ip_address
}
#function ip_address {
# if [[ ${3} == "" ]]; then
# ip_ro=$(ip route get 1 | tr -s ' ' | cut -d' ' -f7)
# echo "$(curl -s -m 10 https://ifconfig.me || echo LOCAL ${ip_ro})"
# else
# echo "${3}"
# fi
#}
#------------------------------------------------------------------------------
# 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 background color
# arg: $2 foreground color
# optional variables;
# PL_TIME_SHOW_SECONDS: true/false for hh:mm:ss / hh:mm
function time_segment {
local bg_color="$1"
local fg_color="$2"
if [ "$PL_TIME_SHOW_SECONDS" = true ]; then
local content="\t"
else
local content="\A"
fi
PS1+="$(segment_end "$fg_color" "$bg_color")"
PS1+="$(segment_content "$fg_color" "$bg_color" " $content ")"
__last_color="$bg_color"
}
#------------------------------------------------------------------------------
# append to prompt: user@host or user or root@host
# arg: $1 background color
# arg: $2 foreground 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_segment {
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+="@$(hostname -s| tr '[:upper:]' '[:lower:]')"
fi
fi
PS1+="$(segment_end "$fg_color" "$bg_color")"
PS1+="$(segment_content "$fg_color" "$bg_color" " $content ")"
__last_color="$bg_color"
}
# -----------------------------------------------------------------------------
# append to prompt: current directory
# arg: $1 background color
# arg: $2 foreground color
# option variables;
# PL_PATH_TRIM: 0—fullpath, 1—current dir, [x]—trim to x number of dir
function path_segment {
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+="$(segment_end "$fg_color" "$bg_color")"
PS1+="$(segment_content "$fg_color" "$bg_color" " $content ")"
__last_color="$bg_color"
}
# -----------------------------------------------------------------------------
# append to prompt: the number of background jobs running
# arg: $1 background color
# arg: $2 foreground color
function background_jobs_segment {
local bg_color="$1"
local fg_color="$2"
local number_jobs
number_jobs=$(jobs | grep -cv "Done" | tr -d '[:space:]')
if [ ! "$number_jobs" -eq 0 ]; then
PS1+="$(segment_end "$fg_color" "$bg_color")"
PS1+="$(segment_content "$fg_color" "$bg_color" " ${PL_SYMBOLS[background_jobs]} $number_jobs ")"
__last_color="$bg_color"
fi
}
# -----------------------------------------------------------------------------
# append to prompt: indicator is the current directory is ready-only
# arg: $1 background color
# arg: $2 foreground color
function read_only_segment {
local bg_color="$1"
local fg_color="$2"
if [ ! -w "$PWD" ]; then
PS1+="$(segment_end "$fg_color" "$bg_color")"
PS1+="$(segment_content "$fg_color" "$bg_color" " ${PL_SYMBOLS[read_only]} ")"
__last_color="$bg_color"
fi
}
# -----------------------------------------------------------------------------
# append to prompt: append the normal '$' or super-user '#' prompt character
# arg: $1 background color
# arg: $2 foreground color
# option variables;
# PL_PROMPT_SHOW_SHLVL: true/relative/false to show the shell level
# true Show the value of $SHLVL
# relative Show the shell level relatively to the first shell sourcing pureline.
# Useful when that first shell is already a sub-shell,
# like in vscode integrated terminals.
# false Show nothing
function prompt_segment {
local bg_color="$1"
local fg_color="$2"
if [[ -n $PL_PROMPT_SHOW_SHLVL ]]; then
# create local variable 'shell_level' ...
if [[ $PL_PROMPT_SHOW_SHLVL == true ]]; then
local shell_level=$SHLVL
elif [[ $PL_PROMPT_SHOW_SHLVL == relative ]]; then
[[ -v __pl_starting_shlvl ]] || export __pl_starting_shlvl=$SHLVL
local shell_level=$((SHLVL - __pl_starting_shlvl + 1))
fi
# ... except if its value is 1
((shell_level != 1)) || unset shell_level
fi
local content
content=" ${shell_level:-}$(prompt_char) "
if [ ${EUID} -eq 0 ]; then
if [ -n "$PL_PROMPT_ROOT_FG" ]; then
fg_color="$PL_PROMPT_ROOT_FG"
fi
if [ -n "$PL_PROMPT_ROOT_BG" ]; then
bg_color="$PL_PROMPT_ROOT_BG"
fi
fi
PS1+="$(segment_end "$fg_color" "$bg_color")"
PS1+="$(segment_content "$fg_color" "$bg_color" "$content")"
__last_color="$bg_color"
}
# -----------------------------------------------------------------------------
# append to prompt: return code for previous command
# arg: $1 background color
# arg: $2 foreground color
function return_code_segment {
if [ ! "$__return_code" -eq 0 ]; then
local bg_color="$1"
local fg_color="$2"
local content=" ${PL_SYMBOLS[return_code]} $__return_code "
PS1+="$(segment_end "$fg_color" "$bg_color")"
PS1+="$(segment_content "$fg_color" "$bg_color" "$content")"
__last_color="$bg_color"
fi
}
# -----------------------------------------------------------------------------
# append to prompt: end the current promptline and start a newline
function newline_segment {
if [ -n "$__last_color" ]; then
PS1+="$(segment_end "$__last_color" 'Default')"
fi
PS1+="\n"
unset __last_color
}
# -----------------------------------------------------------------------------
# code to run before processing the inherited $PROMPT_COMMAND
function __pureline_pre {
__return_code=$? # save return code of last command
if [[ -n $PL_TITLEBAR ]]; then
if (( ${BASH_VERSINFO[0]:-0} > 4 || (${BASH_VERSINFO[0]:-0} == 4 && ${BASH_VERSINFO[1]:-0} >= 4) )); then
# since bash 4.4, @P allows variable expansion as if it were a prompt string (like PS1)
echo -ne "\e]2;${PL_TITLEBAR@P}\a" # set the gui window title
else
echo -ne "\e]2;'${PL_TITLEBAR}'\a" # set the gui window title
fi
fi
return $__return_code # forward it to the inherited $PROMPT_COMMAND
}
# -----------------------------------------------------------------------------
# code to run after processing the inherited $PROMPT_COMMAND
function __pureline_post {
local segment_index
PS1="" # reset the command prompt
# load the segments
for segment_index in "${!PL_SEGMENTS[@]}"; do
${PL_SEGMENTS[$segment_index]}
done
# final end point
if ((${#PL_SEGMENTS[@]} > 0)); then
PS1+="$(segment_end "$__last_color" 'Default') "
else
# No segments loaded, set a basic prompt
PS1="PL | No segments Loaded: $(prompt_char)"
fi
# cleanup
PS1+="${PL_COLORS[Color_Off]}"
if [ "$PL_ERASE_TO_EOL" = true ]; then
PS1+="\[\e[K\]"
fi
unset __last_color
unset __return_code
}
# -----------------------------------------------------------------------------
# define the default color set
function set_default_colors() {
PL_COLORS=(
[Color_Off]='\[\e[0m\]' # Text Reset
# Foreground
[Default]='\[\e[0;39m\]' # Default
[Black]='\[\e[0;30m\]' # Black
[Red]='\[\e[0;31m\]' # Red
[Green]='\[\e[0;32m\]' # Green
[Yellow]='\[\e[0;33m\]' # Yellow
[Blue]='\[\e[0;34m\]' # Blue
[Purple]='\[\e[0;35m\]' # Purple
[Cyan]='\[\e[0;36m\]' # Cyan
[White]='\[\e[0;37m\]' # White
[Green1]='\[\e[38;5;065m\]'
[Green2]='\[\e[38;5;035m\]'
[Blue1]='\[\e[38;5;069m\]'
[Blue2]='\[\e[38;5;111m\]'
[Orange]='\[\e[38;5;208m\]'
[Purple1]='\[\e[38;5;095m\]'
# [Purple1]='\[\e[38;5;127m\]'
# [Purple1]='\[\e[38;5;171m\]'
# Background
[On_Default]='\[\e[49m\]' # Default
[On_Black]='\[\e[40m\]' # Black
[On_Red]='\[\e[41m\]' # Red
[On_Green]='\[\e[42m\]' # Green
[On_Yellow]='\[\e[43m\]' # Yellow
[On_Blue]='\[\e[44m\]' # Blue
[On_Purple]='\[\e[45m\]' # Purple
[On_Cyan]='\[\e[46m\]' # Cyan
[On_White]='\[\e[47m\]' # White
[On_Green1]='\[\e[48;5;065m\]'
[On_Green2]='\[\e[48;5;035m\]'
[On_Blue1]='\[\e[48;5;069m\]'
[On_Blue2]='\[\e[48;5;111m\]'
[On_Orange]='\[\e[48;5;208m\]'
[On_Purple1]='\[\e[48;5;095m\]'
# [On_Purple1]='\[\e[48;5;171m\]'
# [On_Purple1]='\[\e[48;5;127m\]'
)
}
# 171
# 32
# -----------------------------------------------------------------------------
# default symbols are intended for 'out-of-the-box' compatibility.
# symbols from code page 437: character set of the original IBM PC
function set_default_symbols {
PL_SYMBOLS=(
[hard_separator]="▶"
[soft_separator]="│"
[read_only]="Θ"
[return_code]="x"
[background_jobs]="↨"
[background_jobs]="↔"
)
}
# -----------------------------------------------------------------------------
# default set of segments
function set_default_segments {
PL_SEGMENTS=(
'user_segment Yellow Black'
'path_segment Blue Black'
'read_only_segment Red White'
)
PL_USER_SHOW_HOST=true
PL_PATH_TRIM=1
}
# git_segment
# Set default symbols if not already defined in config
# Defaults should be standard symbols.
[[ -z ${PL_SYMBOLS[git_branch]} ]] && PL_SYMBOLS[git_branch]="╬"
[[ -z ${PL_SYMBOLS[git_untracked]} ]] && PL_SYMBOLS[git_untracked]="?"
[[ -z ${PL_SYMBOLS[git_stash]} ]] && PL_SYMBOLS[git_stash]="§"
[[ -z ${PL_SYMBOLS[git_ahead]} ]] && PL_SYMBOLS[git_ahead]="↑"
[[ -z ${PL_SYMBOLS[git_behind]} ]] && PL_SYMBOLS[git_behind]="↓"
[[ -z ${PL_SYMBOLS[git_modified]} ]] && PL_SYMBOLS[git_modified]="+"
[[ -z ${PL_SYMBOLS[git_staged]} ]] && PL_SYMBOLS[git_staged]="•"
[[ -z ${PL_SYMBOLS[git_conflicts]} ]] && PL_SYMBOLS[git_conflicts]="*"
# -----------------------------------------------------------------------------
# append to prompt: git branch with indicators for;
# number of; modified files, staged files and conflicts
# arg: $1 background color
# arg: $2 foreground 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
function git_segment {
local git_branch
which git >/dev/null 2>&1 || return; ## return if no git
git_branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)
[[ -z $git_branch ]] && return; ## return early if not a git branch
local bg_color="$1"
local fg_color="$2"
local content="${PL_SYMBOLS[git_branch]} $git_branch"
if [ "$PL_GIT_STASH" = true ]; then
local number_stash
number_stash="$(git stash list 2>/dev/null | grep -F -v -c 'fatal:' | tr -d '[:space:]')"
if [ ! "$number_stash" -eq 0 ]; then
content+=" ${PL_SYMBOLS[soft_separator]} ${PL_SYMBOLS[git_stash]}$number_stash"
fi
fi
if [ "$PL_GIT_AHEAD" = true ]; then
local number_behind_ahead
number_behind_ahead="$(git rev-list --count --left-right '@{upstream}...HEAD' 2>/dev/null)"
local number_ahead="${number_behind_ahead#* }"
number_ahead="${number_behind_ahead#*$'\t'}"
local number_behind="${number_behind_ahead% *}"
number_behind="${number_behind_ahead%$'\t'*}"
if [ ! "0$number_ahead" -eq 0 ] || [ ! "0$number_behind" -eq 0 ]; then
if [ ! "0$number_ahead" -eq 0 ]; then
content+=" ${PL_SYMBOLS[soft_separator]} ${PL_SYMBOLS[git_ahead]}$number_ahead"
fi
if [ ! "0$number_behind" -eq 0 ]; then
content+=" ${PL_SYMBOLS[soft_separator]} ${PL_SYMBOLS[git_behind]}$number_behind"
fi
fi
fi
if [ "$PL_GIT_STAGED" = true ]; then
local number_staged
number_staged="$(git diff --staged --name-only --diff-filter=AM 2> /dev/null | wc -l | tr -d '[:space:]')"
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
number_conflicts="$(git diff --name-only --diff-filter=U 2> /dev/null | wc -l | tr -d '[:space:]')"
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
number_modified="$(git diff --name-only --diff-filter=M 2> /dev/null | wc -l | tr -d '[:space:]')"
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
number_untracked="$(git ls-files --other --exclude-standard 2> /dev/null | wc -l | tr -d '[:space:]')"
if [ ! "$number_untracked" -eq "0" ]; then
content+=" ${PL_SYMBOLS[soft_separator]} ${PL_SYMBOLS[git_untracked]}$number_untracked"
fi
fi
if [ -n "$(git status --porcelain 2> /dev/null)" ]; 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
PS1+="$(segment_end "$fg_color" "$bg_color")"
PS1+="$(segment_content "$fg_color" "$bg_color" " $content ")"
__last_color="$bg_color"
}
# kubernetes_segment
# -----------------------------------------------------------------------------
# append to prompt: default kubernetes context/namespace from $KUBECONFIG
# arg: $1 background color
# arg: $2 foreground color
function kubernetes_segment {
# check if the 'kubectl' command is available
if kubectl_loc="$(type -p "kubectl")" || [[ -n $kubectl_loc ]]; then
#local context=$(kubectl config current-context 2>/dev/null)
local context=$(kubectl config current-context | sed -E -e 's/:[6443]+//' -e 's/-mta-karolinska-se//' -e 's/api-//g')
local namespace=$(echo ${context} | cut -d '/' -f1 | tr '[:upper:]' '[:lower:]')
local cluster=$(echo ${context} | cut -d '/' -f2 | tr '[:upper:]' '[:lower:]')
local user=$(echo ${context} | cut -d '/' -f3 | tr '[:upper:]' '[:lower:]')
if [ -n "$context" ]; then
#local ns=$( kubectl config view -o jsonpath="{.contexts[?(@.name == \"${context}\")].context.namespace}" 2>/dev/null )
#if [ -n "$ns" ]; then
local bg_color=$1 # Set the background color
local fg_color=$2 # Set the foregropund color
local kubesymbol=$'\xE2\x8E\x88'
# local content="${kubesymbol} ${context}:${ns}"
local content="${user} | ${cluster} | ${namespace}"
PS1+=$(segment_end "$fg_color" "$bg_color")
PS1+=$(segment_content "$fg_color" "$bg_color" " $content ")
__last_color=$bg_color
#fi
fi
fi
}
# pwd_segment
# Set default symbols if not already defined in config
# Defaults should be standard symbols.
[[ -z ${PL_SYMBOLS[pwd_separator]} ]] && PL_SYMBOLS[pwd_separator]=${PL_SYMBOLS[soft_separator]}
[[ -z ${PL_SYMBOLS[pwd_trimmed]} ]] && PL_SYMBOLS[pwd_trimmed]=''
# -----------------------------------------------------------------------------
# append to prompt: current directory
# arg: $1 background color
# arg: $2 foreground color
# option variables;
# PL_PATH_TRIM: 0—fullpath, 1—current dir, [x]—trim to x number of dir
function pwd_segment {
local bg_color="$1"
local fg_color="$2"
#local content="\w"
local content="${PWD/#$HOME/~}" #works for bash < 4.4 #$(pwd)
content="${content/#$HOME/\~}" #escaping needed for bash >= 4.4
if [ "$PL_PATH_TRIM" -eq 1 ]; then
# local content="\W"
content="${content##*/}"
elif [ "$PL_PATH_TRIM" -gt 1 ]; then
# PROMPT_DIRTRIM="$PL_PATH_TRIM"
local re=")$"
local ree="\/[^\/]*"
for (( i = 0; i < $PL_PATH_TRIM; i++ ))
do
re="$ree$re"
done
re="($re"
[[ $content =~ $re ]]
ret=${BASH_REMATCH[1]}
if [ ${#ret} -gt 0 ] ; then
#content=${ret:1}
content="${PL_SYMBOLS[pwd_trimmed]}$ret"
fi
fi
content="${content//\// ${PL_SYMBOLS[pwd_separator]} }"
if [[ "${content:0:2}" == " ${PL_SYMBOLS[pwd_separator]}" ]] ; then content="/$content" ; fi
if [[ "${content: -2}" == "${PL_SYMBOLS[pwd_separator]} " ]] ; then content=${content%%???} ; fi
PS1+="$(segment_end "$fg_color" "$bg_color")"
PS1+="$(segment_content "$fg_color" "$bg_color" " $content ")"
__last_color="$bg_color"
}
# screen_session_segmen
# Set default symbols if not already defined in config
# Defaults should be standard symbols.
[[ -z ${PL_SYMBOLS[screen]} ]] && PL_SYMBOLS[screen]='■'
# -----------------------------------------------------------------------------
# append to prompt: "screen" session name
# arg: $1 background color
# arg: $2 foreground color
function screen_session_segment {
if [[ "$TERM" == screen.* && -n $STY ]]; then
local bg_color="$1"
local fg_color="$2"
local content=" ${PL_SYMBOLS[screen]} ${STY}"
PS1+="$(segment_end "$fg_color" "$bg_color")"
PS1+="$(segment_content "$fg_color" "$bg_color" "$content ")"
__last_color="$bg_color"
fi
}
# ssh_segment
# Set default symbols if not already defined in config
# Defaults should be standard symbols.
[[ -z ${PL_SYMBOLS[ssh]} ]] && PL_SYMBOLS[ssh]='╤'
# -----------------------------------------------------------------------------
# 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_segment {
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+="$(segment_end "$fg_color" "$bg_color")"
PS1+="$(segment_content "$fg_color" "$bg_color" " $content ")"
__last_color="$bg_color"
fi
}
# virtual_env_segment
# Set default symbols if not already defined in config
# Defaults should be standard symbols.
[[ -z ${PL_SYMBOLS[python]} ]] && PL_SYMBOLS[python]='π'
# -----------------------------------------------------------------------------
# append to prompt: python virtual environment name
# arg: $1 background color
# arg: $2 foreground color
function virtual_env_segment {
if [ -n "$VIRTUAL_ENV" ]; then
local venv="${VIRTUAL_ENV##*/}"
local bg_color="$1"
local fg_color="$2"
local content=" ${PL_SYMBOLS[python]} $venv"
PS1+="$(segment_end "$fg_color" "$bg_color")"
PS1+="$(segment_content "$fg_color" "$bg_color" "$content ")"
__last_color="$bg_color"
fi
}
# -----------------------------------------------------------------------------
# entry point to setup pureline
function main() {
local segment_index
local segment_function
set_default_colors
set_default_symbols
set_default_segments
# set some defaults
PL_TITLEBAR="\u@\h: \w" # title bar setting can use PS1 style \u etc
PL_ERASE_TO_EOL=false # need on some terminals to prevent glitches
# If using tmux, allow pane titles to persist
[[ -n $TMUX ]] && unset PL_TITLEBAR
# check if an argument has been given for a config file
if [ -f "$1" ]; then
# shellcheck source=/dev/null
source "$1"
fi
# source external segments
# local segment_dir
# segment_dir=$(dirname "${BASH_SOURCE[0]}")'/segments'
#for segment_index in "${!PL_SEGMENTS[@]}"; do
# check if segment function is not defined
# segment_function=${PL_SEGMENTS[$segment_index]%% *}
# if [ -z "$(type -t "$segment_function")" ]; then
# if not defined, source external function
# shellcheck source=/dev/null
# source "$segment_dir"'/'"$segment_function"
# fi
#done
# dynamically set the PS1
if [[ ! ${PROMPT_COMMAND} =~ 'pureline_ps1' ]]; then
eval "$(echo -e "
function pureline_ps1 {
__pureline_pre
$PROMPT_COMMAND
__pureline_post
}
")"
PROMPT_COMMAND="pureline_ps1"
# Note: defining PROMPT_COMMAND as a call to a single function simplifies a lot
# the integration of pureline in other prompt-modifying tools
# (like the 'shell integration' feature of the integrated terminals of VSCode).
fi
}
main "${@}"