-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjwalter.lib
872 lines (759 loc) · 27.1 KB
/
jwalter.lib
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
# jwalter's Theme - https://github.com/itsjohannawren/zsh-jwalter
# Based heavily on agnoster's Theme - https://gist.github.com/3712874
if [ -z "${JWALTER_NET_FS}" ]; then
JWALTER_NET_FS="nfs afs smb smbfs cifs"
fi
if [ -z "${JWALTER_SHELL_DAEMONS}" ]; then
JWALTER_SHELL_DAEMONS="sshd:SSH in.sshd:SSH mosh-server:Mosh telnetd:Telnet in.telnetd:Telnet agetty:Local getty:Local Terminal:Local iTerm:Local iTerm2:Local xterm:Local Konsole:Local"
fi
if ! grep -Eq '^[1-9][0-9]*$' <<< "${JWALTER_HOSTNAME_PARTS}"; then
JWALTER_HOSTNAME_PARTS="1"
fi
if [ -z "${JWALTER_HOSTNAME_DIVISORS}" ] || ! grep -Eiq '^[a-z0-9_, -]+$' <<<"${JWALTER_HOSTNAME_DIVISORS}"; then
JWALTER_HOSTNAME_DIVISORS=""
fi
if ! grep -Eq '^(full|aliased)$' <<<"${JWALTER_PATH_STYLE}"; then
JWALTER_PATH_STYLE="full" # "full" or "aliased"
fi
if ! grep -Eq '^[0-9]+$' <<<"${JWALTER_PATH_TRUNCATE}"; then
JWALTER_PATH_TRUNCATE="0"
fi
if ! grep -Eq '^(icon|emote)$' <<<"${JWALTER_EXIT_STYLE}"; then
JWALTER_EXIT_STYLE="emote" # "icon" or "emote"
fi
if ! grep -Eq '^(icon|count|countalways)$' <<<"${JWALTER_JOB_STYLE}"; then
JWALTER_JOB_STYLE="count" # "icon", "count", or "countalways"
fi
if ! grep -Eq '^(yes|no)$' <<<"${JWALTER_MOUNT_RW}"; then
JWALTER_MOUNT_RW="no"
fi
if ! grep -Eq '^(yes|no)$' <<<"${JWALTER_MOUNTPOINT_RW}"; then
JWALTER_MOUNTPOINT_RW="no"
fi
if ! grep -Eq '^\.' <<<"${JWALTER_MOUNTPOINT_RW_FILE}"; then
JWALTER_MOUNTPOINT_RW_FILE=".jwalter-writeability-test"
fi
if ! grep -Eq '^(icon|text)$' <<<"${JWALTER_MOUNTPOINT_RW_STYLE}"; then
JWALTER_MOUNTPOINT_RW_STYLE="text" # "icon" or "text"
fi
if [ -z "${JWALTER_PROMPT_SECTIONS}" ]; then
JWALTER_PROMPT_SECTIONS="rootstatus userhost jobstatus exitstatus nvm rvm exectime path git svn"
fi
if ! grep -Eq '^(yes|no)$' <<<"${JWALTER_GIT_NET}"; then
JWALTER_GIT_NET="yes"
fi
if ! grep -Eq '^(yes|no)$' <<<"${JWALTER_GIT_NET_FULL}"; then
JWALTER_GIT_NET_FULL="no"
fi
if ! grep -Eq '^.+$' <<<"${JWALTER_GIT_NET_FULL_OVERRIDE}"; then
JWALTER_GIT_NET_FULL_OVERRIDE="jwalter_net_full"
fi
if ! grep -Eq '^(yes|no)$' <<<"${JWALTER_SVN_NET}"; then
JWALTER_SVN_NET="yes"
fi
if ! grep -Eq '^(yes|no)$' <<<"${JWALTER_SVN_NET_FULL}"; then
JWALTER_SVN_NET_FULL="no"
fi
if ! grep -Eq '^.+$' <<<"${JWALTER_SVN_NET_FULL_OVERRIDE}"; then
JWALTER_SVN_NET_FULL_OVERRIDE="jwalter_net_full"
fi
if ! grep -Eq '^(yes|no)$' <<<"${JWALTER_CVS_NET}"; then
JWALTER_CVS_NET="yes"
fi
if ! grep -Eq '^(yes|no)$' <<<"${JWALTER_CVS_NET_FULL}"; then
JWALTER_CVS_NET_FULL="no"
fi
if ! grep -Eq '^.+$' <<<"${JWALTER_CVS_NET_FULL_OVERRIDE}"; then
JWALTER_CVS_NET_FULL_OVERRIDE="jwalter_net_full"
fi
if ! grep -Eq '^(yes|no)$' <<<"${JWALTER_NVM}"; then
JWALTER_NVM="yes"
fi
if ! grep -Eq '^(yes|no)$' <<<"${JWALTER_RVM}"; then
JWALTER_RVM="yes"
fi
# =============================================================================
__JWALTER_SEG_BG=""
__JWALTER_SEG_SEPARATOR=""
__JWALTER_EXEC_START=""
__JWALTER_EXEC_TIME=""
__JWALTER_EXIT_STATUS=""
__JWALTER_EXIT_COMMAND=""
# =============================================================================
CVSROOT=""
# =============================================================================
segment() {
local BACKGROUND FOREGROUND
if [ -n "${1}" ]; then
BACKGROUND="%K{${1}}"
else
BACKGROUND="%k"
fi
if [ -n "${2}" ]; then
FOREGROUND="%F{${2}}"
else
FOREGROUND="%f"
fi
if [ -n "${__JWALTER_SEG_BG}" ] && [ "${1}" != "${__JWALTER_SEG_BG}" ]; then
echo -n " %{${BACKGROUND}%F{${__JWALTER_SEG_BG}}%}${__JWALTER_SEG_SEPARATOR}%{${FOREGROUND}%} "
elif [ -z "${__JWALTER_SEG_BG}" ] && ([ "${BACKGROUND}" = "%K{default}" ] || [ "${BACKGROUND}" = "%K{black}" ]); then
echo -n "%{${BACKGROUND}%}%{${FOREGROUND}%}"
else
echo -n "%{${BACKGROUND}%}%{${FOREGROUND}%} "
fi
if [ -n "${3}" ]; then
echo -n "${3}"
fi
__JWALTER_SEG_BG="${1}"
}
# End the prompt, closing any open segments
segments_end() {
if [ -n "${__JWALTER_SEG_BG}" ]; then
echo -n " %{%k%F{${__JWALTER_SEG_BG}}%}${__JWALTER_SEG_SEPARATOR}"
else
echo -n "%{%k%}"
fi
echo -n "%{%f%}"
__JWALTER_SEG_BG=""
}
# =============================================================================
is_network_path() {
local TEST_PATH SYS_MOUNT LONGEST_FS LONGEST_MOUNT
TEST_PATH="${1}"
for SYS_MOUNT in $(mount 2>/dev/null | awk '$2=="on" {mnt=$3} $3=="on" {mnt=$4} $4=="type" {fs=$5} $5=="type" {fs=$6} $4 ~ /^\(/ {sub(/\(/, "", $4); sub(/,/, "", $4); fs=$4} $5 ~ /^\(/ {sub(/\(/, "", $5); sub(/,/, "", $5); fs=$5} (mnt!="" && fs!="") {printf("%s:%s\n", fs, mnt)}'); do
eval set -- "${SYS_MOUNT/:/ }"
if [ "${1}" != "autofs" ]; then
if grep -qE "^${2}" <<<"${TEST_PATH}"; then
if [ "${#2}" -gt "${#LONGEST_MOUNT}" ]; then
LONGEST_FS="${1}"
LONGEST_MOUNT="${2}"
fi
fi
fi
done
if grep -o "\\b${LONGEST_FS}\\b" <<<"${JWALTER_NET_FS}"; then
return 0
else
return 1
fi
}
is_mount_rw() {
local TEST_MOUNT EXIT_STATUS
TEST_MOUNT="${1}"
EXIT_STATUS="$(awk "
BEGIN {
found = 0;
}
{
mount = \$1 \"==\" \$2;
}
mount == \"${TEST_MOUNT}\" && \$4 ~ /^(.+,|)rw(,.+|)\$/ {
printf (\"0\");
found = 1;
exit 0;
}
mount == \"${TEST_MOUNT}\" && \$4 ~ /^(.+,|)ro(,.+|)\$/ {
printf (\"1\");
found = 1;
exit 1;
}
mount == \"${TEST_MOUNT}\" && \$4 !~ /^(.+,|)r[ow](,.+|)\$/ {
printf (\"2\");
found = 1;
exit 2;
}
END {
if (found == 0) {
printf (\"2\");
exit 2;
}
}
" /proc/mounts 2>/dev/null)"
echo "${EXIT_STATUS}"
return "${EXIT_STATUS}"
}
is_directory_writeable() {
local TEST_DIRECTORY
TEST_DIRECTORY="${1}"
if touch "${TEST_DIRECTORY}/${JWALTER_MOUNTPOINT_RW_FILE}" &>/dev/null; then
rm -f "${TEST_DIRECTORY}/${JWALTER_MOUNTPOINT_RW_FILE}" &>/dev/null
echo "0"
return 0
fi
echo "1"
return 1
}
# =============================================================================
prompt_exitstatus() {
if [ "${__JWALTER_EXIT_STATUS}" = "__NONE__" ]; then
if [ "${JWALTER_EXIT_STYLE}" = "icon" ]; then
segment black default "⛵"
elif [ "${JWALTER_EXIT_STYLE}" = "emote" ]; then
segment white black "-_-"
fi
elif [ "${__JWALTER_EXIT_STATUS}" != "0" ]; then
if [ "${JWALTER_EXIT_STYLE}" = "icon" ]; then
segment black default "⛔"
elif [ "${JWALTER_EXIT_STYLE}" = "emote" ]; then
segment red black "X_x"
fi
else
if [ "${JWALTER_EXIT_STYLE}" = "icon" ]; then
segment black default "✅"
elif [ "${JWALTER_EXIT_STYLE}" = "emote" ]; then
segment green black "^_^"
fi
fi
}
# =============================================================================
prompt_rootstatus() {
if [ "${UID}" -eq 0 ]; then
segment black yellow "⚡"
fi
}
# =============================================================================
prompt_jobstatus() {
local JOB_COUNT
JOB_COUNT="$(jobs -l | awk '/^\[[0-9]+\]/ {n++;} END{printf("%i",n);}')"
if [ "${JOB_COUNT}" != "0" ]; then
if [ "${JWALTER_JOB_STYLE}" = "count" ] || [ "${JWALTER_JOB_STYLE}" = "countalways" ]; then
if [ "${JOB_COUNT}" = "1" ]; then
segment yellow black "${JOB_COUNT} job"
else
segment yellow black "${JOB_COUNT} jobs"
fi
else
segment black default "✨"
fi
elif [ "${JWALTER_JOB_STYLE}" = "countalways" ]; then
segment yellow black "${JOB_COUNT} jobs"
fi
}
# =============================================================================
preexec() {
__JWALTER_EXEC_TIME=""
__JWALTER_EXEC_START="$(date +"%s")"
__JWALTER_EXIT_COMMAND="${1}"
}
precmd() {
if [ -n "${__JWALTER_EXEC_START}" ]; then
__JWALTER_EXEC_TIME="$(($(date +"%s") - __JWALTER_EXEC_START))"
__JWALTER_EXEC_START=""
else
__JWALTER_EXEC_TIME="0"
fi
if [ -n "${__JWALTER_EXIT_COMMAND}" ]; then
if grep -qE '^ssh ' <<<"${__JWALTER_EXIT_COMMAND}" && which badge &>/dev/null; then
badge --reset
fi
__JWALTER_EXIT_COMMAND=""
__JWALTER_EXIT_STATUS=""
else
__JWALTER_EXIT_STATUS="__NONE__"
fi
}
fuzzy_time() {
local SECONDS OUTPUT
SECONDS="${1}"
OUTPUT=""
if [ "${SECONDS}" -ge 3600 ]; then
OUTPUT="${OUTPUT}$((SECONDS / 3600))ᴴ"
SECONDS="$((SECONDS - ((SECONDS / 3600) * 3600)))"
fi
if [ "${SECONDS}" -ge 60 ]; then
OUTPUT="${OUTPUT}$((SECONDS / 60))ᴹ"
SECONDS="$((SECONDS - ((SECONDS / 60) * 60)))"
fi
if [ "${SECONDS}" -gt 0 ] || ([ "${SECONDS}" -eq 0 ] && [ -z "${OUTPUT}" ]); then
OUTPUT="${OUTPUT}${SECONDS}ᙚ"
fi
echo -n "${OUTPUT}"
}
prompt_exectime() {
segment cyan black "$(fuzzy_time "${__JWALTER_EXEC_TIME}")"
}
# =============================================================================
shell_daemon() {
local PARENT_ID PARENT_CMD IS_SCREEN
PARENT_ID="${1:-$$}"
while [ "${PARENT_ID}" != "1" ]; do
PARENT_CMD="$(basename "$(ps -p "${PARENT_ID}" -o command= | sed -e 's/^-*//' -e 's/ .*//' -e 's/:$//')")"
if [ "${PARENT_CMD}" = "SCREEN" ]; then
IS_SCREEN="1"
elif grep -q "\\b${PARENT_CMD}:" <<<"${JWALTER_SHELL_DAEMONS}"; then
if [ -z "${IS_SCREEN}" ]; then
grep -o "\\b${PARENT_CMD}:\\S*" <<<"${JWALTER_SHELL_DAEMONS}"
else
grep -o "\\b${PARENT_CMD}:\\S*" <<<"${JWALTER_SHELL_DAEMONS}" | sed -e 's/$/+Screen/'
fi
return
fi
PARENT_ID="$(ps -p "${PARENT_ID}" -o ppid= | sed -e 's/ *//g')"
done
}
sys_hostname() {
local HOSTNAME_SYS MATCH
HOSTNAME_SYS="$(hostname -f 2>/dev/null || (echo "$(hostname 2>/dev/null) $(domainname 2>/dev/null) $(dnsdomainname 2>/dev/null)"))"
HOSTNAME_SYS="$(sed -e 's/(none)//' -e 's/ *$//g' -e 's/ */./g' <<<"${HOSTNAME_SYS}")"
if [ -n "${JWALTER_HOSTNAME_DIVISORS}" ]; then
MATCH="$(grep -Eo "\.($(sed -e 's/\([^a-zA-Z0-9_]\)/\\\1/g' -e 's/ */|/' <<<"${JWALTER_HOSTNAME_DIVISORS}"))\." <<<"${HOSTNAME_SYS}" | sed -e 's/^\./\\./g')"
if [ -n "${MATCH}" ]; then
echo -n "$(sed -e "s/${MATCH}.*$//" <<< "${HOSTNAME_SYS}")"
return
fi
fi
if [ -n "${JWALTER_HOSTNAME_PARTS}" ] && [ "${JWALTER_HOSTNAME_PARTS}" != "0" ]; then
if [ "$(sed -e 's/\./ /g' <<<"${HOSTNAME_SYS}" | wc -w | awk '{print $1}')" -gt "${JWALTER_HOSTNAME_PARTS}" ]; then
echo -n "$(cut -d . "-f1-${JWALTER_HOSTNAME_PARTS}" <<<"${HOSTNAME_SYS}")"
return
fi
fi
echo -n "${HOSTNAME_SYS}"
}
prompt_userhost() {
local USER HOST SHELL_DAEMON
USER="$(whoami)"
HOST="$(sys_hostname)"
if [ "${USER}" != "${DEFAULT_USER}" ] || [ -n "${SSH_CLIENT}" ]; then
SHELL_DAEMON="$(shell_daemon $$)"
if [ -n "${SHELL_DAEMON}" ]; then
segment black default "%(!.%{%F{yellow}%}.)${USER}@${HOST} (${SHELL_DAEMON/*:/})"
else
segment black default "%(!.%{%F{yellow}%}.)${USER}@${HOST}"
fi
fi
}
# =============================================================================
prompt_path() {
local BUFFER NET_FS MOUNT MOUNT_RW MOUNT_RW_EXTRA
BUFFER=""
NET_FS="$(is_network_path "$(pwd -P)")"
# shellcheck disable=2181
if [ "$?" = "0" ]; then
BUFFER="${BUFFER}, ${NET_FS}"
fi
#JWALTER_MOUNT_RW="no"
#JWALTER_MOUNTPOINT_RW="no"
#JWALTER_MOUNTPOINT_RW_FILE=".jwalter-writeability-test"
if [ "${JWALTER_MOUNTPOINT_RW_STYLE}" != "off" ]; then
MOUNT="$(df . 2>/dev/null | tail -n 1 | awk '{printf ("%s==%s", $1, $6);}')"
MOUNT_RW=""
if [ "${JWALTER_MOUNT_RW}" != "yes" ] && [ "${JWALTER_MOUNTPOINT_RW}" = "yes" ]; then
MOUNT_RW="$(is_directory_writeable "${MOUNT#*==}" 2>/dev/null)"
elif [ "${JWALTER_MOUNT_RW}" = "yes" ]; then
MOUNT_RW="$(is_mount_rw "${MOUNT}" 2>/dev/null)"
if [ "${MOUNT_RW}" != "1" ] && [ "${JWALTER_MOUNTPOINT_RW}" = "yes" ] && ! grep -qE '^(cgroup|configfs|debugfs|devpts|devtmpfs|hugetlbfs|mqueue|proc|pstore|securityfs|sunrpc|sysfs)$' <<<"${MOUNT%%==*}"; then
MOUNT_RW="$(is_directory_writeable "${MOUNT#*==}" 2>/dev/null)"
MOUNT_RW_EXTRA="+"
fi
fi
#BUFFER="${BUFFER}, ${MOUNT#*==}"
if [ "${MOUNT_RW}" = "0" ]; then
if [ "${JWALTER_MOUNTPOINT_RW_STYLE}" = "text" ]; then
BUFFER="${BUFFER}, rw${MOUNT_RW_EXTRA}"
else
BUFFER="${BUFFER}, ✎${MOUNT_RW_EXTRA}"
fi
elif [ "${MOUNT_RW}" = "1" ]; then
if [ "${JWALTER_MOUNTPOINT_RW_STYLE}" = "text" ]; then
BUFFER="${BUFFER}, ro${MOUNT_RW_EXTRA}"
else
BUFFER="${BUFFER}, 🚫${MOUNT_RW_EXTRA}"
fi
#else
# if [ "${JWALTER_MOUNTPOINT_RW_STYLE}" = "text" ]; then
# BUFFER="${BUFFER}, unk${MOUNT_RW_EXTRA}"
# else
# BUFFER="${BUFFER}, ❓${MOUNT_RW_EXTRA}"
# fi
fi
fi
BUFFER="$(sed -e 's/^, //' <<<"${BUFFER}")"
if [ -n "${BUFFER}" ]; then
BUFFER="(${BUFFER}) "
fi
if [ "${JWALTER_PATH_TRUNCATE}" = "0" ]; then
if [ "${JWALTER_PATH_STYLE}" = "aliased" ]; then
BUFFER="${BUFFER}%~"
elif [ "${JWALTER_PATH_STYLE}" = "full" ]; then
BUFFER="${BUFFER}%/"
fi
else
if [ "${JWALTER_PATH_STYLE}" = "aliased" ]; then
BUFFER="${BUFFER}%$((JWALTER_PATH_TRUNCATE+1))(c:...:)%${JWALTER_PATH_TRUNCATE}c"
elif [ "${JWALTER_PATH_STYLE}" = "full" ]; then
BUFFER="${BUFFER}%$((JWALTER_PATH_TRUNCATE+1))(.:...:)%${JWALTER_PATH_TRUNCATE}."
fi
fi
segment blue black "${BUFFER}"
}
# =============================================================================
prompt_git() {
local GIT_PATH GIT_UNTRACKED GIT_MODIFIED GIT_REMOVED GIT_STAGED GIT_AHEAD GIT_BEHIND GIT_BRANCH GIT_COMMIT GIT_TAG GIT_STATS
if [ "${JWALTER_GIT_NET}" != "yes" ]; then
if is_network_path "$(pwd -P)" &>/dev/null; then
return 0
fi
fi
if ! which git &>/dev/null; then
return 0
fi
if git rev-parse --is-inside-work-tree &>/dev/null; then
GIT_PATH="$(git rev-parse --git-dir 2>/dev/null)"
# Are we on a network mount?
if \
is_network_path "$(cd "${GIT_PATH}" &>/dev/null && pwd -P)" &>/dev/null && \
[ "${JWALTER_GIT_NET_FULL}" != "yes" ] && \
[ ! -e "${GIT_PATH}/${JWALTER_GIT_NET_FULL_OVERRIDE}" ] \
; then
segment cyan black
GIT_UNMERGED="0"
GIT_IGNORED="0"
GIT_UNTRACKED="0"
GIT_MODIFIED="0"
GIT_REMOVED="0"
GIT_STAGED="0"
GIT_AHEAD="0"
GIT_BEHIND="0"
GIT_TAG=""
else
eval "$(git status -s --ignored 2>&1 | awk '/^\?\?/ {untracked++; next;} /^!!/ {ignored++; next;} /^UU/ {unmerged++; next;} /^[^ ?]/ {staged++;} /^.M/ {modified++} /^ D/ {removed++;} END {printf ("GIT_UNMERGED=%u\nGIT_IGNORED=%u\nGIT_UNTRACKED=%u\nGIT_MODIFIED=%u\nGIT_REMOVED=%u\nGIT_STAGED=%u\n", unmerged, ignored, untracked, modified, removed, staged);}')"
eval "$(git status -b 2>&1 | awk '/branch is ahead/ {ahead=$(NF-1);} /branch is behind/ {behind=$7;} /different commits? each/ {ahead=$3; behind=$5;} /On branch/ {branch=$NF;} /HEAD detached at/ {branch=$NF "(DETACHED)"} END {printf ("GIT_AHEAD=%u\nGIT_BEHIND=%u\nGIT_BRANCH=\"%s\"\n", ahead, behind, branch);}')"
GIT_REV="$(git rev-parse HEAD 2>/dev/null | head -n 1)"
if [ "${GIT_REV}" != "HEAD" ]; then
GIT_TAG="$(git tag --contains "${GIT_REV}" 2>/dev/null)"
else
GIT_TAG=""
fi
if [ -n "${GIT_TAG}" ]; then
if [ "${GIT_TAG}" != "$(git describe --tags 2>/dev/null)" ]; then
GIT_TAG="ᕮ${GIT_TAG}"
else
GIT_TAG="ᑕ${GIT_TAG}"
fi
fi
GIT_TAG="$(awk '/^[^\t ]/ {buffer=buffer $1 ","} END{sub(/,$/,"",buffer);printf("%s",buffer);}' <<<"${GIT_TAG}")"
if [ -n "$(git status -s 2>/dev/null | tail -n 1)" ]; then
segment yellow black
else
segment green black
fi
fi
GIT_COMMIT="$(git rev-parse HEAD 2>/dev/null | grep -ioE '^[0-9a-f]{10}' | head -n 1)"
if [ -z "${GIT_COMMIT}" ]; then
GIT_COMMIT="initial"
fi
if [ -z "${GIT_BRANCH}" ]; then
GIT_BRANCH="$(git branch 2>/dev/null | awk '/^* / && $3=="" {printf $2} /^* \(no branch, rebasing/ {sub(/\)$/,"",$5); printf $5}')"
if [ -z "${GIT_BRANCH}" ]; then
GIT_BRANCH="NO BRANCH"
fi
fi
echo -n " ${GIT_BRANCH}→${GIT_COMMIT}"
if [ -n "${GIT_TAG}" ]; then
echo -n " ${GIT_TAG}"
fi
GIT_STATS=""
if [ "${GIT_UNTRACKED}" -gt 0 ]; then
GIT_STATS="${GIT_STATS}…${GIT_UNTRACKED}"
fi
if [ "${GIT_IGNORED}" -gt 0 ]; then
GIT_STATS="${GIT_STATS}i${GIT_IGNORED}"
fi
if [ "${GIT_UNMERGED}" -gt 0 ]; then
GIT_STATS="${GIT_STATS}u${GIT_UNMERGED}"
fi
if [ "${GIT_MODIFIED}" -gt 0 ]; then
GIT_STATS="${GIT_STATS}±${GIT_MODIFIED}"
fi
if [ "${GIT_REMOVED}" -gt 0 ]; then
GIT_STATS="${GIT_STATS}-${GIT_REMOVED}"
fi
if [ "${GIT_STAGED}" -gt 0 ]; then
GIT_STATS="${GIT_STATS}+${GIT_STAGED}"
fi
if [ "${GIT_AHEAD}" -gt 0 ]; then
GIT_STATS="${GIT_STATS}↑${GIT_AHEAD}"
fi
if [ "${GIT_BEHIND}" -gt 0 ]; then
GIT_STATS="${GIT_STATS}↓${GIT_BEHIND}"
fi
if [ -n "${GIT_STATS}" ]; then
echo -n " ${GIT_STATS}"
fi
if [ -e "${GIT_PATH}/BISECT_LOG" ]; then
echo -n " (Bisecting)"
elif [ -e "${GIT_PATH}/MERGE_HEAD" ]; then
echo -n " (Merging)"
elif [ -e "${GIT_PATH}/rebase" ] || [ -e "${GIT_PATH}/rebase-apply" ] || [ -e "${GIT_PATH}/rebase-merge" ] || [ -e "${GIT_PATH}/../.dotest" ]; then
echo -n " (Rebasing)"
elif [ "$(git rev-parse --is-bare-repository 2>/dev/null)" = "true" ]; then
echo -n " (Bare)"
fi
fi
}
# =============================================================================
prompt_svn() {
local SVN_STATUS_ADDED SVN_STATUS_CONFLICTED SVN_STATUS_DELETED SVN_STATUS_MODIFIED SVN_STATUS_REPLACED SVN_STATUS_UNVERSIONED SVN_STATUS_MISSING SVN_STATUS_OBSTRUCTED SVN_STATUS_LOCKED
# shellcheck disable=2034
local SVN_PATH SVN_REPO_URL SVN_REPO_ROOT SVN_REPO_UUID SVN_REV SVN_NODE SVN_LAST_REV SVN_LAST_DATE
if [ "${JWALTER_SVN_NET}" != "yes" ]; then
if is_network_path "$(pwd -P)" &>/dev/null; then
return 0
fi
fi
if ! which svn &>/dev/null; then
return 0
fi
eval "$(svn info 2>/dev/null | awk '/^Working Copy Root Path:/ {$1=$2=$3=$4=""; gsub(/^[\t ]+/,"",$0); printf("SVN_PATH=\"%s\"\n",$0);} /^URL:/ {$1=""; gsub(/^[\t ]+/,"",$0); printf("SVN_REPO_URL=\"%s\"\n",$0);} /^Repository Root:/ {$1=$2=""; gsub(/^[\t ]+/,"",$0); printf("SVN_REPO_ROOT=\"%s\"\n",$0);} /^Repository UUID:/ {$1=$2=""; gsub(/^[\t ]+/,"",$0); printf("SVN_REPO_UUID=\"%s\"\n",$0);} /^Revision:/ {$1=""; gsub(/^[\t ]+/,"",$0); printf("SVN_REV=\"%s\"\n",$0);} /^Node Kind:/ {$1=$2=""; gsub(/^[\t ]+/,"",$0); printf("SVN_NODE=\"%s\"\n",$0);} /^Last Changed Rev:/ {$1=$2=$3=""; gsub(/^[\t ]+/,"",$0); printf("SVN_LAST_REV=\"%s\"\n",$0);} /^Last Changed Date:/ {$1=$2=$3=""; gsub(/^[\t ]+/,"",$0); printf("SVN_LAST_DATE=\"%s\"\n",$0);}')"
if [ -z "${SVN_PATH}" ]; then
return 0
fi
if \
is_network_path "$(cd "${SVN_PATH}" &>/dev/null && pwd -P)" &>/dev/null && \
[ "${JWALTER_SVN_NET_FULL}" != "yes" ] && \
[ ! -e "${SVN_PATH}/${JWALTER_SVN_NET_FULL_OVERRIDE}" ] \
; then
eval "$(svn status --depth=immediates | awk '/^A/ {added++;} /^(C|.C|.{6}C)/ {conflicted++;} /^D/ {deleted++;} /^(M|.M)/ {modified++;} /^R/ {replaced++;} /^\?/ {unversioned++;} /^!/ {missing++;} /^~/ {obstructioned++;} /^..(L|...K)/ {locked++;} END {printf("SVN_STATUS_ADDED=%u\nSVN_STATUS_CONFLICTED=%u\nSVN_STATUS_DELETED=%u\nSVN_STATUS_MODIFIED=%u\nSVN_STATUS_REPLACED=%u\nSVN_STATUS_UNVERSIONED=%u\nSVN_STATUS_MISSING=%u\nSVN_STATUS_OBSTRUCTED=%u\nSVN_STATUS_LOCKED=%u\n",added,conflicted,deleted,modified,replaced,unversioned,missing,obstructioned,locked);}')"
else
pushd "${SVN_PATH}" &>/dev/null
eval "$(svn status | awk '/^A/ {added++;} /^(C|.C|.{6}C)/ {conflicted++;} /^D/ {deleted++;} /^(M|.M)/ {modified++;} /^R/ {replaced++;} /^\?/ {unversioned++;} /^!/ {missing++;} /^~/ {obstructioned++;} /^..(L|...K)/ {locked++;} END {printf("SVN_STATUS_ADDED=%u\nSVN_STATUS_CONFLICTED=%u\nSVN_STATUS_DELETED=%u\nSVN_STATUS_MODIFIED=%u\nSVN_STATUS_REPLACED=%u\nSVN_STATUS_UNVERSIONED=%u\nSVN_STATUS_MISSING=%u\nSVN_STATUS_OBSTRUCTED=%u\nSVN_STATUS_LOCKED=%u\n",added,conflicted,deleted,modified,replaced,unversioned,missing,obstructioned,locked);}')"
popd &>/dev/null
fi
if \
[ "${SVN_STATUS_CONFLICTED}" != "0" ] || \
[ "${SVN_STATUS_MISSING}" != "0" ] || \
[ "${SVN_STATUS_OBSTRUCTED}" != "0" ]
then
segment red black
elif \
[ "${SVN_STATUS_ADDED}" != "0" ] || \
[ "${SVN_STATUS_DELETED}" != "0" ] || \
[ "${SVN_STATUS_MODIFIED}" != "0" ] || \
[ "${SVN_STATUS_REPLACED}" != "0" ] || \
[ "${SVN_STATUS_UNVERSIONED}" != "0" ] || \
[ "${SVN_STATUS_LOCKED}" != "0" ]
then
segment yellow black
else
segment green black
fi
echo -n " (svn)→${SVN_REV}"
SVN_STATS=""
if [ "${SVN_STATUS_UNVERSIONED}" -gt 0 ]; then
SVN_STATS="${SVN_STATS}…${SVN_STATUS_UNVERSIONED}"
fi
if [ "${SVN_STATUS_ADDED}" -gt 0 ]; then
SVN_STATS="${SVN_STATS}+${SVN_STATUS_ADDED}"
fi
if [ "${SVN_STATUS_MODIFIED}" -gt 0 ]; then
SVN_STATS="${SVN_STATS}±${SVN_STATUS_MODIFIED}"
fi
if [ "${SVN_STATUS_DELETED}" -gt 0 ]; then
SVN_STATS="${SVN_STATS}-${SVN_STATUS_DELETED}"
fi
if [ "${SVN_STATUS_MISSING}" -gt 0 ]; then
SVN_STATS="${SVN_STATS}?${SVN_STATUS_MISSING}"
fi
if [ "${SVN_STATUS_CONFLICTED}" -gt 0 ] || [ "${SVN_STATUS_OBSTRUCTED}" -gt 0 ]; then
SVN_STATS="${SVN_STATS}!$((SVN_STATUS_CONFLICTED + SVN_STATUS_OBSTRUCTED))"
fi
if [ "${SVN_STATUS_LOCKED}" -gt 0 ]; then
SVN_STATS="${SVN_STATS}${SVN_STATUS_LOCKED}"
fi
if [ "${SVN_REV}" != "${SVN_LAST_REV}" ]; then
SVN_STATS="${SVN_STATS}↓$((SVN_LAST_REV - SVN_REV))"
fi
if [ -n "${SVN_STATS}" ]; then
echo -n " ${SVN_STATS}"
fi
if [ "${SVN_STATUS_OBSTRUCTED}" != "0" ]; then
echo -n " (Obstructed)"
elif [ "${SVN_STATUS_CONFLICTED}" != "0" ]; then
echo -n " (Conflicted)"
fi
}
# =============================================================================
prompt_cvs() {
local CVS_STATUS_ADDED CVS_STATUS_CONFLICTED CVS_STATUS_DELETED CVS_STATUS_MODIFIED CVS_STATUS_REPLACED CVS_STATUS_UNVERSIONED CVS_STATUS_MISSING CVS_STATUS_OBSTRUCTED CVS_STATUS_LOCKED
# shellcheck disable=2034
local CVS_PATH CVS_REPO_URL CVS_REPO_ROOT CVS_REPO_UUID CVS_REV CVS_NODE CVS_LAST_REV CVS_LAST_DATE
if [ "${JWALTER_CVS_NET}" != "yes" ]; then
if is_network_path "$(pwd -P)" &>/dev/null; then
return 0
fi
fi
if ! which svn &>/dev/null; then
return 0
fi
eval "$(svn info 2>/dev/null | awk '/^Working Copy Root Path:/ {$1=$2=$3=$4=""; gsub(/^[\t ]+/,"",$0); printf("CVS_PATH=\"%s\"\n",$0);} /^URL:/ {$1=""; gsub(/^[\t ]+/,"",$0); printf("CVS_REPO_URL=\"%s\"\n",$0);} /^Repository Root:/ {$1=$2=""; gsub(/^[\t ]+/,"",$0); printf("CVS_REPO_ROOT=\"%s\"\n",$0);} /^Repository UUID:/ {$1=$2=""; gsub(/^[\t ]+/,"",$0); printf("CVS_REPO_UUID=\"%s\"\n",$0);} /^Revision:/ {$1=""; gsub(/^[\t ]+/,"",$0); printf("CVS_REV=\"%s\"\n",$0);} /^Node Kind:/ {$1=$2=""; gsub(/^[\t ]+/,"",$0); printf("CVS_NODE=\"%s\"\n",$0);} /^Last Changed Rev:/ {$1=$2=$3=""; gsub(/^[\t ]+/,"",$0); printf("CVS_LAST_REV=\"%s\"\n",$0);} /^Last Changed Date:/ {$1=$2=$3=""; gsub(/^[\t ]+/,"",$0); printf("CVS_LAST_DATE=\"%s\"\n",$0);}')"
if [ -z "${CVS_PATH}" ]; then
return 0
fi
if \
is_network_path "$(cd "${CVS_PATH}" &>/dev/null && pwd -P)" &>/dev/null && \
[ "${JWALTER_CVS_NET_FULL}" != "yes" ] && \
[ ! -e "${CVS_PATH}/${JWALTER_CVS_NET_FULL_OVERRIDE}" ] \
; then
eval "$(svn status --depth=immediates | awk '/^A/ {added++;} /^(C|.C|.{6}C)/ {conflicted++;} /^D/ {deleted++;} /^(M|.M)/ {modified++;} /^R/ {replaced++;} /^\?/ {unversioned++;} /^!/ {missing++;} /^~/ {obstructioned++;} /^..(L|...K)/ {locked++;} END {printf("CVS_STATUS_ADDED=%u\nCVS_STATUS_CONFLICTED=%u\nCVS_STATUS_DELETED=%u\nCVS_STATUS_MODIFIED=%u\nCVS_STATUS_REPLACED=%u\nCVS_STATUS_UNVERSIONED=%u\nCVS_STATUS_MISSING=%u\nCVS_STATUS_OBSTRUCTED=%u\nCVS_STATUS_LOCKED=%u\n",added,conflicted,deleted,modified,replaced,unversioned,missing,obstructioned,locked);}')"
else
pushd "${CVS_PATH}" &>/dev/null
eval "$(svn status | awk '/^A/ {added++;} /^(C|.C|.{6}C)/ {conflicted++;} /^D/ {deleted++;} /^(M|.M)/ {modified++;} /^R/ {replaced++;} /^\?/ {unversioned++;} /^!/ {missing++;} /^~/ {obstructioned++;} /^..(L|...K)/ {locked++;} END {printf("CVS_STATUS_ADDED=%u\nCVS_STATUS_CONFLICTED=%u\nCVS_STATUS_DELETED=%u\nCVS_STATUS_MODIFIED=%u\nCVS_STATUS_REPLACED=%u\nCVS_STATUS_UNVERSIONED=%u\nCVS_STATUS_MISSING=%u\nCVS_STATUS_OBSTRUCTED=%u\nCVS_STATUS_LOCKED=%u\n",added,conflicted,deleted,modified,replaced,unversioned,missing,obstructioned,locked);}')"
popd &>/dev/null
fi
if \
[ "${CVS_STATUS_CONFLICTED}" != "0" ] || \
[ "${CVS_STATUS_MISSING}" != "0" ] || \
[ "${CVS_STATUS_OBSTRUCTED}" != "0" ]
then
segment red black
elif \
[ "${CVS_STATUS_ADDED}" != "0" ] || \
[ "${CVS_STATUS_DELETED}" != "0" ] || \
[ "${CVS_STATUS_MODIFIED}" != "0" ] || \
[ "${CVS_STATUS_REPLACED}" != "0" ] || \
[ "${CVS_STATUS_UNVERSIONED}" != "0" ] || \
[ "${CVS_STATUS_LOCKED}" != "0" ]
then
segment yellow black
else
segment green black
fi
echo -n " (svn)→${CVS_REV}"
CVS_STATS=""
if [ "${CVS_STATUS_UNVERSIONED}" -gt 0 ]; then
CVS_STATS="${CVS_STATS}…${CVS_STATUS_UNVERSIONED}"
fi
if [ "${CVS_STATUS_ADDED}" -gt 0 ]; then
CVS_STATS="${CVS_STATS}+${CVS_STATUS_ADDED}"
fi
if [ "${CVS_STATUS_MODIFIED}" -gt 0 ]; then
CVS_STATS="${CVS_STATS}±${CVS_STATUS_MODIFIED}"
fi
if [ "${CVS_STATUS_DELETED}" -gt 0 ]; then
CVS_STATS="${CVS_STATS}-${CVS_STATUS_DELETED}"
fi
if [ "${CVS_STATUS_MISSING}" -gt 0 ]; then
CVS_STATS="${CVS_STATS}?${CVS_STATUS_MISSING}"
fi
if [ "${CVS_STATUS_CONFLICTED}" -gt 0 ] || [ "${CVS_STATUS_OBSTRUCTED}" -gt 0 ]; then
CVS_STATS="${CVS_STATS}!$((CVS_STATUS_CONFLICTED + CVS_STATUS_OBSTRUCTED))"
fi
if [ "${CVS_STATUS_LOCKED}" -gt 0 ]; then
CVS_STATS="${CVS_STATS}${CVS_STATUS_LOCKED}"
fi
if [ "${CVS_REV}" != "${CVS_LAST_REV}" ]; then
CVS_STATS="${CVS_STATS}↓$((CVS_LAST_REV - CVS_REV))"
fi
if [ -n "${CVS_STATS}" ]; then
echo -n " ${CVS_STATS}"
fi
if [ "${CVS_STATUS_OBSTRUCTED}" != "0" ]; then
echo -n " (Obstructed)"
elif [ "${CVS_STATUS_CONFLICTED}" != "0" ]; then
echo -n " (Conflicted)"
fi
}
# =============================================================================
prompt_nvm() {
local NODE_VERSION NODE_NAME
if ! nvm help &>/dev/null; then
return 0
fi
NODE_VERSION="$(nvm version)"
case "${NODE_VERSION}" in
system)
return 0
;;
none)
return 0
;;
v*)
NODE_NAME="Node "
NODE_VERSION="${NODE_VERSION/v/}"
;;
iojs-v*)
NODE_NAME="ioJS "
NODE_VERSION="${NODE_VERSION/iojs-v/}"
;;
esac
segment magenta black
echo -n "${NODE_NAME}${NODE_VERSION}"
}
# =============================================================================
prompt_rvm() {
local RUBY_VERSION RUBY_NAME
if ! rvm help &>/dev/null; then
return 0
fi
RUBY_VERSION="$(rvm current 2>/dev/null)"
case "${RUBY_VERSION}" in
system)
return 0
;;
ruby-*)
RUBY_NAME="Ruby "
RUBY_VERSION="${RUBY_VERSION/ruby-/}"
;;
esac
segment magenta white
echo -n "${RUBY_NAME}${RUBY_VERSION}"
}
# =============================================================================
prompt_break() {
segments_end
echo
}
# =============================================================================
build_prompt() {
__JWALTER_EXIT_STATUS_TEMP="$?"
if [ -z "${__JWALTER_EXIT_STATUS}" ]; then
__JWALTER_EXIT_STATUS="${__JWALTER_EXIT_STATUS_TEMP}"
fi
local SECTION BUFFER
# shellcheck disable=2013
for SECTION in $(sed -e 's/ +/\n/' <<<"${JWALTER_PROMPT_SECTIONS}"); do
case "${SECTION}" in
break)
BUFFER="${BUFFER}prompt_break;"
;;
userhost)
BUFFER="${BUFFER}prompt_userhost;"
;;
exitstatus)
BUFFER="${BUFFER}prompt_exitstatus;"
;;
rootstatus)
BUFFER="${BUFFER}prompt_rootstatus;"
;;
jobstatus)
BUFFER="${BUFFER}prompt_jobstatus;"
;;
exectime)
BUFFER="${BUFFER}prompt_exectime;"
;;
path)
BUFFER="${BUFFER}prompt_path;"
;;
git)
BUFFER="${BUFFER}prompt_git;"
;;
svn)
BUFFER="${BUFFER}prompt_svn;"
;;
cvs)
BUFFER="${BUFFER}prompt_cvs;"
;;
nvm)
BUFFER="${BUFFER}prompt_nvm;"
;;
rvm)
BUFFER="${BUFFER}prompt_rvm;"
;;
esac
done
# shellcheck disable=2086
echo -n "$(eval ${BUFFER}segments_end)"
}
# shellcheck disable=2034,2016
PROMPT='$(build_prompt) '