-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
p10k.zsh
9249 lines (8428 loc) · 327 KB
/
p10k.zsh
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
if [[ $__p9k_sourced != 13 ]]; then
>&2 print -P ""
>&2 print -P "[%F{1}ERROR%f]: Corrupted powerlevel10k installation."
>&2 print -P ""
if (( ${+functions[antigen]} )); then
>&2 print -P "If using %Bantigen%b, run the following command to fix:"
>&2 print -P ""
>&2 print -P " %F{2}antigen%f reset"
if [[ -d ~/.antigen ]]; then
>&2 print -P ""
>&2 print -P "If it doesn't help, try this:"
>&2 print -P ""
>&2 print -P " %F{2}rm%f -rf %U~/.antigen%u"
fi
else
>&2 print -P "Try resetting cache in your plugin manager or"
>&2 print -P "reinstalling powerlevel10k from scratch."
fi
>&2 print -P ""
return 1
fi
if ! autoload -Uz is-at-least || ! is-at-least 5.1; then
() {
>&2 echo -E "You are using ZSH version $ZSH_VERSION. The minimum required version for Powerlevel10k is 5.1."
>&2 echo -E "Type 'echo \$ZSH_VERSION' to see your current zsh version."
local def=${SHELL:c:A}
local cur=${${ZSH_ARGZERO#-}:c:A}
local cur_v="$($cur -c 'echo -E $ZSH_VERSION' 2>/dev/null)"
if [[ $cur_v == $ZSH_VERSION && $cur != $def ]]; then
>&2 echo -E "The shell you are currently running is likely $cur."
fi
local other=${${:-zsh}:c}
if [[ -n $other ]] && $other -c 'autoload -Uz is-at-least && is-at-least 5.1' &>/dev/null; then
local other_v="$($other -c 'echo -E $ZSH_VERSION' 2>/dev/null)"
if [[ -n $other_v && $other_v != $ZSH_VERSION ]]; then
>&2 echo -E "You have $other with version $other_v but this is not what you are using."
if [[ -n $def && $def != ${other:A} ]]; then
>&2 echo -E "To change your user shell, type the following command:"
>&2 echo -E ""
if [[ "$(grep -F $other /etc/shells 2>/dev/null)" != $other ]]; then
>&2 echo -E " echo ${(q-)other} | sudo tee -a /etc/shells"
fi
>&2 echo -E " chsh -s ${(q-)other}"
fi
fi
fi
}
return 1
fi
builtin source "${__p9k_root_dir}/internal/configure.zsh"
builtin source "${__p9k_root_dir}/internal/worker.zsh"
builtin source "${__p9k_root_dir}/internal/parser.zsh"
builtin source "${__p9k_root_dir}/internal/icons.zsh"
# For compatibility with Powerlevel9k. It's not recommended to use mnemonic color
# names in the configuration except for colors 0-7 as these are standard.
typeset -grA __p9k_colors=(
black 000 red 001 green 002 yellow 003
blue 004 magenta 005 cyan 006 white 007
grey 008 maroon 009 lime 010 olive 011
navy 012 fuchsia 013 aqua 014 teal 014
silver 015 grey0 016 navyblue 017 darkblue 018
blue3 020 blue1 021 darkgreen 022 deepskyblue4 025
dodgerblue3 026 dodgerblue2 027 green4 028 springgreen4 029
turquoise4 030 deepskyblue3 032 dodgerblue1 033 darkcyan 036
lightseagreen 037 deepskyblue2 038 deepskyblue1 039 green3 040
springgreen3 041 cyan3 043 darkturquoise 044 turquoise2 045
green1 046 springgreen2 047 springgreen1 048 mediumspringgreen 049
cyan2 050 cyan1 051 purple4 055 purple3 056
blueviolet 057 grey37 059 mediumpurple4 060 slateblue3 062
royalblue1 063 chartreuse4 064 paleturquoise4 066 steelblue 067
steelblue3 068 cornflowerblue 069 darkseagreen4 071 cadetblue 073
skyblue3 074 chartreuse3 076 seagreen3 078 aquamarine3 079
mediumturquoise 080 steelblue1 081 seagreen2 083 seagreen1 085
darkslategray2 087 darkred 088 darkmagenta 091 orange4 094
lightpink4 095 plum4 096 mediumpurple3 098 slateblue1 099
wheat4 101 grey53 102 lightslategrey 103 mediumpurple 104
lightslateblue 105 yellow4 106 darkseagreen 108 lightskyblue3 110
skyblue2 111 chartreuse2 112 palegreen3 114 darkslategray3 116
skyblue1 117 chartreuse1 118 lightgreen 120 aquamarine1 122
darkslategray1 123 deeppink4 125 mediumvioletred 126 darkviolet 128
purple 129 mediumorchid3 133 mediumorchid 134 darkgoldenrod 136
rosybrown 138 grey63 139 mediumpurple2 140 mediumpurple1 141
darkkhaki 143 navajowhite3 144 grey69 145 lightsteelblue3 146
lightsteelblue 147 darkolivegreen3 149 darkseagreen3 150 lightcyan3 152
lightskyblue1 153 greenyellow 154 darkolivegreen2 155 palegreen1 156
darkseagreen2 157 paleturquoise1 159 red3 160 deeppink3 162
magenta3 164 darkorange3 166 indianred 167 hotpink3 168
hotpink2 169 orchid 170 orange3 172 lightsalmon3 173
lightpink3 174 pink3 175 plum3 176 violet 177
gold3 178 lightgoldenrod3 179 tan 180 mistyrose3 181
thistle3 182 plum2 183 yellow3 184 khaki3 185
lightyellow3 187 grey84 188 lightsteelblue1 189 yellow2 190
darkolivegreen1 192 darkseagreen1 193 honeydew2 194 lightcyan1 195
red1 196 deeppink2 197 deeppink1 199 magenta2 200
magenta1 201 orangered1 202 indianred1 204 hotpink 206
mediumorchid1 207 darkorange 208 salmon1 209 lightcoral 210
palevioletred1 211 orchid2 212 orchid1 213 orange1 214
sandybrown 215 lightsalmon1 216 lightpink1 217 pink1 218
plum1 219 gold1 220 lightgoldenrod2 222 navajowhite1 223
mistyrose1 224 thistle1 225 yellow1 226 lightgoldenrod1 227
khaki1 228 wheat1 229 cornsilk1 230 grey100 231
grey3 232 grey7 233 grey11 234 grey15 235
grey19 236 grey23 237 grey27 238 grey30 239
grey35 240 grey39 241 grey42 242 grey46 243
grey50 244 grey54 245 grey58 246 grey62 247
grey66 248 grey70 249 grey74 250 grey78 251
grey82 252 grey85 253 grey89 254 grey93 255)
# For compatibility with Powerlevel9k.
#
# Type `getColorCode background` or `getColorCode foreground` to see the list of predefined colors.
function getColorCode() {
eval "$__p9k_intro"
if (( ARGC == 1 )); then
case $1 in
foreground)
local k
for k in "${(k@)__p9k_colors}"; do
local v=${__p9k_colors[$k]}
print -rP -- "%F{$v}$v - $k%f"
done
return 0
;;
background)
local k
for k in "${(k@)__p9k_colors}"; do
local v=${__p9k_colors[$k]}
print -rP -- "%K{$v}$v - $k%k"
done
return 0
;;
esac
fi
echo "Usage: getColorCode background|foreground" >&2
return 1
}
# _p9k_declare <type> <uppercase-name> [default]...
function _p9k_declare() {
local -i set=$+parameters[$2]
(( ARGC > 2 || set )) || return 0
case $1 in
-b)
if (( set )); then
[[ ${(P)2} == true ]] && typeset -gi _$2=1 || typeset -gi _$2=0
else
typeset -gi _$2=$3
fi
;;
-a)
local -a v=("${(@P)2}")
if (( set )); then
eval "typeset -ga _${(q)2}=(${(@qq)v})";
else
if [[ $3 != '--' ]]; then
echo "internal error in _p9k_declare " "${(qqq)@}" >&2
fi
eval "typeset -ga _${(q)2}=(${(@qq)*[4,-1]})"
fi
;;
-i)
(( set )) && typeset -gi _$2=$2 || typeset -gi _$2=$3
;;
-F)
(( set )) && typeset -gF _$2=$2 || typeset -gF _$2=$3
;;
-s)
(( set )) && typeset -g _$2=${(P)2} || typeset -g _$2=$3
;;
-e)
if (( set )); then
local v=${(P)2}
typeset -g _$2=${(g::)v}
else
typeset -g _$2=${(g::)3}
fi
;;
*)
echo "internal error in _p9k_declare " "${(qqq)@}" >&2
esac
}
function _p9k_read_word() {
local -a stat
zstat -A stat +mtime -- $1 2>/dev/null || stat=(-1)
local cached=$_p9k__read_word_cache[$1]
if [[ $cached == $stat[1]:* ]]; then
_p9k__ret=${cached#*:}
else
local rest
_p9k__ret=
{ read _p9k__ret rest <$1 } 2>/dev/null
_p9k__ret=${_p9k__ret%$'\r'}
_p9k__read_word_cache[$1]=$stat[1]:$_p9k__ret
fi
[[ -n $_p9k__ret ]]
}
function _p9k_fetch_cwd() {
if [[ $PWD == /* && $PWD -ef . ]]; then
_p9k__cwd=$PWD
else
_p9k__cwd=${${${:-.}:a}:-.}
fi
_p9k__cwd_a=${${_p9k__cwd:A}:-.}
case $_p9k__cwd in
/|.)
_p9k__parent_dirs=()
_p9k__parent_mtimes=()
_p9k__parent_mtimes_i=()
_p9k__parent_mtimes_s=
return
;;
~|~/*)
local parent=${${${:-~/..}:a}%/}/
local parts=(${(s./.)_p9k__cwd#$parent})
;;
*)
local parent=/
local parts=(${(s./.)_p9k__cwd})
;;
esac
local MATCH
_p9k__parent_dirs=(${(@)${:-{$#parts..1}}/(#m)*/$parent${(pj./.)parts[1,MATCH]}})
if ! zstat -A _p9k__parent_mtimes +mtime -- $_p9k__parent_dirs 2>/dev/null; then
_p9k__parent_mtimes=(${(@)parts/*/-1})
fi
_p9k__parent_mtimes_i=(${(@)${:-{1..$#parts}}/(#m)*/$MATCH:$_p9k__parent_mtimes[MATCH]})
_p9k__parent_mtimes_s="$_p9k__parent_mtimes_i"
}
# Usage: _p9k_glob parent_dir_index pattern
#
# parent_dir_index indexes _p9k__parent_dirs.
#
# Returns the number of matches.
#
# Pattern cannot have slashes.
#
# Example: _p9k_glob 3 '*.csproj'
function _p9k_glob() {
local dir=$_p9k__parent_dirs[$1]
local cached=$_p9k__glob_cache[$dir/$2]
if [[ $cached == $_p9k__parent_mtimes[$1]:* ]]; then
return ${cached##*:}
fi
local -a stat
zstat -A stat +mtime -- $dir 2>/dev/null || stat=(-1)
local files=($dir/$~2(N:t))
_p9k__glob_cache[$dir/$2]="$stat[1]:$#files"
return $#files
}
# Usage: _p9k_upglob pattern
#
# Returns index within _p9k__parent_dirs or 0 if there is no match.
#
# Search stops before reaching ~/../ or / and never matches in those directories.
#
# Example: _p9k_upglob '*.csproj'
function _p9k_upglob() {
local cached=$_p9k__upsearch_cache[$_p9k__cwd/$1]
if [[ -n $cached ]]; then
if [[ $_p9k__parent_mtimes_s == ${cached% *}(| *) ]]; then
return ${cached##* }
fi
cached=(${(s: :)cached})
local last_idx=$cached[-1]
cached[-1]=()
local -i i
for i in ${(@)${cached:|_p9k__parent_mtimes_i}%:*}; do
_p9k_glob $i $1 && continue
_p9k__upsearch_cache[$_p9k__cwd/$1]="${_p9k__parent_mtimes_i[1,i]} $i"
return i
done
if (( i != last_idx )); then
_p9k__upsearch_cache[$_p9k__cwd/$1]="${_p9k__parent_mtimes_i[1,$#cached]} $last_idx"
return last_idx
fi
i=$(($#cached + 1))
else
local -i i=1
fi
for ((; i <= $#_p9k__parent_mtimes; ++i)); do
_p9k_glob $i $1 && continue
_p9k__upsearch_cache[$_p9k__cwd/$1]="${_p9k__parent_mtimes_i[1,i]} $i"
return i
done
_p9k__upsearch_cache[$_p9k__cwd/$1]="$_p9k__parent_mtimes_s 0"
return 0
}
# If we execute `print -P $1`, how many characters will be printed on the last line?
# Assumes that `%{%}` and `%G` don't lie.
#
# _p9k_prompt_length '' => 0
# _p9k_prompt_length 'abc' => 3
# _p9k_prompt_length $'abc\nxy' => 2
# _p9k_prompt_length $'\t' => 8
# _p9k_prompt_length '%F{red}abc' => 3
# _p9k_prompt_length $'%{a\b%Gb%}' => 1
function _p9k_prompt_length() {
local -i COLUMNS=1024
local -i x y=${#1} m
if (( y )); then
while (( ${${(%):-$1%$y(l.1.0)}[-1]} )); do
x=y
(( y *= 2 ))
done
while (( y > x + 1 )); do
(( m = x + (y - x) / 2 ))
(( ${${(%):-$1%$m(l.x.y)}[-1]} = m ))
done
fi
typeset -g _p9k__ret=$x
}
typeset -gr __p9k_byte_suffix=('B' 'K' 'M' 'G' 'T' 'P' 'E' 'Z' 'Y')
# 512 => 512B
# 1800 => 1.76K
# 18000 => 17.6K
function _p9k_human_readable_bytes() {
typeset -F n=$1
local suf
for suf in $__p9k_byte_suffix; do
(( n < 1024 )) && break
(( n /= 1024 ))
done
if (( n >= 100 )); then
printf -v _p9k__ret '%.0f.' $n
elif (( n >= 10 )); then
printf -v _p9k__ret '%.1f' $n
else
printf -v _p9k__ret '%.2f' $n
fi
_p9k__ret=${${_p9k__ret%%0#}%.}$suf
}
if is-at-least 5.4; then
function _p9k_print_params() { typeset -p -- "$@" }
else
# Cannot use `typeset -p` unconditionally because of bugs in zsh.
function _p9k_print_params() {
local name
for name; do
case $parameters[$name] in
array*)
print -r -- "$name=(" "${(@q)${(@P)name}}" ")"
;;
association*)
# Cannot use "${(@q)${(@kvP)name}}" because of bugs in zsh.
local kv=("${(@kvP)name}")
print -r -- "$name=(" "${(@q)kv}" ")"
;;
*)
print -r -- "$name=${(q)${(P)name}}"
;;
esac
done
}
fi
# Determine if the passed segment is used in the prompt
#
# Pass the name of the segment to this function to test for its presence in
# either the LEFT or RIGHT prompt arrays.
# * $1: The segment to be tested.
_p9k_segment_in_use() {
(( $_POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[(I)$1(|_joined)] ||
$_POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS[(I)$1(|_joined)] ))
}
# Caching allows storing array-to-array associations. It should be used like this:
#
# if ! _p9k_cache_get "$key1" "$key2"; then
# # Compute val1 and val2 and then store them in the cache.
# _p9k_cache_set "$val1" "$val2"
# fi
# # Here ${_p9k__cache_val[1]} and ${_p9k__cache_val[2]} are $val1 and $val2 respectively.
#
# Limitations:
#
# * Calling _p9k_cache_set without arguments clears the cache entry. Subsequent calls to
# _p9k_cache_get for the same key will return an error.
# * There must be no intervening _p9k_cache_get calls between the associated _p9k_cache_get
# and _p9k_cache_set.
_p9k_cache_set() {
# Uncomment to see cache misses.
# echo "caching: ${(@0q)_p9k__cache_key} => (${(q)@})" >&2
_p9k_cache[$_p9k__cache_key]="${(pj:\0:)*}0"
_p9k__cache_val=("$@")
_p9k__state_dump_scheduled=1
}
_p9k_cache_get() {
_p9k__cache_key="${(pj:\0:)*}"
local v=$_p9k_cache[$_p9k__cache_key]
[[ -n $v ]] && _p9k__cache_val=("${(@0)${v[1,-2]}}")
}
_p9k_cache_ephemeral_set() {
# Uncomment to see cache misses.
# echo "caching: ${(@0q)_p9k__cache_key} => (${(q)@})" >&2
_p9k__cache_ephemeral[$_p9k__cache_key]="${(pj:\0:)*}0"
_p9k__cache_val=("$@")
}
_p9k_cache_ephemeral_get() {
_p9k__cache_key="${(pj:\0:)*}"
local v=$_p9k__cache_ephemeral[$_p9k__cache_key]
[[ -n $v ]] && _p9k__cache_val=("${(@0)${v[1,-2]}}")
}
_p9k_cache_stat_get() {
local -H stat
local label=$1 f
shift
_p9k__cache_stat_meta=
_p9k__cache_stat_fprint=
for f; do
if zstat -H stat -- $f 2>/dev/null; then
_p9k__cache_stat_meta+="${(q)f} $stat[inode] $stat[mtime] $stat[size] $stat[mode]; "
fi
done
if _p9k_cache_get $0 $label meta "$@"; then
if [[ $_p9k__cache_val[1] == $_p9k__cache_stat_meta ]]; then
_p9k__cache_stat_fprint=$_p9k__cache_val[2]
local -a key=($0 $label fprint "$@" "$_p9k__cache_stat_fprint")
_p9k__cache_fprint_key="${(pj:\0:)key}"
shift 2 _p9k__cache_val
return 0
else
local -a key=($0 $label fprint "$@" "$_p9k__cache_val[2]")
_p9k__cache_ephemeral[${(pj:\0:)key}]="${(pj:\0:)_p9k__cache_val[3,-1]}0"
fi
fi
if (( $+commands[md5] )); then
_p9k__cache_stat_fprint="$(md5 -- $* 2>&1)"
elif (( $+commands[md5sum] )); then
_p9k__cache_stat_fprint="$(md5sum -b -- $* 2>&1)"
else
return 1
fi
local meta_key=$_p9k__cache_key
if _p9k_cache_ephemeral_get $0 $label fprint "$@" "$_p9k__cache_stat_fprint"; then
_p9k__cache_fprint_key=$_p9k__cache_key
_p9k__cache_key=$meta_key
_p9k_cache_set "$_p9k__cache_stat_meta" "$_p9k__cache_stat_fprint" "$_p9k__cache_val[@]"
shift 2 _p9k__cache_val
return 0
fi
_p9k__cache_fprint_key=$_p9k__cache_key
_p9k__cache_key=$meta_key
return 1
}
_p9k_cache_stat_set() {
_p9k_cache_set "$_p9k__cache_stat_meta" "$_p9k__cache_stat_fprint" "$@"
_p9k__cache_key=$_p9k__cache_fprint_key
_p9k_cache_ephemeral_set "$@"
}
# _p9k_param prompt_foo_BAR BACKGROUND red
_p9k_param() {
local key="_p9k_param ${(pj:\0:)*}"
_p9k__ret=$_p9k_cache[$key]
if [[ -n $_p9k__ret ]]; then
_p9k__ret[-1,-1]=''
else
if [[ ${1//-/_} == (#b)prompt_([a-z0-9_]#)(*) ]]; then
local var=_POWERLEVEL9K_${${(U)match[1]}//İ/I}$match[2]_$2
if (( $+parameters[$var] )); then
_p9k__ret=${(P)var}
else
var=_POWERLEVEL9K_${${(U)match[1]%_}//İ/I}_$2
if (( $+parameters[$var] )); then
_p9k__ret=${(P)var}
else
var=_POWERLEVEL9K_$2
if (( $+parameters[$var] )); then
_p9k__ret=${(P)var}
else
_p9k__ret=$3
fi
fi
fi
else
local var=_POWERLEVEL9K_$2
if (( $+parameters[$var] )); then
_p9k__ret=${(P)var}
else
_p9k__ret=$3
fi
fi
_p9k_cache[$key]=${_p9k__ret}.
fi
}
# _p9k_get_icon prompt_foo_BAR BAZ_ICON quix
_p9k_get_icon() {
local key="_p9k_get_icon ${(pj:\0:)*}"
_p9k__ret=$_p9k_cache[$key]
if [[ -n $_p9k__ret ]]; then
_p9k__ret[-1,-1]=''
else
if [[ $2 == $'\1'* ]]; then
_p9k__ret=${2[2,-1]}
else
_p9k_param "$1" "$2" ${icons[$2]-$'\1'$3}
if [[ $_p9k__ret == $'\1'* ]]; then
_p9k__ret=${_p9k__ret[2,-1]}
else
_p9k__ret=${(g::)_p9k__ret}
[[ $_p9k__ret != $'\b'? ]] || _p9k__ret="%{$_p9k__ret%}" # penance for past sins
fi
fi
_p9k_cache[$key]=${_p9k__ret}.
fi
}
_p9k_translate_color() {
if [[ $1 == <-> ]]; then # decimal color code: 255
_p9k__ret=${(l.3..0.)1}
elif [[ $1 == '#'[[:xdigit:]]## ]]; then # hexademical color code: #ffffff
_p9k__ret=${${(L)1}//ı/i}
else # named color: red
# Strip prifixes if there are any.
_p9k__ret=$__p9k_colors[${${${1#bg-}#fg-}#br}]
fi
}
# _p9k_color prompt_foo_BAR BACKGROUND red
_p9k_color() {
local key="_p9k_color ${(pj:\0:)*}"
_p9k__ret=$_p9k_cache[$key]
if [[ -n $_p9k__ret ]]; then
_p9k__ret[-1,-1]=''
else
_p9k_param "$@"
_p9k_translate_color $_p9k__ret
_p9k_cache[$key]=${_p9k__ret}.
fi
}
# _p9k_vcs_style CLEAN REMOTE_BRANCH
_p9k_vcs_style() {
local key="$0 ${(pj:\0:)*}"
_p9k__ret=$_p9k_cache[$key]
if [[ -n $_p9k__ret ]]; then
_p9k__ret[-1,-1]=''
else
local style=%b # TODO: support bold
_p9k_color prompt_vcs_$1 BACKGROUND "${__p9k_vcs_states[$1]}"
_p9k_background $_p9k__ret
style+=$_p9k__ret
local var=_POWERLEVEL9K_VCS_${1}_${2}FORMAT_FOREGROUND
if (( $+parameters[$var] )); then
_p9k_translate_color "${(P)var}"
else
var=_POWERLEVEL9K_VCS_${2}FORMAT_FOREGROUND
if (( $+parameters[$var] )); then
_p9k_translate_color "${(P)var}"
else
_p9k_color prompt_vcs_$1 FOREGROUND "$_p9k_color1"
fi
fi
_p9k_foreground $_p9k__ret
_p9k__ret=$style$_p9k__ret
_p9k_cache[$key]=${_p9k__ret}.
fi
}
_p9k_background() {
[[ -n $1 ]] && _p9k__ret="%K{$1}" || _p9k__ret="%k"
}
_p9k_foreground() {
# Note: This code used to produce `%1F` instead of `%F{1}` because it's more efficient.
# Unfortunately, this triggers a bug in zsh. Namely, `%1F{2}` gets percent-expanded as if
# it was `%F{2}`.
[[ -n $1 ]] && _p9k__ret="%F{$1}" || _p9k__ret="%f"
}
_p9k_escape_style() {
[[ $1 == *'}'* ]] && _p9k__ret='${:-"'$1'"}' || _p9k__ret=$1
}
_p9k_escape() {
[[ $1 == *["~!#\`\$^&*()\\\"'<>?{}[]"]* ]] && _p9k__ret="\${(Q)\${:-${(qqq)${(q)1}}}}" || _p9k__ret=$1
}
# * $1: Name of the function that was originally invoked.
# Necessary, to make the dynamic color-overwrite mechanism work.
# * $2: Background color.
# * $3: Foreground color.
# * $4: An identifying icon.
# * $5: 1 to to perform parameter expansion and process substitution.
# * $6: If not empty but becomes empty after parameter expansion and process substitution,
# the segment isn't rendered.
# * $7: Content.
_p9k_left_prompt_segment() {
if ! _p9k_cache_get "$0" "$1" "$2" "$3" "$4" "$_p9k__segment_index"; then
_p9k_color $1 BACKGROUND $2
local bg_color=$_p9k__ret
_p9k_background $bg_color
local bg=$_p9k__ret
_p9k_color $1 FOREGROUND $3
local fg_color=$_p9k__ret
_p9k_foreground $fg_color
local fg=$_p9k__ret
local style=%b$bg$fg
local style_=${style//\}/\\\}}
_p9k_get_icon $1 LEFT_SEGMENT_SEPARATOR
local sep=$_p9k__ret
_p9k_escape $_p9k__ret
local sep_=$_p9k__ret
_p9k_get_icon $1 LEFT_SUBSEGMENT_SEPARATOR
_p9k_escape $_p9k__ret
local subsep_=$_p9k__ret
local icon_
if [[ -n $4 ]]; then
_p9k_get_icon $1 $4
_p9k_escape $_p9k__ret
icon_=$_p9k__ret
fi
_p9k_get_icon $1 LEFT_PROMPT_FIRST_SEGMENT_START_SYMBOL
local start_sep=$_p9k__ret
[[ -n $start_sep ]] && start_sep="%b%k%F{$bg_color}$start_sep"
_p9k_get_icon $1 LEFT_PROMPT_LAST_SEGMENT_END_SYMBOL $sep
_p9k_escape $_p9k__ret
local end_sep_=$_p9k__ret
_p9k_get_icon $1 WHITESPACE_BETWEEN_LEFT_SEGMENTS ' '
local space=$_p9k__ret
_p9k_get_icon $1 LEFT_LEFT_WHITESPACE $space
local left_space=$_p9k__ret
[[ $left_space == *%* ]] && left_space+=$style
_p9k_get_icon $1 LEFT_RIGHT_WHITESPACE $space
_p9k_escape $_p9k__ret
local right_space_=$_p9k__ret
[[ $right_space_ == *%* ]] && right_space_+=$style_
local s='<_p9k__s>' ss='<_p9k__ss>'
local -i non_hermetic=0
# Segment separator logic:
#
# if [[ $_p9k__bg == NONE ]]; then
# 1
# elif (( joined )); then
# 2
# elif [[ $bg_color == (${_p9k__bg}|${_p9k__bg:-0}) ]]; then
# 3
# else
# 4
# fi
local t=$(($#_p9k_t - __p9k_ksh_arrays))
_p9k_t+=$start_sep$style$left_space # 1
_p9k_t+=$style # 2
if [[ -n $fg_color && $fg_color == $bg_color ]]; then
if [[ $fg_color == $_p9k_color1 ]]; then
_p9k_foreground $_p9k_color2
else
_p9k_foreground $_p9k_color1
fi
_p9k_t+=%b$bg$_p9k__ret$ss$style$left_space # 3
else
_p9k_t+=%b$bg$ss$style$left_space # 3
fi
_p9k_t+=%b$bg$s$style$left_space # 4
local join="_p9k__i>=$_p9k_left_join[$_p9k__segment_index]"
_p9k_param $1 SELF_JOINED false
if [[ $_p9k__ret == false ]]; then
if (( _p9k__segment_index > $_p9k_left_join[$_p9k__segment_index] )); then
join+="&&_p9k__i<$_p9k__segment_index"
else
join=
fi
fi
local p=
p+="\${_p9k__n::=}"
p+="\${\${\${_p9k__bg:-0}:#NONE}:-\${_p9k__n::=$((t+1))}}" # 1
if [[ -n $join ]]; then
p+="\${_p9k__n:=\${\${\$(($join)):#0}:+$((t+2))}}" # 2
fi
if (( __p9k_sh_glob )); then
p+="\${_p9k__n:=\${\${(M)\${:-x$bg_color}:#x\$_p9k__bg}:+$((t+3))}}" # 3
p+="\${_p9k__n:=\${\${(M)\${:-x$bg_color}:#x\$${_p9k__bg:-0}}:+$((t+3))}}" # 3
else
p+="\${_p9k__n:=\${\${(M)\${:-x$bg_color}:#x(\$_p9k__bg|\${_p9k__bg:-0})}:+$((t+3))}}" # 3
fi
p+="\${_p9k__n:=$((t+4))}" # 4
_p9k_param $1 VISUAL_IDENTIFIER_EXPANSION '${P9K_VISUAL_IDENTIFIER}'
[[ $_p9k__ret == (|*[^\\])'$('* ]] && non_hermetic=1
local icon_exp_=${_p9k__ret:+\"$_p9k__ret\"}
_p9k_param $1 CONTENT_EXPANSION '${P9K_CONTENT}'
[[ $_p9k__ret == (|*[^\\])'$('* ]] && non_hermetic=1
local content_exp_=${_p9k__ret:+\"$_p9k__ret\"}
if [[ ( $icon_exp_ != '"${P9K_VISUAL_IDENTIFIER}"' && $icon_exp_ == *'$'* ) ||
( $content_exp_ != '"${P9K_CONTENT}"' && $content_exp_ == *'$'* ) ]]; then
p+="\${P9K_VISUAL_IDENTIFIER::=$icon_}"
fi
local -i has_icon=-1 # maybe
if [[ $icon_exp_ != '"${P9K_VISUAL_IDENTIFIER}"' && $icon_exp_ == *'$'* ]]; then
p+='${_p9k__v::='$icon_exp_$style_'}'
else
[[ $icon_exp_ == '"${P9K_VISUAL_IDENTIFIER}"' ]] && _p9k__ret=$icon_ || _p9k__ret=$icon_exp_
if [[ -n $_p9k__ret ]]; then
p+="\${_p9k__v::=$_p9k__ret"
[[ $_p9k__ret == *%* ]] && p+=$style_
p+="}"
has_icon=1 # definitely yes
else
has_icon=0 # definitely no
fi
fi
p+='${_p9k__c::='$content_exp_'}${_p9k__c::=${_p9k__c//'$'\r''}}'
p+='${_p9k__e::=${${_p9k__'${_p9k__line_index}l${${1#prompt_}%%[A-Z0-9_]#}'+00}:-'
if (( has_icon == -1 )); then
p+='${${(%):-$_p9k__c%1(l.1.0)}[-1]}${${(%):-$_p9k__v%1(l.1.0)}[-1]}}'
else
p+='${${(%):-$_p9k__c%1(l.1.0)}[-1]}'$has_icon'}'
fi
p+='}}+}'
p+='${${_p9k__e:#00}:+${${_p9k_t[$_p9k__n]/'$ss'/$_p9k__ss}/'$s'/$_p9k__s}'
_p9k_param $1 ICON_BEFORE_CONTENT ''
if [[ $_p9k__ret != false ]]; then
_p9k_param $1 PREFIX ''
_p9k__ret=${(g::)_p9k__ret}
_p9k_escape $_p9k__ret
p+=$_p9k__ret
[[ $_p9k__ret == *%* ]] && local -i need_style=1 || local -i need_style=0
if (( has_icon != 0 )); then
_p9k_color $1 VISUAL_IDENTIFIER_COLOR $fg_color
_p9k_foreground $_p9k__ret
_p9k__ret=%b$bg$_p9k__ret
_p9k__ret=${_p9k__ret//\}/\\\}}
if [[ $_p9k__ret != $style_ ]]; then
p+=$_p9k__ret'${_p9k__v}'$style_
else
(( need_style )) && p+=$style_
p+='${_p9k__v}'
fi
_p9k_get_icon $1 LEFT_MIDDLE_WHITESPACE ' '
if [[ -n $_p9k__ret ]]; then
_p9k_escape $_p9k__ret
[[ _p9k__ret == *%* ]] && _p9k__ret+=$style_
p+='${${(M)_p9k__e:#11}:+'$_p9k__ret'}'
fi
elif (( need_style )); then
p+=$style_
fi
p+='${_p9k__c}'$style_
else
_p9k_param $1 PREFIX ''
_p9k__ret=${(g::)_p9k__ret}
_p9k_escape $_p9k__ret
p+=$_p9k__ret
[[ $_p9k__ret == *%* ]] && p+=$style_
p+='${_p9k__c}'$style_
if (( has_icon != 0 )); then
local -i need_style=0
_p9k_get_icon $1 LEFT_MIDDLE_WHITESPACE ' '
if [[ -n $_p9k__ret ]]; then
_p9k_escape $_p9k__ret
[[ $_p9k__ret == *%* ]] && need_style=1
p+='${${(M)_p9k__e:#11}:+'$_p9k__ret'}'
fi
_p9k_color $1 VISUAL_IDENTIFIER_COLOR $fg_color
_p9k_foreground $_p9k__ret
_p9k__ret=%b$bg$_p9k__ret
_p9k__ret=${_p9k__ret//\}/\\\}}
[[ $_p9k__ret != $style_ || $need_style == 1 ]] && p+=$_p9k__ret
p+='$_p9k__v'
fi
fi
_p9k_param $1 SUFFIX ''
_p9k__ret=${(g::)_p9k__ret}
_p9k_escape $_p9k__ret
p+=$_p9k__ret
[[ $_p9k__ret == *%* && -n $right_space_ ]] && p+=$style_
p+=$right_space_
p+='${${:-'
p+="\${_p9k__s::=%F{$bg_color\}$sep_}\${_p9k__ss::=$subsep_}\${_p9k__sss::=%F{$bg_color\}$end_sep_}"
p+="\${_p9k__i::=$_p9k__segment_index}\${_p9k__bg::=$bg_color}"
p+='}+}'
p+='}'
_p9k_param $1 SHOW_ON_UPGLOB ''
_p9k_cache_set "$p" $non_hermetic $_p9k__ret
fi
if [[ -n $_p9k__cache_val[3] ]]; then
_p9k__has_upglob=1
_p9k_upglob $_p9k__cache_val[3] && return
fi
_p9k__non_hermetic_expansion=$_p9k__cache_val[2]
(( $5 )) && _p9k__ret=\"$7\" || _p9k_escape $7
if [[ -z $6 ]]; then
_p9k__prompt+="\${\${:-\${P9K_CONTENT::=$_p9k__ret}$_p9k__cache_val[1]"
else
_p9k__prompt+="\${\${:-\"$6\"}:+\${\${:-\${P9K_CONTENT::=$_p9k__ret}$_p9k__cache_val[1]}"
fi
}
# The same as _p9k_left_prompt_segment above but for the right prompt.
_p9k_right_prompt_segment() {
if ! _p9k_cache_get "$0" "$1" "$2" "$3" "$4" "$_p9k__segment_index"; then
_p9k_color $1 BACKGROUND $2
local bg_color=$_p9k__ret
_p9k_background $bg_color
local bg=$_p9k__ret
local bg_=${_p9k__ret//\}/\\\}}
_p9k_color $1 FOREGROUND $3
local fg_color=$_p9k__ret
_p9k_foreground $fg_color
local fg=$_p9k__ret
local style=%b$bg$fg
local style_=${style//\}/\\\}}
_p9k_get_icon $1 RIGHT_SEGMENT_SEPARATOR
local sep=$_p9k__ret
_p9k_escape $_p9k__ret
local sep_=$_p9k__ret
_p9k_get_icon $1 RIGHT_SUBSEGMENT_SEPARATOR
local subsep=$_p9k__ret
[[ $subsep == *%* ]] && subsep+=$style
local icon_
if [[ -n $4 ]]; then
_p9k_get_icon $1 $4
_p9k_escape $_p9k__ret
icon_=$_p9k__ret
fi
_p9k_get_icon $1 RIGHT_PROMPT_FIRST_SEGMENT_START_SYMBOL $sep
local start_sep=$_p9k__ret
[[ -n $start_sep ]] && start_sep="%b%k%F{$bg_color}$start_sep"
_p9k_get_icon $1 RIGHT_PROMPT_LAST_SEGMENT_END_SYMBOL
_p9k_escape $_p9k__ret
local end_sep_=$_p9k__ret
_p9k_get_icon $1 WHITESPACE_BETWEEN_RIGHT_SEGMENTS ' '
local space=$_p9k__ret
_p9k_get_icon $1 RIGHT_LEFT_WHITESPACE $space
local left_space=$_p9k__ret
[[ $left_space == *%* ]] && left_space+=$style
_p9k_get_icon $1 RIGHT_RIGHT_WHITESPACE $space
_p9k_escape $_p9k__ret
local right_space_=$_p9k__ret
[[ $right_space_ == *%* ]] && right_space_+=$style_
local w='<_p9k__w>' s='<_p9k__s>'
local -i non_hermetic=0
# Segment separator logic:
#
# if [[ $_p9k__bg == NONE ]]; then
# 1
# elif (( joined )); then
# 2
# elif [[ $_p9k__bg == (${bg_color}|${bg_color:-0}) ]]; then
# 3
# else
# 4
# fi
local t=$(($#_p9k_t - __p9k_ksh_arrays))
_p9k_t+=$start_sep$style$left_space # 1
_p9k_t+=$w$style # 2
_p9k_t+=$w$style$subsep$left_space # 3
_p9k_t+=$w%F{$bg_color}$sep$style$left_space # 4
local join="_p9k__i>=$_p9k_right_join[$_p9k__segment_index]"
_p9k_param $1 SELF_JOINED false
if [[ $_p9k__ret == false ]]; then
if (( _p9k__segment_index > $_p9k_right_join[$_p9k__segment_index] )); then
join+="&&_p9k__i<$_p9k__segment_index"
else
join=
fi
fi
local p=
p+="\${_p9k__n::=}"
p+="\${\${\${_p9k__bg:-0}:#NONE}:-\${_p9k__n::=$((t+1))}}" # 1
if [[ -n $join ]]; then
p+="\${_p9k__n:=\${\${\$(($join)):#0}:+$((t+2))}}" # 2
fi
if (( __p9k_sh_glob )); then
p+="\${_p9k__n:=\${\${(M)\${:-x\$_p9k__bg}:#x${(b)bg_color}}:+$((t+3))}}" # 3
p+="\${_p9k__n:=\${\${(M)\${:-x\$_p9k__bg}:#x${(b)bg_color:-0}}:+$((t+3))}}" # 3
else
p+="\${_p9k__n:=\${\${(M)\${:-x\$_p9k__bg}:#x(${(b)bg_color}|${(b)bg_color:-0})}:+$((t+3))}}" # 3
fi
p+="\${_p9k__n:=$((t+4))}" # 4
_p9k_param $1 VISUAL_IDENTIFIER_EXPANSION '${P9K_VISUAL_IDENTIFIER}'
[[ $_p9k__ret == (|*[^\\])'$('* ]] && non_hermetic=1
local icon_exp_=${_p9k__ret:+\"$_p9k__ret\"}
_p9k_param $1 CONTENT_EXPANSION '${P9K_CONTENT}'
[[ $_p9k__ret == (|*[^\\])'$('* ]] && non_hermetic=1
local content_exp_=${_p9k__ret:+\"$_p9k__ret\"}
if [[ ( $icon_exp_ != '"${P9K_VISUAL_IDENTIFIER}"' && $icon_exp_ == *'$'* ) ||
( $content_exp_ != '"${P9K_CONTENT}"' && $content_exp_ == *'$'* ) ]]; then
p+="\${P9K_VISUAL_IDENTIFIER::=$icon_}"
fi
local -i has_icon=-1 # maybe
if [[ $icon_exp_ != '"${P9K_VISUAL_IDENTIFIER}"' && $icon_exp_ == *'$'* ]]; then
p+="\${_p9k__v::=$icon_exp_$style_}"
else
[[ $icon_exp_ == '"${P9K_VISUAL_IDENTIFIER}"' ]] && _p9k__ret=$icon_ || _p9k__ret=$icon_exp_
if [[ -n $_p9k__ret ]]; then
p+="\${_p9k__v::=$_p9k__ret"
[[ $_p9k__ret == *%* ]] && p+=$style_
p+="}"
has_icon=1 # definitely yes
else
has_icon=0 # definitely no
fi
fi
p+='${_p9k__c::='$content_exp_'}${_p9k__c::=${_p9k__c//'$'\r''}}'
p+='${_p9k__e::=${${_p9k__'${_p9k__line_index}r${${1#prompt_}%%[A-Z0-9_]#}'+00}:-'
if (( has_icon == -1 )); then
p+='${${(%):-$_p9k__c%1(l.1.0)}[-1]}${${(%):-$_p9k__v%1(l.1.0)}[-1]}}'
else
p+='${${(%):-$_p9k__c%1(l.1.0)}[-1]}'$has_icon'}'
fi
p+='}}+}'
p+='${${_p9k__e:#00}:+${_p9k_t[$_p9k__n]/'$w'/$_p9k__w}'
_p9k_param $1 ICON_BEFORE_CONTENT ''
if [[ $_p9k__ret != true ]]; then
_p9k_param $1 PREFIX ''
_p9k__ret=${(g::)_p9k__ret}
_p9k_escape $_p9k__ret
p+=$_p9k__ret
[[ $_p9k__ret == *%* ]] && p+=$style_
p+='${_p9k__c}'$style_