forked from lvv/git-prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-prompt.sh
executable file
·1420 lines (1167 loc) · 52.4 KB
/
git-prompt.sh
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
# don't set prompt if this is not interactive shell
[[ $- != *i* ]] && return
################################################################### CONFIG
##### read config file if any.
unset make_color_ok make_color_dirty jobs_color_bkg jobs_color_stop slash_color slash_color_readonly at_color at_color_remote
unset command_time_color clock_color
unset dir_color rc_color user_id_color root_id_color init_vcs_color clean_vcs_color
unset modified_vcs_color added_vcs_color untracked_vcs_color deleted_vcs_color op_vcs_color detached_vcs_color hex_vcs_color conflicted_vcs_color
unset rawhex_len
conf=git-prompt.conf; [[ -r $conf ]] && . $conf
conf=/etc/git-prompt.conf; [[ -r $conf ]] && . $conf
conf=~/.git-prompt.conf; [[ -r $conf ]] && . $conf
conf=~/.config/git-prompt.conf; [[ -r $conf ]] && . $conf
unset conf
##### set defaults if not set
git_module=${git_module:-on}
svn_module=${svn_module:-off}
hg_module=${hg_module:-on}
vim_module=${vim_module:-on}
virtualenv_module=${virtualenv_module:-on}
battery_module=${battery_module:-off}
make_module=${make_module:-off}
jobs_module=${jobs_module:-on}
rc_module=${rc_module:-on}
command_time_module=${command_time_module:-on}
load_module=${load_module:-on}
clock_module=${clock_module:-off}
sudo_module=${sudo_module:-off}
error_bell=${error_bell:-off}
cwd_cmd=${cwd_cmd:-\\w}
default_host_abbrev_mode=${default_host_abbrev_mode:-delete}
default_id_abbrev_mode=${default_id_abbrev_mode:-delete}
prompt_modules_order=${prompt_modules_order:-RC LOAD CTIME VIRTUALENV VCS SUDO WHO_WHERE JOBS BATTERY CWD MAKE}
#### check for battery files, make, disable corresponding module if not installed
# FIXME check other possible locations (BAT1, /proc ?)
if [[ ! -f /sys/class/power_supply/BAT0/present || ! $(< /sys/class/power_supply/BAT0/present) ]]; then
battery_module=off
fi
if [[ -z $(which make 2> /dev/null) ]]; then
make_module=off
fi
#### dir, rc, root color
cols=`tput colors` # in emacs shell-mode tput colors returns -1
if [[ -n "$cols" && $cols -ge 8 ]]; then # if terminal supports colors
dir_color=${dir_color:-CYAN}
slash_color=${slash_color:-CYAN}
slash_color_readonly=${slash_color_readonly:-MAGENTA}
prompt_color=${prompt_color:-white}
rc_color=${rc_color:-red}
virtualenv_color=${virtualenv_color:-green}
user_id_color=${user_id_color:-blue}
root_id_color=${root_id_color:-magenta}
at_color=${at_color:-green}
at_color_remote=${at_color_remote:-RED}
jobs_color_bkg=${jobs_color:-yellow}
jobs_color_stop=${jobs_color:-red}
make_color_ok=${make_color_ok:-BLACK}
make_color_dirty=${make_color_dirty:-RED}
command_time_color=${command_time_color:-YELLOW}
clock_color=${clock_color:-BLACK}
sudo_color=${sudo_color:-RED}
else # only B/W
dir_color=${dir_color:-bw_bold}
rc_color=${rc_color:-bw_bold}
fi
unset cols
#### prompt character, for root/non-root
prompt_char=${prompt_char:-'>'}
root_prompt_char=${root_prompt_char:-'>'}
#### vcs colors
init_vcs_color=${init_vcs_color:-WHITE} # initial
clean_vcs_color=${clean_vcs_color:-blue} # nothing to commit (working directory clean)
modified_vcs_color=${modified_vcs_color:-red} # Changed but not updated:
added_vcs_color=${added_vcs_color:-green} # Changes to be committed:
untracked_vcs_color=${untracked_vcs_color:-BLUE} # Untracked files:
deleted_vcs_color=${deleted_vcs_color:-yellow} # Deleted files:
conflicted_vcs_color=${conflicted_vcs_color:-CYAN} # Conflicted files:
op_vcs_color=${op_vcs_color:-MAGENTA}
detached_vcs_color=${detached_vcs_color:-RED}
hex_vcs_color=${hex_vcs_color:-dim} # gray
max_file_list_length=${max_file_list_length:-100}
short_hostname=${short_hostname:-off}
upcase_hostname=${upcase_hostname:-on}
count_only=${count_only:-off}
rawhex_len=${rawhex_len:-5}
hg_revision_display=${hg_revision_display:-none}
hg_multiple_heads_display=${hg_multiple_heads_display:-on}
command_time_threshold=${command_time_threshold:-15}
clock_style=${clock_style:-analog}
clock_alert_interval=${clock_alert_interval:-30}
enable_utf8=${enable_utf8:-on}
if [[ -z "$load_colors" || -z "$load_thresholds" || ${#load_colors[@]} -ne ${#load_thresholds[@]} ]]; then
load_colors=(BLACK red RED whiteonred)
load_thresholds=(100 200 300 400)
fi
load_display_style=${load_display_style:-bar}
aj_max=20
##################################################################### post config
################# make PARSE_VCS_STATUS
unset PARSE_VCS_STATUS
[[ $git_module = "on" ]] && type git >&/dev/null && PARSE_VCS_STATUS+="parse_git_status"
[[ $svn_module = "on" ]] && type svn >&/dev/null && PARSE_VCS_STATUS+="${PARSE_VCS_STATUS+||}parse_svn_status"
[[ $hg_module = "on" ]] && type hg >&/dev/null && PARSE_VCS_STATUS+="${PARSE_VCS_STATUS+||}parse_hg_status"
PARSE_VCS_STATUS+="${PARSE_VCS_STATUS+||}return"
### determining svn version information
### In svn versions 1.7 and above there is only a single .svn directory
### in the repository root, while before there was a .svn in every subdirectory.
### Here we determine and save svn version information
### and use the appropriate method in the runtime module
### However, if the "svnversion" utility is installed,
### we use its output instead.
if [[ $PARSE_VCS_STATUS =~ "svn" ]]; then
unset svn_method
type svnversion >&/dev/null && svn_method="svnversion"
svn_version_str=$(svn --version 2> /dev/null | head -1 | sed -ne 's/.* \([0-9]\)\.\([0-9]\{1,2\}\).*/\1\2/p')
if [[ "$svn_method" != "svnversion" ]]; then
if [[ $svn_version_str > 16 ]]; then
svn_method="info"
else
svn_method="dotsvn"
fi
fi
unset svn_version_str
fi
################# terminfo colors-16
#
# black? 0 8
# red 1 9
# green 2 10
# yellow 3 11
# blue 4 12
# magenta 5 13
# cyan 6 14
# white 7 15
#
# terminfo setaf/setab - sets ansi foreground/background
# terminfo sgr0 - resets all attributes
# terminfo colors - number of colors
#
################# Colors-256
# To use foreground and background colors:
# Set the foreground color to index N: \033[38;5;${N}m
# Set the background color to index M: \033[48;5;${M}m
# To make vim aware of a present 256 color extension, you can either set
# the $TERM environment variable to xterm-256color or use vim's -T option
# to set the terminal. I'm using an alias in my bashrc to do this. At the
# moment I only know of two color schemes which is made for multi-color
# terminals like urxvt (88 colors) or xterm: inkpot and desert256,
### if term support colors, then use color prompt, else bold
black='\['`tput sgr0; tput setaf 0`'\]'
red='\['`tput sgr0; tput setaf 1`'\]'
green='\['`tput sgr0; tput setaf 2`'\]'
yellow='\['`tput sgr0; tput setaf 3`'\]'
blue='\['`tput sgr0; tput setaf 4`'\]'
magenta='\['`tput sgr0; tput setaf 5`'\]'
cyan='\['`tput sgr0; tput setaf 6`'\]'
white='\['`tput sgr0; tput setaf 7`'\]'
BLACK='\['`tput setaf 0; tput bold`'\]'
RED='\['`tput setaf 1; tput bold`'\]'
GREEN='\['`tput setaf 2; tput bold`'\]'
YELLOW='\['`tput setaf 3; tput bold`'\]'
BLUE='\['`tput setaf 4; tput bold`'\]'
MAGENTA='\['`tput setaf 5; tput bold`'\]'
CYAN='\['`tput setaf 6; tput bold`'\]'
WHITE='\['`tput setaf 7; tput bold`'\]'
whiteonred='\['`tput setaf 7; tput setab 1; tput bold`'\]'
dim='\['`tput sgr0; tput dim`'\]' # half-bright
bw_bold='\['`tput bold`'\]'
on=''
off=': '
bell="\[`eval ${!error_bell} tput bel`\]"
colors_reset='\['`tput sgr0`'\]'
# replace symbolic colors names to raw terminfo strings
init_vcs_color=${!init_vcs_color}
conflicted_vcs_color=${!conflicted_vcs_color}
modified_vcs_color=${!modified_vcs_color}
untracked_vcs_color=${!untracked_vcs_color}
clean_vcs_color=${!clean_vcs_color}
added_vcs_color=${!added_vcs_color}
op_vcs_color=${!op_vcs_color}
detached_vcs_color=${!detached_vcs_color}
deleted_vcs_color=${!deleted_vcs_color}
hex_vcs_color=${!hex_vcs_color}
dir_color=${!dir_color}
slash_color=${!slash_color}
slash_color_readonly=${!slash_color_readonly}
prompt_color=${!prompt_color}
rc_color=${!rc_color}
virtualenv_color=${!virtualenv_color}
user_id_color=${!user_id_color}
root_id_color=${!root_id_color}
at_color=${!at_color}
at_color_remote=${!at_color_remote}
unset PROMPT_COMMAND
# assemble prompt command string based on the module order specified above
# RC, LOAD, CLOCK, CTIME, VIRTUALENV and VCS has to be flanked by spaces on either side
# except if they are at the start or end of the sequence.
# excess spaces (which may occur if some of the modules produce empty output)
# will be trimmed at runtime, in the prompt_command_function.
prompt_command_string=$(echo $prompt_modules_order |
sed '
s/RC/\$space\$rc\$space/;
s/LOAD/$space$load_indicator$space/;
s/CLOCK/$space$clock_indicator$space/;
s/CTIME/$space$command_time$space/;
s/VIRTUALENV/\$space\$virtualenv_string\$space/;
s/VCS/\$space\$head_local\$space/;
s/WHO_WHERE/\$color_who_where\$colors_reset/;
s/JOBS/\$jobs_indicator/;
s/BATTERY/\$battery_indicator/;
s/CWD/\$dir_color\$cwd/;
s/MAKE/\$make_indicator/;
s/SUDO/\$sudo_marker/;
s/ //g;
s/\$space\$space/\$space/g;
s/^\$space//;
s/\$space$//;
s/\$space/ /g;
')
# save startup and midnight timestamp for clock
_gp_clock_timestamp_start=$(date +%s)
_gp_clock_timestamp_midnight=$(date -d 0:0 +%s)
_gp_clock_timestamp_last=${_gp_clock_timestamp_start}
#################################################################### MARKERS
ellipse_marker_utf8="…"
ellipse_marker_plain="..."
_gp_check_utf8() {
if [[ $enable_utf8 == "on" && ("$LC_CTYPE $LC_ALL $LANG" =~ "UTF" || $LANG =~ "utf") && $TERM != "linux" ]]; then
utf8_prompt=1
ellipse_marker=$ellipse_marker_utf8
else
utf8_prompt=
ellipse_marker=$ellipse_marker_plain
fi
}
_gp_check_utf8
export who_where
cwd_truncate() {
# based on: https://www.blog.montgomerie.net/pwd-in-the-title-bar-or-a-regex-adventure-in-bash
# arg1: max path lenght
# returns abbrivated $PWD in public "cwd" var
cwd=${PWD/$HOME/\~} # substitute "~"
case $1 in
full)
return
;;
last)
cwd=${PWD##/*/}
[[ $PWD == $HOME ]] && cwd="~"
return
;;
*)
# if bash < v3.2 then don't truncate
if [[ ${BASH_VERSINFO[0]} -eq 3 && ${BASH_VERSINFO[1]} -le 1 || ${BASH_VERSINFO[0]} -lt 3 ]] ; then
return
fi
;;
esac
# split path into: head='~/', truncateble middle, last_dir
local cwd_max_length=$1
# expression which bash-3.1 or older can not understand, so we wrap it in eval
exp31='[[ "$cwd" =~ (~?/)(.*/)([^/]*)$ ]]'
if eval $exp31 ; then # only valid if path have more then 1 dir
local path_head=${BASH_REMATCH[1]}
local path_middle=${BASH_REMATCH[2]}
local path_last_dir=${BASH_REMATCH[3]}
local cwd_middle_max=$(( $cwd_max_length - ${#path_last_dir} ))
[[ $cwd_middle_max < 0 ]] && cwd_middle_max=0
# trunc middle if over limit
if [[ ${#path_middle} -gt $(( $cwd_middle_max + ${#ellipse_marker} + 5 )) ]]; then
# truncate
middle_tail=${path_middle:${#path_middle}-${cwd_middle_max}}
# trunc on dir boundary (trunc 1st, probably tuncated dir)
exp31='[[ $middle_tail =~ [^/]*/(.*)$ ]]'
eval $exp31
middle_tail=${BASH_REMATCH[1]}
# use truncated only if we cut at least 4 chars
if [[ $(( ${#path_middle} - ${#middle_tail})) -gt 4 ]]; then
cwd=$path_head$ellipse_marker$middle_tail$path_last_dir
fi
fi
fi
return
}
set_shell_label() {
local param full
full="$plain_who_where $@"
short="$*"
# hack to replace garbled bash command under mc on some systems
if [[ "$short" == ". /usr/libexec/mc/mc-wrapper.sh" ]]; then
short="mc"
full="$plain_who_where $short"
fi
xterm_label() {
local args="$*"
printf "\033]0;%s\007" "${args:0:200}"
}
screen_label() {
# FIXME $STY not inherited though "su -"
if [[ "$STY" ]]; then
# workaround screen UTF-8 bug
short=${param//$ellipse_marker/$ellipse_marker_plain}
full=${full//$ellipse_marker/$ellipse_marker_plain}
fi
if [[ "$TMUX" ]]; then
full="$plain_who_where${_gp_tmux_session:+|${_gp_tmux_session}} $short"
fi
# FIXME: run this only if screen is in xterm (how to test for this?)
xterm_label "$full"
printf "\033k%s\033\\" "$short"
}
if [[ -n "$STY" || -n "$TMUX" ]]; then
# in this case, do not prepend host name,
# because screen/tmux statusbar should display it only once -
# displaying it in every window name would waste space
screen_label "$short"
else
case $TERM in
screen*)
# display host name in window title if we're inside screen or tmux locally,
# and ssh'd to a remote server
if [[ -n ${SSH_CLIENT} || -n ${SSH2_CLIENT} || -n ${SSH_CONNECTION} ]]; then
short="@$host:$short"
fi
screen_label "$short"
;;
xterm* | rxvt* | gnome-* | konsole | eterm | wterm )
# is there a capability which we can to test
# for "set term title-bar" and its escapes?
xterm_label "$full"
;;
*) ;;
esac
fi
}
export -f set_shell_label
###################################################### ID (user name)
_gp_get_id() {
id=`id -un`
# abbreviate user name if needed
if [[ "$default_id_abbrev_mode" == "delete" ]]
then
id=${id#$default_user}
elif [[ "$default_id_abbrev_mode" == "abbrev" ]]
then
# only abbreviate if the abbreviated string is actually shorter than the full one
if [[ "$id" == "$default_user" && ${#id} -ge $((${#ellipse_marker} + 1)) ]]
then
id="${id:0:1}$ellipse_marker"
fi
#else
# keep full user name
fi
}
_gp_get_id
########################################################### TTY
_gp_get_tty() {
local tty
tty=$(tty)
tty=${tty/\/dev\/pts\//p} # RH tty devs
tty=${tty/\/dev\/tty/}
tty=${tty/\/dev\/vc\//vc} # gentoo tty devs
# replace tty name with screen number
# however, "screen" as $TERM may also mean tmux
if [[ "$TERM" =~ "screen" ]] ; then
if [[ "$STY" ]]; then
tty="$WINDOW"
elif [[ -n $TMUX && -n $TMUX_PANE ]]; then
# get tmux session name so that we can include it in the window title
# and window number, to display it in the prompt
# TODO configurable prompt marker
# we have to do it like this, because session name may contain spaces
local sep tmux_info
sep="~@~" # used to be \x1f, bug tmux escapes arguments now
tmux_info=$(tmux display-message -t $TMUX_PANE -p "#S${sep}#I" 2> /dev/null)
_gp_tmux_session=${tmux_info/$sep*/}
tty=${tmux_info/*$sep/}
else
tty=
fi
# if we start an ssh connection from within a screen/tmux session,
# the "screen" $TERM setting tends to be preserved.
# In this case we don't want the tty displayed (it would be a misleading "0"),
# unless there is an actual screen/tmux session on the server too.
if [[ -n "$tty" ]]; then
# replace tty number with circled numbers
if [[ $utf8_prompt ]]; then
local -a circled_digits=(⓪ ① ② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩ ⑪ ⑫ ⑬ ⑭ ⑮ ⑯ ⑰ ⑱ ⑲ ⑳)
if [[ "$tty" -ge 0 && "$tty" -le 20 ]]; then
tty="${circled_digits[$tty]} "
fi
else
tty=" $tty"
fi
fi
fi
# we don't need tty name under X11
case $TERM in
xterm* | rxvt* | gnome-terminal | konsole | eterm* | wterm | cygwin) tty= ;;
*);;
esac
_gp_tty="$tty"
}
_gp_get_tty
########################################################### HOST
### we don't display home host/domain $SSH_* set by SSHD or keychain
# How to find out if session is local or remote? Working with "su -", ssh-agent, and so on ?
## is sshd our parent?
# if { for ((pid=$$; $pid != 1 ; pid=`ps h -o pid --ppid $pid`)); do ps h -o command -p $pid; done | grep -q sshd && echo == REMOTE ==; }
#then
_gp_get_host() {
if [[ -n ${SSH_CLIENT} || -n ${SSH2_CLIENT} || -n ${SSH_CONNECTION} ]]; then
probably_ssh_session=1
at_color_cur=$at_color_remote
else
probably_ssh_session=
at_color_cur=$at_color
fi
host=${HOSTNAME}
if [[ $short_hostname = "on" ]]; then
if [[ "$(uname)" =~ "CYGWIN" ]]; then
host=`hostname`
else
host=`hostname -s`
fi
fi
uphost=`echo ${host} | tr a-z-. A-Z_`
host_color=${uphost}_host_color
host_color=${!host_color}
if [[ -z $host_color && -x /usr/bin/cksum ]] ; then
cksum_color_no=`echo $uphost | cksum | awk '{print $1%6}'`
color_index=(green yellow blue magenta cyan white) # FIXME: bw, color-256
host_color=${color_index[cksum_color_no]}
fi
# abbreviate host name if needed
# disregard setting and display full host if session is remote
if [[ "$default_host_abbrev_mode" == "delete" && -z $probably_ssh_session ]]
then
host=${host#$default_host}
elif [[ "$default_host_abbrev_mode" == "abbrev" && -z $probably_ssh_session ]]
then
# only abbreviate if the abbreviated string is actually shorter than the full one
if [[ "$host" == "$default_host" && ${#host} -ge $((${#ellipse_marker} + 1)) ]]
then
host="${host:0:1}$ellipse_marker"
fi
else
# set upcase hostname if needed
if [[ $upcase_hostname = "on" ]]; then
host=${uphost}
fi
fi
host_color=${!host_color}
# we might already have short host name
[[ -n $default_domain ]] && host=${host%.$default_domain}
unset probably_ssh_session
}
_gp_get_host
#################################################################### WHO_WHERE
# [[user@]host[-tty]]
_gp_set_who_where() {
if [[ -n $id || -n $host ]] ; then
[[ -n $id && -n $host ]] && at='@' || at=''
color_who_where="${id//\\/\\\\}${host:+$at_color_cur$at$host_color$host}${_gp_tty:+$_gp_tty}"
plain_who_where="${id}$at$host"
# if root then make it root_color
if [ "$id" == "root" ] ; then
user_id_color=$root_id_color
prompt_char="$root_prompt_char"
fi
color_who_where="$user_id_color$color_who_where$colors_reset"
else
color_who_where=''
fi
# There are at least two separate problems with mc:
# it clobbers $PROMPT_COMMAND, so none of the dynamically generated
# components can work,
# and it swallows escape sequences, so colors don't work either.
# Here we try to salvage some of the functionality for shells within mc.
#
# specifically exclude emacs, want full when running inside emacs
if [[ -z "$TERM" || ("$TERM" = "dumb" && -z "$INSIDE_EMACS") || -n "$MC_SID" ]]; then
unset PROMPT_COMMAND
if [[ -n $id || -n $host ]] ; then
PS1="$color_who_where:\w$prompt_char "
else
PS1="\w$prompt_char "
fi
return 1
fi
}
_gp_set_who_where
# exit if in mc (see above)
[[ $? -ne 0 ]] && return 1
create_battery_indicator () {
# if not a laptop: :
# if laptop on AC, not charging: ⚡
# if laptop on AC, charging: ▕⚡▏
# if laptop on battery: one of ▕▁▏▕▂▏▕▃▏▕▄▏▕▅▏▕▆▏▕▇▏▕█▏
# color: red if power < 30 %, else normal
local battery_present battery_status battery_percent battery_color battery_pwr_index tmp
local -a battery_diagrams
battery_present=$(< /sys/class/power_supply/BAT0/present)
if [[ $battery_present ]]; then
battery_status=$(< /sys/class/power_supply/BAT0/status)
battery_percent=$(< /sys/class/power_supply/BAT0/capacity)
if [[ "$battery_status" =~ "Discharging" ]]; then
if [[ $utf8_prompt ]]; then
battery_diagrams=( ▕▁▏ ▕▂▏ ▕▃▏ ▕▄▏ ▕▅▏ ▕▆▏ ▕▇▏ ▕█▏ )
battery_pwr_index=$(($battery_percent/13))
battery_indicator=${battery_diagrams[battery_pwr_index]}
else
battery_indicator="|$battery_percent|"
fi
elif [[ "$battery_status" =~ "Charging" ]]; then
if [[ $utf8_prompt ]]; then
battery_indicator="▕⚡▏"
else
battery_indicator="|^|"
fi
else
if [[ $utf8_prompt ]]; then
battery_indicator=" ⚡ "
else
battery_indicator=" = "
fi
fi
if [[ $battery_percent -ge 31 ]]; then
battery_color=$colors_reset
elif [[ $battery_percent -ge 11 ]]; then
battery_color=$RED
else
battery_color=$whiteonred
fi
else
battery_indicator=":"
battery_color=$colors_reset
fi
battery_indicator="$battery_color$battery_indicator$colors_reset"
}
create_jobs_indicator() {
# background jobs ⚒⚑⚐⚠
local jobs_bkg=$(jobs -r)
local jobs_stop=$(jobs -s)
if [[ -n $jobs_bkg || -n $jobs_stop ]]; then
if [[ $utf8_prompt ]]; then
jobs_indicator="⚒"
else
jobs_indicator="%"
fi
if [[ -n $jobs_stop ]]; then
jobs_indicator="${!jobs_color_stop}$jobs_indicator$colors_reset"
else
jobs_indicator="${!jobs_color_bkg}$jobs_indicator$colors_reset"
fi
else
jobs_indicator=""
fi
}
check_make_status() {
make_indicator=""
[[ $make_ignore_dir_list =~ $PWD ]] && return
local myrc
if [[ -e Makefile ]]; then
if [[ $utf8_prompt ]]; then
make_indicator="⚑"
else
make_indicator="*"
fi
make -q &> /dev/null
myrc=$?
if [[ $myrc -eq 0 ]]; then
make_indicator="${!make_color_ok}$make_indicator"
else
make_indicator="${!make_color_dirty}$make_indicator"
fi
else
make_indicator=""
fi
}
meas_command_time() {
if [[ ${_gp_timestamp} ]]; then
local elapsed_time=$(($SECONDS - ${_gp_timestamp}))
if [[ $elapsed_time -gt $command_time_threshold ]]; then
command_time="${!command_time_color}${elapsed_time}s$colors_reset"
fi
fi
unset _gp_timestamp
}
create_clock() {
clock_indicator=""
# this contrived calculation is done to avoid calling `date` every time
local current_time time_of_day index
current_time=$(($SECONDS + ${_gp_clock_timestamp_start}))
if [[ $clock_alert_interval -gt 0 ]]; then
[[ $(( ($current_time - ${_gp_clock_timestamp_last})/(60 * $clock_alert_interval) )) -eq 0 ]] && return
fi
if [[ $clock_style == "analog" && $utf8_prompt ]]; then
# unicode clock face characters
# U+1F550 (ONE OCLOCK) .. U+1F55B (TWELVE OCLOCK), for the plain hours
# U+1F55C (ONE-THIRTY) .. U+1F567 (TWELVE-THIRTY), for the thirties
local clockfaces=(🕧 🕐 🕜 🕑 🕝 🕒 🕞 🕓 🕟 🕔 🕠 🕕 🕡 🕖 🕢 🕗 🕣 🕘 🕤 🕙 🕥 🕚 🕦 🕛)
time_of_day=$(( (${current_time} - ${_gp_clock_timestamp_midnight}) % 86400 ))
index=$(( (($time_of_day - 900) % 43200) / 1800 ))
clock_indicator="${!clock_color}${clockfaces[$index]}${colors_reset}"
else
clock_indicator="${!clock_color}\t${colors_reset}"
fi
_gp_clock_timestamp_last=$current_time
}
create_load_indicator () {
local load_color load_value load_str load_bar load_mark i j
if [[ "$OSTYPE" =~ "linux" ]]; then
local eol
read load_str eol < /proc/loadavg
load_str=${load_str## *}
else
load_str=$(uptime | sed -ne 's/.* load average: \([0-9]\.[0-9]\{1,2\}\).*/\1/p')
fi
load_value=${load_str/\./}
load_value=$((10#$load_value))
if [[ $load_value -le ${load_thresholds[0]} ]]; then
load_indicator=""
return
fi
for ((i = ${#load_thresholds[@]} ; i > 0 ; i--)); do
j=$((i-1))
if [[ $load_value -gt ${load_thresholds[$j]} ]]; then
load_color=${!load_colors[$j]}
break
fi
done
if [[ $utf8_prompt ]]; then
load_mark="☢"
if [[ $load_display_style == "bar" ]]; then
local load_int=$((load_value / 100 - 1))
local load_frac=$((load_value % 100))
load_frac=$((load_frac / 12))
local -a load_chars=( " " "▏" "▎" "▍" "▌" "▋" "▊" "▉" "█" )
printf -v load_str "%${load_int}s"
load_str=${load_str// /◙}
load_str+=${load_chars[$load_frac]}
fi
else
load_mark="L"
fi
if [[ $load_display_style == "markonly" ]]; then load_str= ; fi
load_indicator="$load_color$load_mark$load_str$colors_reset"
}
create_sudo_marker() {
sudo_marker=
if [[ $utf8_prompt ]]; then
sudo -nl &> /dev/null && sudo_marker="${!sudo_color}⚷$colors_reset"
else
sudo -nl &> /dev/null && sudo_marker="${!sudo_color}!$colors_reset"
fi
}
parse_svn_status() {
local svn_info_str myrc rev
case $svn_method in
svnversion) rev=$(svnversion 2> /dev/null)
myrc=$?
[[ $myrc -ne 0 || "$rev" == "exported" || "$rev" =~ "Unversioned" ]] && return 1
;;
info) svn_info_str=$(svn info 2> /dev/null)
myrc=$?
[[ $myrc -eq 0 ]] || return 1
rev=${svn_info_str##*Revision: }
rev=${rev%%[[:space:]]*}
;;
dotsvn) [[ -d .svn ]] || return 1
svn_info_str=$(svn info 2> /dev/null)
myrc=$?
[[ $myrc -eq 0 ]] || return 1
rev=${svn_info_str##*Revision: }
rev=${rev%%[[:space:]]*}
;;
*) return 1
esac
vcs=svn
### get status
unset status modified added clean init deleted untracked conflicted op detached
eval `svn status 2>/dev/null | sed 's/\\\\/x/g' |
sed -n '
s/^A... \([^.].*\)/ added_files+=(\"\1\");/p
s/^M... \([^.].*\)/ modified_files+=(\"\1\");/p
s/^R... \([^.].*\)/ added=added;/p
s/^D... \([^.].*\)/ deleted_files+=(\"\1\");/p
s/^C... \([^.].*\)/ conflicted_files+=(\"\1\");/p
s/^\!... \([^.].*\)/ deleted_files+=(\"\1\");/p
s/^\?... \([^.].*\)/ untracked_files+=(\"\1\");/p
'
`
modified=${modified_files[0]:+modified}
added=${added_files[0]:+added}
deleted=${deleted_files[0]:+deleted}
untracked=${untracked_files[0]:+untracked}
conflicted=${conflicted_files[0]:+conflicted}
# TODO branch detection if standard repo layout
[[ -z $modified ]] && \
[[ -z $untracked ]] && \
[[ -z $added ]] && \
[[ -z $deleted ]] && \
[[ -z $conflicted ]] && \
clean=clean
vcs_info=r$hex_vcs_color$rev$colors_reset
}
parse_hg_status() {
# ☿
# Get all information we need in one go from hg log's output
# if we're not in a hg directory, this takes exactly the same time as 'hg root' would do,
# and if we're in a hg dir, we don't have to call 'hg branch' and 'hg id' separately.
local id_str
id_str=$(hg log --follow -l 1 --template '{rev}\x1f{node}\x1f{tags}\x1f{branches}\x1f{bookmarks}\x1f{phase}' 2> /dev/null) || return 1
# This contrived way is necessary because branch names and tags can contain spaces.
# The ASCII "Unit separator" \x1f was chosen as a "safe" separator character
# because it was intended for exactly this purpose.
# Nowadays nobody knows that such a character exists at all :)
local oldIFS
oldIFS="$IFS"
IFS=$'\x1f'
local -a id_array
id_array=($id_str)
IFS="$oldIFS"
local branch bookmark num rev tags tip_regex not_uptodate phase
num="${id_array[0]}"
rev="${id_array[1]}"
tags="${id_array[2]}"
branch="${id_array[3]}"
bookmark="${id_array[4]}"
phase="${id_array[5]}"
vcs=hg
### get status
unset status modified added clean init deleted untracked op detached
eval `hg status 2>/dev/null | sed 's/\\\\/x/g' |
sed -n '
s/^M \([^.].*\)/ modified_files+=(\"\1\");/p
s/^A \([^.].*\)/ added_files+=(\"\1\");/p
s/^R \([^.].*\)/ deleted_files+=(\"\1\");/p
s/^\! \([^.].*\)/ deleted_files+=(\"\1\");/p
s/^\? \([^.].*\)/ untracked_files+=(\"\1\");/p
'`
### EXPERIMENTAL: it is actually faster, especially for many files
# eval `hg status 2>/dev/null |
# perl -lne '
# push @{$x{substr($_,0,1)}}, substr($_,2);
# END {
# print qq/modified_files=(/, (map {qq/ "$_" /} @{$x{M}}), q/);/;
# print qq/added_files=(/, (map {qq/ "$_" /} @{$x{A}}), q/);/;
# print qq/deleted_files=(/, (map {qq/ "$_" /} @{$x{R}}, @{$x{"!"}}), q/);/;
# print qq/untracked_files=(/, (map {qq/ "$_" /} @{$x{"?"}}), q/);/;
# }
# '`
modified=${modified_files[0]:+modified}
added=${added_files[0]:+added}
deleted=${deleted_files[0]:+deleted}
untracked=${untracked_files[0]:+untracked}
[[ -z $modified ]] && [[ -z $untracked ]] && [[ -z $added ]] && [[ -z $deleted ]] && clean=clean
# older versions of hg log --template '{branch}' report empty if branch is default
[[ -z $branch ]] && branch=default
vcs_info=${branch/default/D}
if [[ "$bookmark" ]] ; then
vcs_info+=/$bookmark
fi
if [[ $hg_multiple_heads_display == "on" ]]; then
local hg_heads
hg_heads=$(hg heads --template '{rev}\n' $branch 2> /dev/null | wc -l)
if [[ $hg_heads -gt 1 ]]; then
detached=detached
local excl_mark='!'
vcs_info="$detached_vcs_color$hg_heads$excl_mark$vcs_info"
fi
fi
local hg_vcs_char hg_up_char
if [[ $utf8_prompt ]]; then
hg_vcs_char="☿"
hg_up_char="⬆"
case $phase in
public) phase="${green}⚌";; # ☻
draft) phase="${yellow}⚍";; # ☺
secret) phase="${red}⚏";; # ☹
*) phase="" ;;
esac
else
hg_vcs_char=":"
hg_up_char="^"
phase=${phase:0:3}
case $phase in
public) phase="${green}pub";;
draft) phase="${yellow}dra";;
secret) phase="${red}sec";;
*) phase="";;
esac
fi
tip_regex=\\btip\\b
if [[ ! $tags =~ $tip_regex ]]; then
not_uptodate="$YELLOW$hg_up_char"
fi
local hg_revision
case $hg_revision_display in
id) hg_revision=$rev
hg_revision="$hex_vcs_color$hg_vcs_char${hg_revision:0:$rawhex_len}"
;;
num) hg_revision=$num
hg_vcs_char="#"
hg_revision="$hex_vcs_color$hg_vcs_char$hg_revision"
;;
*) hg_revision="" ;;
esac
vcs_info+=$hg_revision$phase$not_uptodate
}
parse_git_status() {
# TODO add status: LOCKED (.git/index.lock)
git_dir=`[[ $git_module = "on" ]] && git rev-parse --git-dir 2> /dev/null`
#git_dir=`eval \$$git_module git rev-parse --git-dir 2> /dev/null`
#git_dir=` git rev-parse --git-dir 2> /dev/null`
[[ -n ${git_dir/./} ]] || return 1
vcs=git
########################################################## GIT STATUS
[[ $rawhex_len -gt 0 ]] && freshness="$dim="
unset branch status modified added clean init deleted untracked op detached
if [[ $utf8_prompt ]]; then
git_up_char="↑"
git_dn_char="↓"
git_updn_char="↕"
git_stash_char="☡";
else
git_up_char="^"
git_dn_char="v"
git_updn_char="*"
git_stash_char="$"
fi
# info not in porcelain status
eval " $(
LANG=C git status 2>/dev/null | sed 's/\\\\/x/g' |