-
Notifications
You must be signed in to change notification settings - Fork 1
/
liquidprompt
executable file
·2004 lines (1738 loc) · 63.5 KB
/
liquidprompt
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
################################################################################
# LIQUID PROMPT
# An intelligent and non-intrusive prompt for Bash and zsh
################################################################################
# Licensed under the AGPL version 3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###########
# AUTHORS #
###########
# Alex Prengère <[email protected]> # Untracked git files
# Anthony Gelibert <[email protected]> # Several fix
# Aurelien Requiem <[email protected]> # Major clean refactoring, variable path length, error codes, several bugfixes.
# Brendan Fahy <[email protected]> # Postfix variable
# Clément Mathieu <[email protected]> # Bazaar support
# David Loureiro <[email protected]> # Small portability fix
# Étienne Deparis <[email protected]> # Fossil support
# Florian Le Frioux <[email protected]> # Use ± mark when root in VCS dir.
# François Schmidts <[email protected]> # Initial PROMPT_DIRTRIM support
# Frédéric Lepied <[email protected]> # Python virtual env
# Jonas Bengtsson <[email protected]> # Git remotes fix
# Joris Dedieu <[email protected]> # Portability framework, FreeBSD support, bugfixes.
# Joris Vaillant <[email protected]> # Small git fix
# Luc Didry <[email protected]> # ZSH port, several fix
# Ludovic Rousseau <[email protected]> # Lot of bugfixes.
# Markus Dreseler <[email protected]> # Runtime of last command
# Nicolas Lacourte <[email protected]> # Screen title
# nojhan <[email protected]> # Original author.
# Olivier Mengué <[email protected]> # Major optimizations, refactorings everywhere; current maintainer
# Poil <[email protected]> # Speed improvements
# Rolf Morel <[email protected]> # "Shorten path" refactoring and fixes
# Thomas Debesse <[email protected]> # Fix columns use.
# Yann 'Ze' Richard <[email protected]> # Do not fail on missing commands.
# Austen Adler <[email protected]> # ZSH runtime
# See the README.md file for a summary of features.
# Issue #161: do not load if not an interactive shell
test -z "$TERM" -o "x$TERM" = xdumb && return
# Check for recent enough version of bash.
if test -n "${BASH_VERSION-}" -a -n "$PS1" ; then
bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
if (( bmajor < 3 || ( bmajor == 3 && bminor < 2 ) )); then
unset bash bmajor bminor
return
fi
unset bash bmajor bminor
_LP_SHELL_bash=true
_LP_SHELL_zsh=false
_LP_OPEN_ESC="\["
_LP_CLOSE_ESC="\]"
_LP_USER_SYMBOL="\u"
_LP_HOST_SYMBOL="\h"
_LP_FQDN_SYMBOL="\H"
_LP_TIME_SYMBOL="\t"
_LP_MARK_SYMBOL='\$'
_LP_PWD_SYMBOL="\\w"
_LP_DIR_SYMBOL="\\W"
_LP_FIRST_INDEX=0
_LP_PERCENT='%' # must be escaped on zsh
_LP_BACKSLASH='\\' # must be escaped on bash
# Escape the given strings
# Must be used for all strings injected in PS1 that may comes from remote sources,
# like $PWD, VCS branch names...
_lp_escape()
{
echo -nE "${1//\\/\\\\}"
}
# Disable the DEBUG trap used by the RUNTIME feature
# (in case we are reloading LP in the same shell after disabling
# the feature in .liquidpromptrc)
# FIXME this doesn't seem to work :(
[[ -n "${LP_ENABLE_RUNTIME-}" ]] && trap - DEBUG
elif test -n "${ZSH_VERSION-}" ; then
_LP_SHELL_bash=false
_LP_SHELL_zsh=true
_LP_OPEN_ESC="%{"
_LP_CLOSE_ESC="%}"
_LP_USER_SYMBOL="%n"
_LP_HOST_SYMBOL="%m"
_LP_FQDN_SYMBOL="%M"
_LP_TIME_SYMBOL="%*"
_LP_MARK_SYMBOL='%(!.#.%%)'
_LP_PWD_SYMBOL="%~"
_LP_DIR_SYMBOL="%1~"
_LP_FIRST_INDEX=1
_LP_PERCENT='%%'
_LP_BACKSLASH="\\"
_lp_escape()
{
arg="${1//\\/\\\\}"
echo -nE "${arg//\%/$_LP_PERCENT}"
}
# For ZSH, autoload required functions
autoload -Uz add-zsh-hook
# Disable previous hooks as options that set them
# may have changed
{
add-zsh-hook -d precmd _lp_set_prompt
add-zsh-hook -d preexec _lp_runtime_before
add-zsh-hook -d precmd _lp_runtime_after
} >/dev/null
else
echo "liquidprompt: shell not supported" >&2
return
fi
# Store $2 (or $?) as a true/false value in variable named $1
# $? is propagated
# _lp_bool foo 5
# => foo=false
# _lp_bool foo 0
# => foo=true
_lp_bool()
{
local res=${2:-$?}
if (( res )); then
eval $1=false
else
eval $1=true
fi
return $res
}
# Save $IFS as we want to restore the default value at the beginning of the
# prompt function
_LP_IFS="$IFS"
###############
# OS specific #
###############
# LP_OS detection, default to Linux
case $(uname) in
FreeBSD) LP_OS=FreeBSD ;;
DragonFly) LP_OS=FreeBSD ;;
OpenBSD) LP_OS=OpenBSD ;;
Darwin) LP_OS=Darwin ;;
SunOS) LP_OS=SunOS ;;
*) LP_OS=Linux ;;
esac
# Get cpu count
case "$LP_OS" in
Linux) _lp_CPUNUM=$( nproc 2>/dev/null || \grep -c '^[Pp]rocessor' /proc/cpuinfo ) ;;
FreeBSD|Darwin|OpenBSD) _lp_CPUNUM=$( sysctl -n hw.ncpu ) ;;
SunOS) _lp_CPUNUM=$( kstat -m cpu_info | \grep -c "module: cpu_info" ) ;;
esac
# Extended regexp patterns for sed
# GNU/BSD sed
_LP_SED_EXTENDED=r
[[ "$LP_OS" = Darwin ]] && _LP_SED_EXTENDED=E
# get current load
case "$LP_OS" in
Linux)
_lp_cpu_load () {
local eol
read lp_cpu_load eol < /proc/loadavg
}
;;
FreeBSD|Darwin|OpenBSD)
_lp_cpu_load () {
local bol eol
# If you have problems with syntax coloring due to the following
# line, do this: ln -s liquidprompt liquidprompt.bash
# and edit liquidprompt.bash
read bol lp_cpu_load eol <<<"$( LC_ALL=C sysctl -n vm.loadavg )"
}
;;
SunOS)
_lp_cpu_load () {
read lp_cpu_load <<<"$( LC_ALL=C uptime | sed 's/.*load average: *\([0-9.]*\).*/\1/' )"
}
esac
#################
# CONFIGURATION #
#################
# The following code is run just once. But it is encapsulated in a function
# to benefit of 'local' variables.
#
# What we do here:
# 1. Setup variables that can be used by the user: the "API" of Liquid Prompt
# for config/theme. Those variables are local to the function.
# In practice, this is only color variables.
# 2. Setup default values
# 3. Load the configuration
_lp_source_config()
{
# TermInfo feature detection
local ti_sgr0="$( { tput sgr0 || tput me ; } 2>/dev/null )"
local ti_bold="$( { tput bold || tput md ; } 2>/dev/null )"
local ti_setaf
local ti_setab
if tput setaf 0 >/dev/null 2>&1; then
ti_setaf() { tput setaf "$1" ; }
elif tput AF 0 >/dev/null 2>&1; then
# FreeBSD
ti_setaf() { tput AF "$1" ; }
elif tput AF 0 0 0 >/dev/null 2>&1; then
# OpenBSD
ti_setaf() { tput AF "$1" 0 0 ; }
else
echo "liquidprompt: terminal $TERM not supported" >&2
ti_setaf () { : ; }
fi
if tput setab 0 >/dev/null 2>&1; then
ti_setab() { tput setab "$1" ; }
elif tput AB 0 >/dev/null 2>&1; then
# FreeBSD
ti_setab() { tput AB "$1" ; }
elif tput AB 0 0 0 >/dev/null 2>&1; then
# OpenBSD
ti_setab() { tput AB "$1" 0 0 ; }
else
echo "liquidprompt: terminal $TERM not supported" >&2
ti_setab() { : ; }
fi
# Colors: variables are local so they will have a value only
# during config loading and will not conflict with other values
# with the same names defined by the user outside the config.
local BOLD="${_LP_OPEN_ESC}${ti_bold}${_LP_CLOSE_ESC}"
local BLACK="${_LP_OPEN_ESC}$(ti_setaf 0)${_LP_CLOSE_ESC}"
local BOLD_GRAY="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 0)${_LP_CLOSE_ESC}"
local WHITE="${_LP_OPEN_ESC}$(ti_setaf 7)${_LP_CLOSE_ESC}"
local BOLD_WHITE="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 7)${_LP_CLOSE_ESC}"
local RED="${_LP_OPEN_ESC}$(ti_setaf 1)${_LP_CLOSE_ESC}"
local BOLD_RED="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 1)${_LP_CLOSE_ESC}"
local WARN_RED="${_LP_OPEN_ESC}$(ti_setaf 0 ; ti_setab 1)${_LP_CLOSE_ESC}"
local CRIT_RED="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 7 ; ti_setab 1)${_LP_CLOSE_ESC}"
local DANGER_RED="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 3 ; ti_setab 1)${_LP_CLOSE_ESC}"
local GREEN="${_LP_OPEN_ESC}$(ti_setaf 2)${_LP_CLOSE_ESC}"
local BOLD_GREEN="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 2)${_LP_CLOSE_ESC}"
local YELLOW="${_LP_OPEN_ESC}$(ti_setaf 3)${_LP_CLOSE_ESC}"
local BOLD_YELLOW="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 3)${_LP_CLOSE_ESC}"
local BLUE="${_LP_OPEN_ESC}$(ti_setaf 4)${_LP_CLOSE_ESC}"
local BOLD_BLUE="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 4)${_LP_CLOSE_ESC}"
local PURPLE="${_LP_OPEN_ESC}$(ti_setaf 5)${_LP_CLOSE_ESC}"
local PINK="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 5)${_LP_CLOSE_ESC}"
local CYAN="${_LP_OPEN_ESC}$(ti_setaf 6)${_LP_CLOSE_ESC}"
local BOLD_CYAN="${_LP_OPEN_ESC}${ti_bold}$(ti_setaf 6)${_LP_CLOSE_ESC}"
# NO_COL is special: it will be used at runtime, not just during config loading
NO_COL="${_LP_OPEN_ESC}${ti_sgr0}${_LP_CLOSE_ESC}"
# compute the hash of the hostname
# and get the corresponding number in [1-6] (red,green,yellow,blue,purple or cyan)
# FIXME Add more formats (bold? 256 colors?)
# cksum is separated with tab on SunOS, space on others
local cksum="$(hostname | cksum)"
LP_COLOR_HOST_HASH="${_LP_OPEN_ESC}$(ti_setaf $(( 1 + ${cksum%%[ ]*} % 6 )) )${_LP_CLOSE_ESC}"
unset ti_sgr0 ti_bold
unset -f ti_setaf ti_setab
# Default values (globals)
LP_BATTERY_THRESHOLD=${LP_BATTERY_THRESHOLD:-75}
LP_LOAD_THRESHOLD=${LP_LOAD_THRESHOLD:-60}
LP_TEMP_THRESHOLD=${LP_TEMP_THRESHOLD:-60}
LP_RUNTIME_THRESHOLD=${LP_RUNTIME_THRESHOLD:-2}
LP_PATH_LENGTH=${LP_PATH_LENGTH:-35}
LP_PATH_KEEP=${LP_PATH_KEEP:-2}
LP_PATH_DEFAULT="${LP_PATH_DEFAULT:-$_LP_PWD_SYMBOL}"
LP_HOSTNAME_ALWAYS=${LP_HOSTNAME_ALWAYS:-0}
LP_USER_ALWAYS=${LP_USER_ALWAYS:-1}
LP_PERCENTS_ALWAYS=${LP_PERCENTS_ALWAYS:-1}
LP_PS1=${LP_PS1:-""}
if $_LP_SHELL_bash; then
LP_PS1_PREFIX="\n"
LP_PS1_POSTFIX="\n> "
elif $_LP_SHELL_zsh; then
LP_PS1_PREFIX=$'\n'
LP_PS1_POSTFIX=$'\n'"> "
fi
LP_ENABLE_PERM=${LP_ENABLE_PERM:-1}
LP_ENABLE_SHORTEN_PATH=${LP_ENABLE_SHORTEN_PATH:-1}
LP_ENABLE_PROXY=${LP_ENABLE_PROXY:-1}
LP_ENABLE_TEMP=${LP_ENABLE_TEMP:-1}
LP_ENABLE_JOBS=${LP_ENABLE_JOBS:-1}
LP_ENABLE_LOAD=${LP_ENABLE_LOAD:-1}
LP_ENABLE_BATT=${LP_ENABLE_BATT:-1}
LP_ENABLE_GIT=${LP_ENABLE_GIT:-1}
LP_ENABLE_SVN=${LP_ENABLE_SVN:-1}
LP_ENABLE_VCSH=${LP_ENABLE_VCSH:-1}
LP_ENABLE_FOSSIL=${LP_ENABLE_FOSSIL:-1}
LP_ENABLE_HG=${LP_ENABLE_HG:-1}
LP_ENABLE_BZR=${LP_ENABLE_BZR:-1}
LP_ENABLE_TIME=${LP_ENABLE_TIME:-0}
LP_ENABLE_RUNTIME=${LP_ENABLE_RUNTIME:-1}
LP_ENABLE_VIRTUALENV=${LP_ENABLE_VIRTUALENV:-1}
LP_ENABLE_SCLS=${LP_ENABLE_SCLS:-1}
LP_ENABLE_VCS_ROOT=${LP_ENABLE_VCS_ROOT:-0}
LP_ENABLE_TITLE=${LP_ENABLE_TITLE:-0}
LP_ENABLE_SCREEN_TITLE=${LP_ENABLE_SCREEN_TITLE:-0}
LP_ENABLE_SSH_COLORS=${LP_ENABLE_SSH_COLORS:-0}
LP_ENABLE_FQDN=${LP_ENABLE_FQDN:-0}
# LP_DISABLED_VCS_PATH="${LP_DISABLED_VCS_PATH}"
LP_ENABLE_SUDO=${LP_ENABLE_SUDO:-0}
LP_MARK_DEFAULT="${LP_MARK_DEFAULT:-$_LP_MARK_SYMBOL}"
LP_MARK_BATTERY="${LP_MARK_BATTERY:-"⌁"}"
LP_MARK_ADAPTER="${LP_MARK_ADAPTER:-"⏚"}"
LP_MARK_LOAD="${LP_MARK_LOAD:-"⌂"}"
LP_MARK_TEMP="${LP_MARK_TEMP:-"θ"}"
LP_MARK_PROXY="${LP_MARK_PROXY:-"↥"}"
LP_MARK_HG="${LP_MARK_HG:-"☿"}"
LP_MARK_SVN="${LP_MARK_SVN:-"‡"}"
LP_MARK_GIT="${LP_MARK_GIT:-"±"}"
LP_MARK_VCSH="${LP_MARK_VCSH:-"|"}"
LP_MARK_FOSSIL="${LP_MARK_FOSSIL:-"⌘"}"
LP_MARK_BZR="${LP_MARK_BZR:-"⚯"}"
LP_MARK_DISABLED="${LP_MARK_DISABLED:-"⌀"}"
LP_MARK_UNTRACKED="${LP_MARK_UNTRACKED:-"*"}"
LP_MARK_STASH="${LP_MARK_STASH:-"+"}"
LP_MARK_INDEX="${LP_MARK_INDEX:-"+"}"
LP_MARK_BRACKET_OPEN="${LP_MARK_BRACKET_OPEN:-"["}"
LP_MARK_BRACKET_CLOSE="${LP_MARK_BRACKET_CLOSE:-"]"}"
LP_MARK_SHORTEN_PATH="${LP_MARK_SHORTEN_PATH:-" … "}"
LP_MARK_PREFIX="${LP_MARK_PREFIX:-" "}"
LP_MARK_PERM="${LP_MARK_PERM:-":"}"
LP_COLOR_PATH=${LP_COLOR_PATH:-$BOLD}
LP_COLOR_PATH_ROOT=${LP_COLOR_PATH_ROOT:-$BOLD_YELLOW}
LP_COLOR_PROXY=${LP_COLOR_PROXY:-$BOLD_BLUE}
LP_COLOR_JOB_D=${LP_COLOR_JOB_D:-$YELLOW}
LP_COLOR_JOB_R=${LP_COLOR_JOB_R:-$BOLD_YELLOW}
LP_COLOR_JOB_Z=${LP_COLOR_JOB_Z:-$BOLD_YELLOW}
LP_COLOR_ERR=${LP_COLOR_ERR:-$PURPLE}
LP_COLOR_MARK=${LP_COLOR_MARK:-$BOLD}
LP_COLOR_MARK_ROOT=${LP_COLOR_MARK_ROOT:-$BOLD_RED}
LP_COLOR_MARK_SUDO=${LP_COLOR_MARK_SUDO:-$LP_COLOR_MARK_ROOT}
LP_COLOR_USER_LOGGED=${LP_COLOR_USER_LOGGED:-""}
LP_COLOR_USER_ALT=${LP_COLOR_USER_ALT:-$BOLD}
LP_COLOR_USER_ROOT=${LP_COLOR_USER_ROOT:-$BOLD_YELLOW}
LP_COLOR_HOST=${LP_COLOR_HOST:-""}
LP_COLOR_SSH=${LP_COLOR_SSH:-$BLUE}
LP_COLOR_SU=${LP_COLOR_SU:-$BOLD_YELLOW}
LP_COLOR_TELNET=${LP_COLOR_TELNET:-$WARN_RED}
LP_COLOR_X11_ON=${LP_COLOR_X11_ON:-$GREEN}
LP_COLOR_X11_OFF=${LP_COLOR_X11_OFF:-$YELLOW}
LP_COLOR_WRITE=${LP_COLOR_WRITE:-$GREEN}
LP_COLOR_NOWRITE=${LP_COLOR_NOWRITE:-$RED}
LP_COLOR_UP=${LP_COLOR_UP:-$GREEN}
LP_COLOR_COMMITS=${LP_COLOR_COMMITS:-$YELLOW}
LP_COLOR_COMMITS_BEHIND=${LP_COLOR_COMMITS_BEHIND:-$BOLD_RED}
LP_COLOR_CHANGES=${LP_COLOR_CHANGES:-$RED}
LP_COLOR_DIFF=${LP_COLOR_DIFF:-$PURPLE}
LP_COLOR_CHARGING_ABOVE=${LP_COLOR_CHARGING_ABOVE:-$GREEN}
LP_COLOR_CHARGING_UNDER=${LP_COLOR_CHARGING_UNDER:-$YELLOW}
LP_COLOR_DISCHARGING_ABOVE=${LP_COLOR_DISCHARGING_ABOVE:-$YELLOW}
LP_COLOR_DISCHARGING_UNDER=${LP_COLOR_DISCHARGING_UNDER:-$RED}
LP_COLOR_TIME=${LP_COLOR_TIME:-$BLUE}
LP_COLOR_IN_MULTIPLEXER=${LP_COLOR_IN_MULTIPLEXER:-$BOLD_BLUE}
LP_COLOR_RUNTIME=${LP_COLOR_RUNTIME:-$YELLOW}
LP_COLOR_VIRTUALENV=${LP_COLOR_VIRTUALENV:-$CYAN}
if [[ -z "${LP_COLORMAP-}" ]]; then
LP_COLORMAP=(
"" # 0
"$GREEN" # 1
"$BOLD_GREEN" # 2
"$YELLOW" # 3
"$BOLD_YELLOW" # 4
"$RED" # 5
"$BOLD_RED" # 6
"$WARN_RED" # 7
"$CRIT_RED" # 8
"$DANGER_RED" # 9
)
fi
# Debugging flags
LP_DEBUG_TIME=${LP_DEBUG_TIME:-0}
local configfile
# Default config file may be the XDG standard ~/.config/liquidpromptrc,
# but heirloom dotfile has priority.
if [[ -f "$HOME/.liquidpromptrc" ]]; then
configfile="$HOME/.liquidpromptrc"
else
local first
local search
# trailing ":" is so that ${search#*:} always removes something
search="${XDG_CONFIG_HOME:-"$HOME/.config"}:${XDG_CONFIG_DIRS:-/etc/xdg}:"
while [[ -n "$search" ]]; do
first="${search%%:*}"
search="${search#*:}"
if [[ -f "$first/liquidpromptrc" ]]; then
configfile="$first/liquidpromptrc"
break
fi
done
fi
if [[ -n "$configfile" ]]; then
source "$configfile"
elif [[ -f "/etc/liquidpromptrc" ]]; then
source "/etc/liquidpromptrc"
fi
# Delete this code in version 1.11
if [[ -n "${LP_COLORMAP_1-}" ]]; then
echo "liquidprompt: LP_COLORMAP_x variables are deprecated. Update your theme to use LP_COLORMAP array." >&2
LP_COLORMAP=(
"$LP_COLORMAP_0"
"$LP_COLORMAP_1"
"$LP_COLORMAP_2"
"$LP_COLORMAP_3"
"$LP_COLORMAP_4"
"$LP_COLORMAP_5"
"$LP_COLORMAP_6"
"$LP_COLORMAP_7"
"$LP_COLORMAP_8"
"$LP_COLORMAP_9"
)
unset LP_COLORMAP_0 LP_COLORMAP_1 LP_COLORMAP_2 LP_COLORMAP_3 LP_COLORMAP_4 \
LP_COLORMAP_5 LP_COLORMAP_6 LP_COLORMAP_7 LP_COLORMAP_8 LP_COLORMAP_9
fi
}
# do source config files
_lp_source_config
unset -f _lp_source_config
# Disable feature if the tool is not installed
_lp_require_tool()
{
(( LP_ENABLE_$1 )) && { command -v $2 >/dev/null || eval LP_ENABLE_$1=0 ; }
}
_lp_require_tool GIT git
_lp_require_tool SVN svn
_lp_require_tool FOSSIL fossil
_lp_require_tool HG hg
_lp_require_tool BZR bzr
if [[ "$LP_OS" = Darwin ]]; then
_lp_require_tool BATT pmset
else
_lp_require_tool BATT acpi
fi
unset -f _lp_require_tool
if (( LP_ENABLE_JOBS )); then
typeset -i _LP_ENABLE_DETACHED_SESSIONS _LP_ENABLE_SCREEN _LP_ENABLE_TMUX
command -v screen >/dev/null ; _LP_ENABLE_SCREEN=!$?
command -v tmux >/dev/null ; _LP_ENABLE_TMUX=!$?
(( _LP_ENABLE_DETACHED_SESSIONS = ( _LP_ENABLE_SCREEN || _LP_ENABLE_TMUX ) ))
fi
# Use standard path symbols inside Midnight Commander
[[ -n "${MC_SID-}" ]] && LP_ENABLE_SHORTEN_PATH=0
# If we are running in a terminal multiplexer, brackets are colored
if [[ "$TERM" == screen* ]]; then
LP_BRACKET_OPEN="${LP_COLOR_IN_MULTIPLEXER}${LP_MARK_BRACKET_OPEN}${NO_COL}"
LP_BRACKET_CLOSE="${LP_COLOR_IN_MULTIPLEXER}${LP_MARK_BRACKET_CLOSE}${NO_COL}"
(( LP_ENABLE_TITLE = LP_ENABLE_TITLE && LP_ENABLE_SCREEN_TITLE ))
LP_TITLE_OPEN="$(printf '\033k')"
# "\e\" but on bash \ must be escaped
LP_TITLE_CLOSE="$(printf '\033%s' "$_LP_BACKSLASH")"
else
LP_BRACKET_OPEN="${LP_MARK_BRACKET_OPEN}"
LP_BRACKET_CLOSE="${LP_MARK_BRACKET_CLOSE}"
LP_TITLE_OPEN="$(printf '\e]0;')"
LP_TITLE_CLOSE="$(printf '\a')"
fi
[[ "_$TERM" == _linux* ]] && LP_ENABLE_TITLE=0
# update_terminal_cwd is a shell function available on MacOS X Lion that
# will update an icon of the directory displayed in the title of the terminal
# window.
# See http://hints.macworld.com/article.php?story=20110722211753852
if [[ "${TERM_PROGRAM-}" == Apple_Terminal ]] && command -v update_terminal_cwd >/dev/null; then
_LP_TERM_UPDATE_DIR=update_terminal_cwd
# Remove "update_terminal_cwd; " that has been add by Apple in /et/bashrc.
# See issue #196
PROMPT_COMMAND="${PROMPT_COMMAND//update_terminal_cwd; /}"
else
_LP_TERM_UPDATE_DIR=:
fi
# Default value for LP_PERM when LP_ENABLE_PERM is 0
LP_PERM=${LP_MARK_PERM} # without color
# Same as bash '\l', but inlined as a constant as the value will not change
# during the shell's life
LP_TTYN="$(basename -- "$(tty)" 2>/dev/null)"
###############
# Who are we? #
###############
command -v _lp_sudo_check >/dev/null && unset -f _lp_sudo_check
# Yellow for root, bold if the user is not the login one, else no color.
if (( EUID != 0 )); then # if user is not root
# if user is not login user
if [[ "${USER}" != "$(logname 2>/dev/null || echo "$LOGNAME")" ]]; then
LP_USER="${LP_COLOR_USER_ALT}${_LP_USER_SYMBOL}${NO_COL}"
elif (( LP_USER_ALWAYS )); then
LP_USER="${LP_COLOR_USER_LOGGED}${_LP_USER_SYMBOL}${NO_COL}"
else
LP_USER=""
fi
# "sudo -n" is only supported from sudo 1.7.0
if (( LP_ENABLE_SUDO )) \
&& command -v sudo >/dev/null \
&& LC_MESSAGES=C sudo -V | GREP_OPTIONS= \grep -qE '^Sudo version (1(\.([789]\.|[1-9][0-9])|[0-9])|[2-9])'
then
LP_COLOR_MARK_NO_SUDO="$LP_COLOR_MARK"
# Test the code with the commands:
# sudo id # sudo, enter your credentials
# sudo -K # revoke your credentials
_lp_sudo_check()
{
if sudo -n true 2>/dev/null; then
LP_COLOR_MARK=$LP_COLOR_MARK_SUDO
else
LP_COLOR_MARK=$LP_COLOR_MARK_NO_SUDO
fi
}
fi
else # root!
LP_USER="${LP_COLOR_USER_ROOT}${_LP_USER_SYMBOL}${NO_COL}"
LP_COLOR_MARK="${LP_COLOR_MARK_ROOT}"
LP_COLOR_PATH="${LP_COLOR_PATH_ROOT}"
# Disable VCS info for all paths
if (( ! LP_ENABLE_VCS_ROOT )); then
LP_DISABLED_VCS_PATH=/
LP_MARK_DISABLED="$LP_MARK_DEFAULT"
fi
fi
# Empty _lp_sudo_check if root or sudo disabled
if ! command -v _lp_sudo_check >/dev/null; then
_lp_sudo_check() { :; }
fi
#################
# Where are we? #
#################
_lp_connection()
{
if [[ -n "${SSH_CLIENT-}${SSH2_CLIENT-}${SSH_TTY-}" ]]; then
echo ssh
else
# tmux: see GH #304
# TODO check on *BSD
local whoami="$(LC_ALL=C who am i)"
local sess_parent="$(ps -o comm= -p $PPID 2> /dev/null)"
if [[ x"$whoami" != *'('* || x"$whoami" = *'(:'* || x"$whoami" = *'(tmux'* ]]; then
echo lcl # Local
elif [[ "$sess_parent" = "su" || "$sess_parent" = "sudo" ]]; then
echo su # Remote su/sudo
else
echo tel # Telnet
fi
fi
}
# Put the hostname if not locally connected
# color it in cyan within SSH, and a warning red if within telnet
# else display the host without color
# The connection is not expected to change from inside the shell, so we
# build this just once
LP_HOST=""
# Only process hostname elements if we haven't turned them off
if (( LP_HOSTNAME_ALWAYS != -1 )); then
[[ -r /etc/debian_chroot ]] && LP_HOST="($(< /etc/debian_chroot))"
# Which host symbol should we use?
if (( LP_ENABLE_FQDN )); then
LP_HOST_SYMBOL="${_LP_FQDN_SYMBOL}"
else
LP_HOST_SYMBOL="${_LP_HOST_SYMBOL}"
fi
# If we are connected with a X11 support
if [[ -n "$DISPLAY" ]]; then
LP_HOST="${LP_COLOR_X11_ON}${LP_HOST}@${NO_COL}"
else
LP_HOST="${LP_COLOR_X11_OFF}${LP_HOST}@${NO_COL}"
fi
case "$(_lp_connection)" in
lcl)
if (( LP_HOSTNAME_ALWAYS )); then
LP_HOST+="${LP_COLOR_HOST}${LP_HOST_SYMBOL}${NO_COL}"
else
# FIXME do we want to display the chroot if local?
LP_HOST="" # no hostname if local
fi
;;
ssh)
# If we want a different color for each host
(( LP_ENABLE_SSH_COLORS )) && LP_COLOR_SSH="$LP_COLOR_HOST_HASH"
LP_HOST+="${LP_COLOR_SSH}${LP_HOST_SYMBOL}${NO_COL}"
;;
su)
LP_HOST+="${LP_COLOR_SU}${LP_HOST_SYMBOL}${NO_COL}"
;;
tel)
LP_HOST+="${LP_COLOR_TELNET}${LP_HOST_SYMBOL}${NO_COL}"
;;
*)
LP_HOST+="${LP_HOST_SYMBOL}" # defaults to no color
;;
esac
fi
# Useless now, so undefine
unset -f _lp_connection
_lp_get_home_tilde_collapsed()
{
local tilde="~"
echo "${PWD/#$HOME/$tilde}"
}
# Shorten the path of the current working directory
# * Show only the current directory
# * Show as much of the cwd path as possible, if shortened display a
# leading mark, such as ellipses, to indicate that part is missing
# * show at least LP_PATH_KEEP leading dirs and current directory
_lp_shorten_path()
{
if (( ! LP_ENABLE_SHORTEN_PATH )); then
# We are not supposed to come here often as this case is already
# optimized at install time
LP_PWD="${LP_COLOR_PATH}${LP_PATH_DEFAULT}$NO_COL"
return
fi
local ret=
local p="$(_lp_get_home_tilde_collapsed)"
local mask="${LP_MARK_SHORTEN_PATH}"
local -i max_len=$(( ${COLUMNS:-80} * LP_PATH_LENGTH / 100 ))
if (( LP_PATH_KEEP == -1 )); then
# only show the current directory, excluding any parent dirs
ret="${p##*/}" # discard everything up to and including the last slash
[[ "${ret}" == "" ]] && ret="/" # if in root directory
elif (( ${#p} <= max_len )); then
ret="${p}"
elif (( LP_PATH_KEEP == 0 )); then
# len is over max len, show as much of the tail as is allowed
ret="${p##*/}" # show at least complete current directory
p="${p:0:${#p} - ${#ret}}"
ret="${mask}${p:${#p} - (${max_len} - ${#ret} - ${#mask})}${ret}"
else
# len is over max len, show at least LP_PATH_KEEP leading dirs and
# current directory
local tmp="${p//\//}"
local -i delims=$(( ${#p} - ${#tmp} ))
for (( dir=0; dir < LP_PATH_KEEP; dir++ )); do
(( dir == delims )) && break
local left="${p#*/}"
local name="${p:0:${#p} - ${#left}}"
p="${left}"
ret+="${name%/}/"
done
if (( delims <= LP_PATH_KEEP )); then
# no dirs between LP_PATH_KEEP leading dirs and current dir
ret+="${p##*/}"
else
local base="${p##*/}"
p="${p:0:${#p} - ${#base}}"
[[ ${ret} != "/" ]] && ret="${ret%/}" # strip trailing slash
local -i len_left=$(( max_len - ${#ret} - ${#base} - ${#mask} ))
ret+="${mask}${p:${#p} - ${len_left}}${base}"
fi
fi
# Escape special chars
LP_PWD="${LP_COLOR_PATH}$(_lp_escape "$ret")$NO_COL"
}
if (( LP_ENABLE_SHORTEN_PATH )); then
if (( LP_PATH_KEEP == -1 )); then
# _lp_shorten_path becomes a noop
_lp_shorten_path()
{
:
}
# Will never change
LP_PWD="${LP_COLOR_PATH}${_LP_DIR_SYMBOL}$NO_COL"
fi
else
# Will never change
LP_PWD="${LP_COLOR_PATH}${LP_PATH_DEFAULT}$NO_COL"
if $_LP_SHELL_bash && [[ -n "$PROMPT_DIRTRIM" ]]; then
unset -f _lp_shorten_path
alias _lp_shorten_path=_lp_set_dirtrim
fi
fi
# In Bash shells, PROMPT_DIRTRIM is the number of directories to keep at the end
# of the displayed path (if "\w" is present in the PS1 var).
# Liquid Prompt can calculate this number under two conditions, path shortening
# must be disabled and PROMPT_DIRTRIM must be already set.
_lp_set_dirtrim() {
local p="$(_lp_get_home_tilde_collapsed)"
local -i max_len="${COLUMNS:-80}*$LP_PATH_LENGTH/100"
local -i dt=0
if (( ${#p} > max_len )); then
local q="/${p##*/}"
local show="$q"
# +3 because of the ellipsis: "..."
while (( ${#show}+3 < max_len )); do
(( dt++ ))
p="${p%$q}"
q="/${p##*/}"
show="$q$show"
done
(( dt == 0 )) && dt=1
fi
PROMPT_DIRTRIM=$dt
# For debugging
# echo PROMPT_DIRTRIM=$PROMPT_DIRTRIM >&2
}
################
# Related jobs #
################
# Display the count of each if non-zero:
# - detached screens sessions and/or tmux sessions running on the host
# - attached running jobs (started with $ myjob &)
# - attached stopped jobs (suspended with Ctrl-Z)
_lp_jobcount_color()
{
(( LP_ENABLE_JOBS )) || return
local ret=""
local -i r s
# Count detached sessions
if (( _LP_ENABLE_DETACHED_SESSIONS )); then
local -i detached=0
(( _LP_ENABLE_SCREEN )) && detached=$(screen -ls 2> /dev/null | \grep -c '[Dd]etach[^)]*)$')
(( _LP_ENABLE_TMUX )) && detached+=$(tmux list-sessions 2> /dev/null | \grep -cv 'attached')
(( detached > 0 )) && ret+="${LP_COLOR_JOB_D}${detached}d${NO_COL}"
fi
# Count running jobs
if (( r = $(jobs -r | wc -l) )); then
[[ -n "$ret" ]] && ret+='/'
ret+="${LP_COLOR_JOB_R}${r}&${NO_COL}"
fi
# Count stopped jobs
if (( s = $(jobs -s | wc -l) )); then
[[ -n "$ret" ]] && ret+='/'
ret+="${LP_COLOR_JOB_Z}${s}z${NO_COL}"
fi
echo -nE "$ret"
}
######################
# VCS branch display #
######################
_lp_are_vcs_enabled()
{
[[ -z "$LP_DISABLED_VCS_PATH" ]] && return 0
$_LP_SHELL_zsh && setopt local_options && setopt sh_word_split
local path
local IFS=:
for path in $LP_DISABLED_VCS_PATH; do
[[ "$PWD" == *"$path"* ]] && return 1
done
return 0
}
# GIT #
# Get the branch name of the current directory
_lp_git_branch()
{
(( LP_ENABLE_GIT )) || return
\git rev-parse --is-inside-work-tree >/dev/null 2>&1 || return
local branch
# Recent versions of Git support the --short option for symbolic-ref, but
# not 1.7.9 (Ubuntu 12.04)
if branch="$(\git symbolic-ref -q HEAD)"; then
_lp_escape "${branch#refs/heads/}"
else
# In detached head state, use commit instead
# No escape needed
\git rev-parse --short -q HEAD
fi
}
# Display additional information if HEAD is in merging, rebasing
# or cherry-picking state
_lp_git_head_status() {
local gitdir
gitdir="$(\git rev-parse --git-dir 2>/dev/null)"
if [[ -f "${gitdir}/MERGE_HEAD" ]]; then
echo " MERGING"
elif [[ -d "${gitdir}/rebase-apply" || -d "${gitdir}/rebase-merge" ]]; then
echo " REBASING"
elif [[ -f "${gitdir}/CHERRY_PICK_HEAD" ]]; then
echo " CHERRY-PICKING"
fi
}
# Set a color depending on the branch state:
# - green if the repository is up to date
# - yellow if there is some commits not pushed
# - red if there is changes to commit
#
# Add the number of pending commits and the impacted lines.
_lp_git_branch_color()
{
(( LP_ENABLE_GIT )) || return
local branch
branch="$(_lp_git_branch)"
if [[ -n "$branch" ]]; then
local end
end="${LP_COLOR_CHANGES}$(_lp_git_head_status)${NO_COL}"
if LC_ALL=C \git status --porcelain 2>/dev/null | \grep -q '^??'; then
end="$LP_COLOR_TIME$LP_MARK_UNTRACKED$end"
fi
# Show if there are changes in the index
if [ $(git diff --cached | wc -l) -gt 0 ]; then
end="$LP_COLOR_COMMITS$LP_MARK_INDEX$end"
fi
local remote
remote="$(\git config --get branch.${branch}.remote 2>/dev/null)"
local has_commit=""
local commit_ahead
local commit_behind
if [[ -n "$remote" ]]; then
local remote_branch
remote_branch="$(\git config --get branch.${branch}.merge)"
if [[ -n "$remote_branch" ]]; then
remote_branch=${remote_branch/refs\/heads/refs\/remotes\/$remote}
commit_ahead="$(\git rev-list --count $remote_branch..HEAD 2>/dev/null)"
commit_behind="$(\git rev-list --count HEAD..$remote_branch 2>/dev/null)"
if [[ "$commit_ahead" -ne "0" && "$commit_behind" -ne "0" ]]; then
has_commit="${LP_COLOR_COMMITS}^$commit_ahead${NO_COL}/${LP_COLOR_COMMITS_BEHIND}v$commit_behind${NO_COL}"
elif [[ "$commit_ahead" -ne "0" ]]; then
has_commit="${LP_COLOR_COMMITS}^$commit_ahead${NO_COL}"
elif [[ "$commit_behind" -ne "0" ]]; then
has_commit="${LP_COLOR_COMMITS_BEHIND}v$commit_behind${NO_COL}"
fi
fi
fi
local ret
local shortstat # only to check for uncommitted changes
shortstat="$(LC_ALL=C \git diff --shortstat HEAD 2>/dev/null)"
if [[ -n "$shortstat" ]]; then
local u_stat # shorstat of *unstaged* changes
u_stat="$(LC_ALL=C \git diff --shortstat 2>/dev/null)"
u_stat=${u_stat/*changed, /} # removing "n file(s) changed"
local i_lines # inserted lines
if [[ "$u_stat" = *insertion* ]]; then
i_lines=${u_stat/ inser*}
else
i_lines=0
fi
local d_lines # deleted lines
if [[ "$u_stat" = *deletion* ]]; then
d_lines=${u_stat/*\(+\), }
d_lines=${d_lines/ del*/}
else
d_lines=0
fi
local has_lines
has_lines="+$i_lines/-$d_lines"
if [[ -n "$has_commit" ]]; then
# Changes to commit and commits to push
ret="${branch} ${NO_COL}(${LP_COLOR_DIFF}$has_lines${NO_COL},$has_commit)"
else
ret="${branch} ${NO_COL}(${LP_COLOR_DIFF}$has_lines${NO_COL})" # changes to commit
fi
elif [[ -n "$has_commit" ]]; then
# some commit(s) to push
if [[ "$commit_behind" -gt "0" ]]; then
ret="${LP_COLOR_COMMITS_BEHIND}${branch} ${NO_COL}($has_commit)"
else
ret="${LP_COLOR_COMMITS}${branch} ${NO_COL}($has_commit)"
fi
else
ret="${LP_COLOR_UP}${branch}" # nothing to commit or push
fi
echo -nE "$ret$end"
fi
}
# Search upwards through a directory structure looking for a file/folder with
# the given name. Used to avoid invoking 'hg' and 'bzr'.
_lp_upwards_find()
{
local dir
dir="$PWD"
while [[ -n "$dir" ]]; do
[[ -d "$dir/$1" ]] && return 0
dir="${dir%/*}"
done
return 1
}
# MERCURIAL #
# Get the branch name of the current directory
_lp_hg_branch()
{
(( LP_ENABLE_HG )) || return
# First do a simple search to avoid having to invoke hg -- at least on my