-
Notifications
You must be signed in to change notification settings - Fork 1
/
newt.zsh-theme
1244 lines (1016 loc) · 35.5 KB
/
newt.zsh-theme
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
zmodload zsh/datetime
zmodload zsh/parameter
zmodload zsh/mathfunc
# Prompt Segments {{{1
# + context: user@host {{{1
__newt+context+precmd () {
local u=;
local h="${(%):-%m}"
# Only show user if it's other than DEFAULT_USER. Treat root as
# another default user, since prompt color shows we're privileged.
[[ $+DEFAULT_USER -eq 1 && $USER != $DEFAULT_USER && $USER != root ]] \
&& u=$USER
# Hide hostname if on local host (not SSH) or inside tmux (let TMUX
# status show host info)
[[ -z $SSH_CLIENT || -n $TMUX ]] && h=
if [[ -n $h ]]; then
[[ -n $u ]] && u=${u}@
u=${u}${h}
fi
__newt[+context+]=$u
}
# + dir: Current directory {{{1
__newt+dir+setup () {
__newt_default dir default '%4~'
__newt_default dir \* bg "$__newt[color3]"
__newt_default dir \* fg "$__newt[color4]"
__newt_default dir root bg "$__newt[color5]"
__newt_default dir root fg "$__newt[color6]"
}
__newt+dir+precmd () {
__newt[+dir+]=$(__newt_zstyle dir default)
[[ $EUID = 0 ]] \
&& __newt[+dir+state]=root \
|| __newt[+dir+state]=
}
# + exec_time: Execution time of last command {{{1
__newt+exec_time+setup () {
__newt_default exec_time long $'\u9593%t' # 間
__newt_default exec_time threshold 5
}
__newt+exec_time+preexec () {
__newt[+exec_time+start]=$EPOCHREALTIME
}
__newt+exec_time+precmd () {
local state
local stop=$EPOCHREALTIME
local start=${__newt[+exec_time+start]:-$stop}
local threshold="$(__newt_zstyle exec_time threshold)"
local -F elapsed=$((stop - start))
if (( $elapsed >= $threshold )); then
state=long
else
state=default
fi
local precision="$(__newt_zstyle exec_time precision)"
if [[ -z $precision ]]; then
(( elapsed < 10 )) && precision=1 || precision=0
fi
__newt[+exec_time+state]=$state
zformat -f '__newt[+exec_time+]' \
"$(__newt_zstyle exec_time $state)" \
t:"$(__newt_human_time $elapsed $precision)" \
s:"$(printf '%.*f' $precision $elapsed)"
__newt[+exec_time+start]=
}
__newt_human_time () {
local s t=$1
local -i m h d precision=${2:-3}
((
s = t % 60, t = floor(t) / 60,
m = t % 60, t = t / 60,
h = t % 24, t = t / 24,
d = t
))
t=
((d)) && t+=${d}d
((h)) && t+=${h}h
((m)) && t+=${m}m
printf -v s '%.*fs' $precision $s
t+=$s
print -rn $t
}
# + history: Current history {{{1
__newt+history+precmd () {
__newt[+history+]=$(__newt_zstyle -d '%!' history default)
}
# + jobs: Background jobs {{{1
__newt+jobs+setup () {
__newt_default jobs default $'\u2699'' %1(j:%2(j,%j,):-)'
}
__newt+jobs+precmd () {
# \u2699 is ⚙
(( ${(%):-%j} )) \
&& __newt[+jobs+]=$(__newt_zstyle jobs default) \
|| __newt[+jobs+]=$(__newt_zstyle jobs zero)
}
# + none: Placeholder to do nothing {{{1
__newt+none+setup () {
}
# + notice: Generic info display {{{1
__newt+notice+add-note () {
__newt[+notice+notes]+=" $*"
}
__newt+notice+precmd () {
__newt[+notice+]=$__newt[+notice+notes]
unset '__newt[+notice+notes]'
}
# + prompt_time: How long the prompt takes to draw {{{1
# Install this precmd function specially so it gets called first
__newt_prompt_time_precmd () {
__newt[+prompt_time+start]=$EPOCHREALTIME
}
__newt+prompt_time+setup () {
precmd_functions[1,0]=(__newt_prompt_time_precmd)
local i=2
while (( i <= $#precmd_functions )); do
if [[ $precmd_functions[$i] = __newt_prompt_time_precmd ]]; then
# Remove it
precmd_functions[$i]=()
else
i=$((i+1))
fi
done
}
__newt+prompt_time+zle-line-init () {
local now=$EPOCHREALTIME
local elapsed=$((now - __newt[+prompt_time+start]))
printf -v '__newt[+prompt_time+]' '%.*f' \
$(__newt_zstyle -d 6 prompt_time precision) $elapsed
}
# + status: Exit status of last command {{{1
# NB Special case precmd func
__newt_precmd_save_status () {
# This should be first, to save status from user's command
__newt[save_status]=$?
}
__newt+status+setup () {
__newt[save_status]=0
__newt_default status error $'\u2718 %?' # ✘
__newt_default status error bg "$__newt[color5]"
__newt_default status error fg "$__newt[color6]"
#__newt_default status ok $'\u2713' # ✓
__newt_default status ok fg "$__newt[color-green]"
#__newt_default status suspended $'\u25c6' # ◆
__newt_default status suspended fg "$__newt[color-yellow]"
}
__newt+status+preexec () {
# A command is being run, so clear flag
unset '__newt[+status+done]'
}
__newt+status+precmd () {
local state
case $__newt[save_status] in
0) state=ok ;;
20|148) state=suspended ;;
*) state=error ;;
esac
# Ignore if no new command has been run since last status was shown
(( $+__newt[+status+done] )) && state=nocommand
__newt[+status+state]=$state
zformat -f '__newt[+status+]' "$(__newt_zstyle status $state)" \
'?':$__newt[save_status]
__newt[+status+done]=1
}
# + time: Current time {{{1
__newt+time+precmd () {
__newt[+time+]=$(__newt_zstyle -d %T time default)
}
# + vcs: Version control {{{1
__newt+vcs+setup () {
local green=$(__newt_fg_color $__newt[color-green])
local yellow=$(__newt_fg_color $__newt[color-yellow])
local red=$(__newt_fg_color $__newt[color-red])
#__newt_other_default ':vcs_info:*+*:*' debug true
__newt_other_default ':vcs_info:*' check-for-changes true
__newt_other_default ':vcs_info:*' stagedstr $green$'\u25cf' # ●
__newt_other_default ':vcs_info:*' untrackedstr $yellow$'\u25cf' # ●
__newt_other_default ':vcs_info:*' unstagedstr $red$'\u25cf' # ●
__newt_other_default ':vcs_info:*' formats $'\ue0a0%m%u%c %f%b' #
__newt_other_default ':vcs_info:*' actionformats $'\ue0a0 %b|%a%f' #
__newt_other_default ':vcs_info:git*+post-backend:*' hooks \
newt-show-gitdir \
newt-remotebranch \
newt-upstream \
# ∴
__newt_other_default ':vcs_info:git*+set-message:*' hooks \
newt-untracked \
newt-finalize \
# ∴
__newt_default vcs \* bg "$__newt[color3]"
__newt_default vcs \* fg "$__newt[color4]"
__newt_default vcs clobbered bg "$__newt[color5]"
__newt_default vcs clobbered fg "$__newt[color6]"
__newt_default vcs root bg "$__newt[color5]"
__newt_default vcs root fg "$__newt[color6]"
}
__newt+vcs+precmd () {
vcs_info 2> /dev/null
if (( $+__newt[+vcs+clobber] )); then
__newt[+vcs+state]=clobbered
elif [[ ${EUID} = 0 ]]; then
# Generally it's a mistake to use a VCS as root
__newt[+vcs+state]=root
elif (( $+__newt[+vcs+action] )); then
__newt[+vcs+state]=action
elif (( $+__newt[+vcs+dirty] )); then
__newt[+vcs+state]=dirty
else
unset '__newt[+vcs+state]'
fi
__newt[+vcs+]=$vcs_info_msg_0_
unset '__newt[+vcs+clobber]'
unset '__newt[+vcs+action]'
unset '__newt[+vcs+dirty]'
}
# + vi_mode: Line editing mode {{{1
__newt+vi_mode+setup () {
__newt_default vi_mode viins ''
__newt_default vi_mode vicmd NORMAL
__newt_default vi_mode replace REPLACE
__newt_default vi_mode isearch SEARCH
__newt_default vi_mode visual VISUAL
__newt_default vi_mode vline V-LINE
local vary const const_color
[[ $__newt[color1] = '' ]] \
&& vary=fg const=bg const_color='' \
|| vary=bg const=fg const_color='bg:'
__newt_default vi_mode \* $const "$const_color"
__newt_default vi_mode viins $vary "$__newt[color-yellow]"
__newt_default vi_mode vicmd $vary "$__newt[color-green]"
__newt_default vi_mode replace $vary "$__newt[color-cyan]"
__newt_default vi_mode isearch $vary "$__newt[color-magenta]"
__newt_default vi_mode visual $vary "$__newt[color-blue]"
__newt_default vi_mode vline $vary "$__newt[color-blue]"
}
__newt+vi_mode+precmd () { __newt+vi_mode+hook "$@" }
__newt+vi_mode+zle-isearch-update () { __newt+vi_mode+hook "$@" }
__newt+vi_mode+zle-isearch-exit () { __newt+vi_mode+hook "$@" }
__newt+vi_mode+zle-line-pre-redraw () { __newt+vi_mode+hook "$@" }
__newt+vi_mode+hook () {
local mode="${VIM_MODE_KEYMAP-$KEYMAP}"
#__newt_debug "vi_mode: ${__newt[+vi_mode+state]} -> $mode [$@]"
case $mode in
viins|vicmd|replace|isearch|visual|vline) ;;
main|*) mode=viins ;;
esac
local str=$(__newt_zstyle vi_mode $mode)
[[ $mode = $__newt[+vi_mode+state] && $str = $__newt[+vi_mode+] ]] \
&& return 1
#__newt_debug " + ${__newt[+vi_mode+state]} -> $mode"
__newt[+vi_mode+state]=$mode
__newt[+vi_mode+]=$str
}
# VCS_Info hooks for git {{{1
# + $GITDIR {{{1
+vi-newt-show-gitdir () {
local inner
if (( $+GIT_DIR )); then
() {
# See if we're in a shadowed repository
local GIT_DIR GIT_WORK_TREE
inner=$(${vcs_comm[cmd]} rev-parse --verify HEAD 2> /dev/null)
}
if [[ -n $inner && $inner != $hook_com[revision] ]]; then
# GIT_DIR is shadowing a different repo. This can be
# very confusing! Set a flag to trigger an alert.
__newt[+vcs+clobber]=1
fi
local gdir; print -v gdir -D $GIT_DIR
gdir="$(__newt_fg_color "$__newt[color-cyan]")$gdir"$'\u2261'"%f" # ≡
hook_com[branch]="$gdir${hook_com[branch]}"
fi
}
# + Tracking remote branch? {{{1
+vi-newt-remotebranch () {
local remote
# Are we on a remote-tracking branch?
remote=${$(${vcs_comm[cmd]} rev-parse --verify ${hook_com[branch_orig]}@{upstream} \
--symbolic-full-name 2>/dev/null)#refs/remotes/}
# The first test will show a tracking branch whenever there is one. The
# second test, however, will only show the remote branch's name if it
# differs from the local one.
#if [[ -n ${remote} ]] ; then
if [[ -n ${remote} && ${remote#*/} != ${hook_com[branch_orig]} ]] ; then
hook_com[branch]+="$(__newt_fg_color "$__newt[color-cyan]")"$'\u00a4'"${remote}" # ¤
fi
}
# + New untracked files? {{{1
+vi-newt-untracked () {
if [[ $(${vcs_comm[cmd]} rev-parse --is-inside-work-tree 2> /dev/null) = 'true' ]] \
&& ${vcs_comm[cmd]} status --porcelain | command grep -m 1 '^??' &>/dev/null
then
local str; zstyle -s :vcs_info:\* untrackedstr str
hook_com[unstaged]+=${str:-T}
fi
}
+vi-newt-finalize () {
[[ -n $hook_com[unstaged] ]] && __newt[+vcs+dirty]=1
[[ -n $hook_com[action] ]] && __newt[+vcs+action]=1
}
# + Ahead / behind of upstream? {{{1
+vi-newt-upstream () {
local b; b="${hook_com[branch_orig]}@{upstream}"
local ahead behind
ahead=$( ${vcs_comm[cmd]} rev-list $b..HEAD 2>/dev/null | wc -l)
behind=$(${vcs_comm[cmd]} rev-list HEAD..$b 2>/dev/null | wc -l)
local -a gitstatus
(( $ahead )) && gitstatus+=( $'\u25b4'$ahead ) # ▴
(( $behind )) && gitstatus+=( $'\u25be'$behind ) # ▾
(( $#gitstatus )) && hook_com[misc]+="${(j:/:)gitstatus}"
}
# Styling: setting defaults, getting values {{{1
# + Print the defaults {{{1
# Uses zstyle format so it is easy to copy and modify
prompt_newt_defaults () {
local -a zstyles
local -i max1 max2
local -a context style value
local k
for k in "${(@k)__newt_defaults}"; do
local tmp=${__newt_defaults[$k]}
[[ ${(q)tmp} = *\\* ]] && value+=${(qq)tmp} || value+=${(q)tmp}
k=(':prompt-theme:newt:*' "${(z)k}")
tmp="${(j.:.)k[1,-2]}"
[[ ${(q)tmp} = *\\* ]] && context+=${(qq)tmp} || context+=${(q)tmp}
tmp=${(q)k[-1]}
[[ ${(q)tmp} = *\\* ]] && style+=${(qq)tmp} || style+=${(q)tmp}
(( max1 < ${#context[-1]} )) && max1=${#context[-1]}
(( max2 < ${#style[-1]} )) && max2=${#style[-1]}
done
local i=0
while (( i < $#context )); do
i=$((i+1))
zstyles+=$(printf 'zstyle %-*s %-*s %s' \
$max1 "$context[$i]" $max2 "$style[$i]" "$value[$i]")
done
LANG=C print -o -lr $zstyles
}
# + Set a style's default {{{1
__newt_default () {
local -A opts
zparseopts -A opts -D - d
(( $+opts[-d] )) \
&& unset "__newt_defaults[$*]" \
|| __newt_defaults+=(["${*[1,-2]}"]="${*[-1]}")
}
# + Look up a style {{{1
__newt_zstyle () {
local -A opts
zparseopts -A opts -D - d: m x
local style="${@[-1]}"
local ctx=($__newt[ctx] "${@[1,-2]}")
local val; unset val
local merge
# See if a setting is defined
zstyle -t ${(j.:.)ctx} "$style"
if [[ $? -ne 2 ]]; then
zstyle -s ${(j.:.)ctx} "$style" val
if (( $+opts[-m] )); then
merge=( ${=val} )
relative=${#${(M)merge:#[+-]*}}
if (( !relative )); then
# Nothing to merge, just use val as is
merge=()
elif (( relative == $#merge )); then
# All parts are relative, put defaults at the front
merge[1,0]='*'
fi
local star=${#${(M)merge:#\*}}
(( star )) && unset val
fi
fi
if (( ! $+val )); then
# If -x option, then do a simplified wildcard search through
# the defaults. Say context is a b c, then this will check for
# "a b c", "a b *", "a * *", "* * *", and use the first match.
# If not -x option, then only look for a full "a b c" match.
ctx=( $ctx[2,-1] $style )
local i
(( $+opts[-x] )) && i=$(($#ctx - 1)) || i=0
while true; do
if (( ${+__newt_defaults[$ctx]} )); then
val=${__newt_defaults[$ctx]}
break
fi
(( i < 1 )) && break
ctx[$i]='*'
i=$((i - 1))
done
(( $+val )) || val=$opts[-d]
fi
if (( $#merge )); then
local -a drop
local keep=()
local star=
local tmp
local i=0
while (( i < $#merge )); do
i=$((i+1))
case $merge[$i] in
-* )
tmp=${merge[$i]#-}
drop+=$tmp
;;
+* )
tmp=${merge[$i]#+}
# Keep the value
keep+=$tmp
# And also mark it to be removed from *
drop+=$tmp
;;
\* )
# Only recognize one *, any others get ignored
if (( ! star )); then
keep+=$merge[$i]
star=$#keep
fi
;;
* )
keep+=$merge[$i]
;;
esac
done
#__newt_debug "keep [" ${(qq)keep} "] * [" ${(qq)val} "] - drop [" ${(qq)drop} "]"
(( star )) && keep[$star]=( ${${=val}:|drop} )
val="$keep"
#__newt_debug " = $val"
fi
print -rn $val
}
# + Set a default for non-newt styles {{{1
# NOTE: This needs to be called during setup, not from later hooks.
__newt_other_default () {
# Only set the style if it doesn't already exist
zstyle -t "$1" "$2"
if [[ $? -eq 2 ]]; then
zstyle "$@"
fi
# Register that this style is of interest
__newt[other_defaults]+=" ${(qqq)1} ${(qqq)2}"
}
# + Save styles for future invocations {{{1
__newt_preserve_zstyles () {
local -a preserve
local ctx style val
zstyle -g ctx
for ctx in ${(M)ctx:#:prompt-theme:newt} \
${(M)ctx:#:prompt-theme:newt:*}
do
zstyle -g style "$ctx"
for style in $style; do
[[ $ctx = :prompt-theme:newt && $style = initialized ]] && continue
zstyle -a "$ctx" "$style" val
val=${(j. .)${(qqq)val}}
preserve+="${(qqq)ctx} ${(qqq)style} $val"
done
done
for ctx style in ${(z)__newt[other_defaults]}; do
zstyle -a ${(Q)ctx} ${(Q)style} val
val=${(j. .)${(qqq)val}}
preserve+="$ctx $style $val"
done
export _PROMPT_NEWT_ZSTYLES=${(@qq)${preserve}}
}
# + Restore inherited styles {{{1
__newt_restore_zstyles () {
local style
if ! zstyle -t :prompt-theme:newt initialized; then
for style in ${(Q)${(z)_PROMPT_NEWT_ZSTYLES}}; do
style=(${(Q)${(z)style}})
zstyle $style
done
fi
zstyle :prompt-theme:newt initialized true
}
# Colors handling {{{1
# + Resolve color to prompt format sequence {{{1
__newt_bg_color () {
local c
case $1 in
none)
c=
;;
'' | _ )
c="%k"
;;
fg:*)
c=${1#*:}
[[ -z $c ]] && c=$(__newt_terminal_fg)
c="%K{$c}"
;;
*)
c="%K{$1}"
;;
esac
print -rn $c
}
__newt_fg_color () {
local c
case $1 in
none)
c=
;;
'' | _ )
c="%f"
;;
bg:*)
c=${1#*:}
[[ -z $c ]] && c=$(__newt_terminal_bg)
c="%F{$c}"
;;
*)
c="%F{$1}"
;;
esac
print -rn $c
}
# + Get bg/fg color of terminal {{{1
# - TODO Use escape sequence to query terminal for color, see
# http://thrysoee.dk/xtermcontrol/
# https://github.com/JessThrysoee/xtermcontrol
# https://superuser.com/questions/157563/programmatic-access-to-current-xterm-background-color
# Maybe it can be implemented with the zsh/zpty module?
__newt_terminal_bg () {
local color
zstyle -s :prompt-theme:newt terminal-background color
: ${color:=${COLORFGBG#*;}}
: ${color:=black}
print -n $color
}
__newt_terminal_fg () {
local color
zstyle -s :prompt-theme:newt terminal-foreground color
: ${color:=${COLORFGBG%%;*}}
: ${color:=white}
print -n $color
}
# + Convert truecolor formats into escape codes {{{1
__newt_truecolor_escape () {
__newt_truecolor_escape_format () {
local n
[[ $1 = F ]] && n=38 || n=48
shift;
printf '%%{\x1b[%d;2;%d;%d;%dm%%}' $n "$@"
}
print -nr "${1//(#bm)%(K|F)\{([0-9]#)\;([0-9]#)\;([0-9]#)\}/$(
__newt_truecolor_escape_format $match[@])}"
}
# Defining separators {{{1
prompt_newt_add_separator () {
local -A opts
zparseopts -A opts -D - -wide: -wide-ltr: -wide-rtl:
local name="$1"
shift
# Left-to-right thick
__newt[separator-${name}-0]="$1"
# Left-to-right thin
__newt[separator-${name}-1]="${2-$1}"
# Right-to-left thick
__newt[separator-${name}-2]="${3-$1}"
# Right-to-left thin
__newt[separator-${name}-3]="${4-$3}"
(( $+opts[--wide] )) \
&& __newt[(e)separator-${name}-wide]=$opts[--wide]
(( $+opts[--wide-ltr] )) \
&& __newt[(e)separator-${name}-wide-ltr]=$opts[--wide-ltr]
(( $+opts[--wide-rtl] )) \
&& __newt[(e)separator-${name}-wide-rtl]=$opts[--wide-rtl]
# Set the default separator style to the first one defined
(( $+__newt[default-separator] )) || __newt[default-separator]=$name
}
# Update prompt strings {{{1
__newt_update_prompt () {
local side="$1"
local hook="$2"
__newt_do_segments $side $hook || return 1
__newt_assemble_segments $side
}
__newt_do_segments () {
local side="$1"
local hook="$2"
local changed=0
local func segment
for segment in "${=__newt[$side]}"; do
func="__newt+$segment+$hook"
(( ${+functions[$func]} )) || continue
$func $hook && changed=1
done
(( $changed )) || return 1
return 0
}
# + Assemble pre-calculated segments into a prompt string {{{1
__newt_assemble_segments () {
setopt local_options extended_glob
local side="$1"
local direction
[[ $side = left ]] && direction=0 || direction=1
# For future use, this is how many lines up from the input this prompt
# should be drawn. stack == 0 is the input line.
local stack=0
# ++ Fill in arrays holding values for active segments {{{1
local -a segment content bg fg sep sepdir
local padding
zstyle -t $__newt[ctx] compact && padding= || padding=' '
local seg state tmp
for seg in "${=__newt[$side]}"; do
[[ -n ${__newt[+${seg}+]} || $__newt[+${seg}+show_empty] = 1 ]] \
|| continue
segment+=$seg
state=${__newt[+${seg}+state]:-default}
bg+=$(__newt_zstyle -x "$seg" "$state" bg)
fg+=$(__newt_zstyle -x "$seg" "$state" fg)
sep+=$(__newt_zstyle -x -d $__newt[default-separator] \
"$seg" "$state" separator)
tmp=$(__newt_zstyle -x "$seg" "$state" direction)
[[ $tmp = reverse ]] && sepdir+=1 || sepdir+=0
# Pre-process the content a bit
tmp="${__newt[+${seg}+]}"
# Replace %k and %f with segment bg and fg colors
# NB: This doesn't use zformat because that will gobble up other formats
# in the content, like %(X,...), which the user specified. This simple
# substitution will do the wrong thing with something like '%%killed',
# but that should be rare and *could* be worked around.
local tmp2=$(__newt_bg_color "$bg[-1]")
tmp=${tmp:gs/%k/${tmp2}}
local tmp2=$(__newt_fg_color "$fg[-1]")
tmp=${tmp:gs/%f/${tmp2}}
# Trim whitespace
tmp=${padding}${${tmp##[[:space:]]##}%%[[:space:]]##}${padding}
content+=$tmp
done
# ++ Handle beginning & end of prompt for left/right side {{{1
if [[ $direction = 0 ]]; then
# This is a left-to-right prompt
if (( !stack )); then
# Add on a segment to prepare for user input
segment+=ready-for-input
content+=' '
bg+=
fg+=
sep+=''
sepdir+=0
fi
else
# This is a right-to-left prompt, so the separator precedes the
# segment.
# First, insert an empty segment at the front to effectively
# shift all the prompts left
segment[1,0]=beginning-of-line
content[1,0]=''
bg[1,0]=
fg[1,0]=
# Then add a null separator at the end, to finish the last segment
sep+=''
sepdir+=0
# Remove a trailing space to account for $ZLE_RPROMPT_INDENT, and
# extend the current background color to the end of the line
content[-1]="${content[-1]/% }%E"
# Add a segment to reset colors at end of line
segment+=end-of-line
content+=$'%{\e[0m%}'
bg+=
fg+=
sep+=''
sepdir+=0
fi
# ++ Gather the arrays into a prompt string {{{1
local result=
local i=0
local b0=%k f0=%f
local b1 f1
local wide
while ((i < $#content)); do
i=$((i+1))
#__newt_debug "$i:$segment[i] - (${(q)content[i]}) b(${(q)bg[i]}) f(${(q)fg[$i]}) s(${(q)sep[$i]})"
# +++ Colors for the segment body {{{1
b1=$(__newt_bg_color "$bg[$i]")
[[ $b0 = $b1 ]] || result+=$b1
b0=$b1
f1=$(__newt_fg_color "$fg[$i]")
[[ $f0 = $f1 ]] || result+=$f1
f0=$f1
# +++ The segment content proper {{{1
result+=$content[$i]
# +++ The separator {{{1
if [[ -n ${sep[$i]} ]]; then
if (( $i >= $#content )); then
print "IMPOSSIBLE, index $i has a separator (${(qq)sep[$i]}) off the end ($#content)" >&2
bg[$i+1]=196
fg[$i+1]=220
fi
# The sepdir[i] is 0 for "normal" direction (points right on a
# left-hand prompt, and points left on a right-hand prompt). It
# is 1 for a reversed separator. So XOR of prompt direction and
# sepdir[i] gives the direction of the separator itself.
local sep_direction=$((direction ^ ${sepdir[$i]}))
if [[ $bg[$i] = $bg[$i+1] ]]; then
local thin=1
b1=$(__newt_bg_color "$bg[$i]")
# When direction=1 (right prompt), the separator precedes its
# segment, so look there for the color
f1=$(__newt_fg_color "$fg[$i+$direction]")
else
thin=0
# Solid separator uses the background color of the dominant
# segment as its foreground. When sep_direction=1 ("points
# to the left"), the dominant segment is to the right,
# otherwise it is this segment. Background is from the
# opposite.
f1=$(__newt_fg_color "bg:$bg[$i+$sep_direction]")
b1=$(__newt_bg_color "$bg[$i+$((sep_direction ^ 1))]")
fi
local sep_style=$sep[$i]
(( $+__newt[(e)separator-${sep_style}-0] )) \
|| sep_style=$__newt[default-separator]
local index=$(( 2 * sep_direction + thin ))
#__newt_debug "+ sep $index style($sep_style) direction($sep_direction) thin($thin)"
local separator=$__newt[(e)separator-${sep_style}-${index}]
# Determine special spacing if this separator is extra wide
local left=0 right=0
if [[ $(__newt_zstyle -d 1 wide-separators) -ne 0 ]]; then
(( sep_direction )) \
&& wide=$__newt[(e)separator-${sep_style}-wide-rtl] \
|| wide=$__newt[(e)separator-${sep_style}-wide-ltr]
[[ -z $wide ]] && wide=$__newt[(e)separator-${sep_style}-wide]
if [[ -n $wide ]]; then
left=${wide%\:*}
right=${wide#*\:}
fi
#__newt_debug "$segment[$i] wide($wide) left($left) right($right)"
fi
# Trim preceding segment if separator is wide on the left
(( left )) && result=${result% }
[[ $b0 == $b1 ]] || result+=$b1
b0=$b1
[[ $f0 == $f1 ]] || result+=$f1
f0=$f1
result+=$separator
# Pad separator if it is wide on the right
(( right )) && result+=' '
fi
done
# ++ Store the prompt string {{{1
# Change %F{RRR;GGG;BBB} to TrueColor escapes
result=$(__newt_truecolor_escape "$result")
[[ $side = left ]] && PS1=$result || RPS1=$result
}
# Hook function manipulations {{{1
__newt_list_zsh_hooks () {
print \
chpwd precmd preexec periodic \
zshaddhistory zshexit zsh_directory_name
}
__newt_list_zle_hooks () {
print \
isearch-exit isearch-update line-pre-redraw \
line-init line-finish history-line-set keymap-select
}
__newt_add_hooks () {
local add_func="$1"; shift
local tag="$1"; shift
[[ -n $tag ]] && tag="${tag}-"
local hook func segment
local -a funcs
for hook in "$@"; do
funcs=()
for segment in ${=__newt[left]} ${=__newt[right]}; do
func="__newt+$segment+$tag$hook"
(( ${+functions[$func]} )) || continue
funcs+=$func
done
(( $#funcs )) || continue
__newt[hooks+$tag$hook]="$funcs"
func=__newt_hook_$tag$hook
eval "$func () { __newt_hook $tag$hook \$@ }"
$add_func $hook $func
done
}
__newt_hook () {
local hook="$1"
local did_something=0
__newt_update_prompt left $hook && did_something=1
__newt_update_prompt right $hook && did_something=1
{ (( $did_something )) && zle } || return
zle reset-prompt
}
__newt_delete_hooks () {
local delete_func="$1"; shift
# Both use -D to delete based on a pattern
for hook in "$@"; do
${delete_func} -D $hook 'prompt_newt_*'
${delete_func} -D $hook '__newt_*'
done
}
# Cleanup {{{1
prompt_newt_cleanup () {
__newt_delete_hooks add-zsh-hook \
${=$(__newt_list_zsh_hooks)}
__newt_delete_hooks add-zle-hook-widget \
${=$(__newt_list_zle_hooks)}
local func
for func in ${(kM)functions:#prompt_newt[[:punct:]]*}; do
unset "functions[$func]"
done
for func in ${(kM)functions:#__newt[[:punct:]]*}; do
unset "functions[$func]"
done
for func in ${(kM)functions:#+vi-newt-*}; do
unset "functions[$func]"
done
local ctx hooks
zstyle -g ctx
for ctx in ${(M)ctx:#:vcs_info:*}; do
zstyle -g hooks "$ctx" hooks
if (( ${#hooks:#newt[[:punct:]]*} )); then
zstyle "$ctx" hooks ${hooks:#newt[[:punct:]]*}
else
zstyle -d "$ctx" hooks
fi
done
unset __newt
unset __newt_defaults
unset __newt_look
unset PROMPT_NEWT_LOOK
autoload -Uz prompt_newt_setup
}
# Preview {{{1
prompt_newt_preview () {
local _zsh_theme_preview_euid