forked from waltermeyer/fish-lp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfish_prompt.fish
1355 lines (1169 loc) · 45.9 KB
/
fish_prompt.fish
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
# fish-lp
# Copyright (C) 2017 James W. Barnett
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
## Functions
function fish_prompt --description 'Write out the prompt'
# _lp_prompt is automatically called before printing a new prompt
# (thanks to the 'fish_prompt' event) and sets $LP_PROMPT.
echo "$LP_PROMPT"
end
function _lp_init --description 'Initialize liquidprompt'
## Liquidprompt init functions
function _lp_os_detect --description 'Detect the OS'
set -l _UNAME (uname)
switch "$_UNAME"
case DragonFly
set -g _LP_OS FreeBSD
case '*'
set -g _LP_OS "$_UNAME"
end
if [ "$_LP_OS" = "Darwin" ]
set -g LP_DWIN_KERNEL_REL_VER (uname -r | cut -d . -f 1)
end
end
function _lp_cpu_count --description 'Count CPUs'
switch "$_LP_OS"
case FreeBSD Darwin OpenBSD
set -g _LP_CPUNUM (sysctl -n hw.ncpu)
case SunOS
set -g _LP_CPUNUM (kstat -m cpu_info | grep -c "module: cpu_info")
# liquidprompt for bash/zsh defaults to Linux.
case '*'
set -g _LP_CPUNUM (nproc 2> /dev/null; or grep -c '^[Pp]rocessor' /proc/cpuinfo)
end
end
function _lp_cpu_load_choose --description 'Choose the right _lp_cpu_load function'
# Only the Linux version has been test so far !
function _lp_cpu_load_linux --description 'Get the CPU load on Linux'
if not set -q LP_ENABLE_LOAD
return
end
set -l load
set -l eol
read load eol < /proc/loadavg
echo "$load"
end
function _lp_cpu_load_bsd --description 'Get the CPU load on {Free,Open}BSD, Darwin'
if not set -q LP_ENABLE_LOAD
return
end
set -l bol
set -l load
set -l eol
sysctl -n vm.loadavg | read bol load eol
echo "$load"
end
function _lp_cpu_load_sunos --description 'Get the CPU load on SunOS'
if not set -q LP_ENABLE_LOAD
return
end
uptime | awk '{print substr($10,0,length($10))}'
end
functions -e _lp_cpu_load
switch $_LP_OS
case FreeBSD Darwin OpenBSD
functions -c _lp_cpu_load_bsd _lp_cpu_load
case SunOS
functions -c _lp_cpu_load_sunos _lp_cpu_load
# The OS detection defaults to Linux. Any OS that is not one
# of the previous would anyway default to this.
case '*'
functions -c _lp_cpu_load_linux _lp_cpu_load
end
functions -e _lp_cpu_load_linux
functions -e _lp_cpu_load_bsd
functions -e _lp_cpu_load_sunos
end
function _lp_user --description 'Username'
# Beware the subtlety! Here, we are setting _LP_USER!
# We are checking somewhere else whether it should be displayed.
# The user can decide it at runtime, so we are setting a hidden
# _LP_USER that will remain unchanged.
# If LP_USER has to be displayed, it is set to the value of _LP_USER
# and when it has to be hidden, LP_USER is erased.
set -g _LP_USER
if [ (id -u) -eq 0 ]
# If the user is root.
set -g _LP_USER_IS_ROOT
set _LP_USER "$LP_COLOR_USER_ROOT""$USER""$NO_COL"
else if [ "$USER" != (logname 2> /dev/null; or echo $LOGNAME) ]
# If the user is not login user.
set -g LP_USER_IS_NOT_LOGIN
set _LP_USER "$LP_COLOR_USER_ALT""$USER""$NO_COL"
else
set _LP_USER "$LP_COLOR_USER_LOGGED""$USER""$NO_COL"
set -e LP_USER
end
end
function _lp_hostname --description 'Hostname'
# Same subtlety as in _lp_user! We are setting _LP_HOST, not LP_HOST!
function _lp_connection
# Local function to get fish's parent PID.
# Fish doesn't provide a $PPID env variable.
function _lp_fish_parent --description 'Get fish\'s parent'
set -l FISH_PID %self
set -l FISH_PPID (ps -f "$FISH_PID" | tail -n 1 | tr -s ' ' | cut -d ' ' -f 3)
echo (ps -o comm= -p "$FISH_PPID" 2> /dev/null)
end
if [ -n "$SSH_CLIENT$SSH2_CLIENT$SSH_TTY" ]
echo ssh
else
set -l SESS_SRC (who am i | sed -n 's/.*(\(.*\))/\1/p')
set -l SESS_PARENT (_lp_fish_parent)
if [ -z "$SESS_SRC" -o "$SESS_SRC" = ":" -o "$SESS_PARENT" = "screen" -o "$SESS_PARENT" = "tmux" ]
echo lcl
else if [ "$SESS_PARENT" = "su" -o "$SESS_PARENT" = "sudo" ]
echo su
else
echo tel
end
end
functions -e _lp_fish_parent
end
function _lp_chroot
if [ -r /etc/debian_chroot ]
echo "("(cat /etc/debian_chroot)")"
end
end
set -g _LP_HOST
set -g _LP_HOST (_lp_chroot)
# Connection with X11 support.
# Not sure this works with fish...
if [ -n "$DISPLAY" ]
set _LP_HOST "$LP_COLOR_X11_ON""$_LP_HOST""@""$NO_COL"
else
set _LP_HOST "$LP_COLOR_X11_OFF""$_LP_HOST""@""$NO_COL"
end
set -l _HOSTNAME (hostname -s)
switch (_lp_connection)
case lcl
set -g LP_HOST_LOCAL
set _LP_HOST "$_LP_HOST""$LP_COLOR_HOST""$_HOSTNAME""$NO_COL"
case ssh
if set -q LP_ENABLE_SSH_COLORS
function _lp_hash_to_color
set -l colors "red" "green" "yellow" "blue" "purple" "cyan"
echo "$colors[$argv[1]]"
end
set -l hash (math "1 + "(hostname | cksum | cut -d " " -f 1)" % 6")
set -l color (set_color (_lp_hash_to_color "$hash"))
set _LP_HOST "$_LP_HOST""$color""$_HOSTNAME""$NO_COL"
functions -e _lp_hash_to_color
else
set _LP_HOST "$_LP_HOST""$LP_COLOR_SSH""$_HOSTNAME""$NO_COL"
end
case su
set _LP_HOST "$_LP_HOST""$LP_COLOR_SU""$_HOSTNAME""$NO_COL"
case tel
set _LP_HOST "$_LP_HOST""$LP_COLOR_TELNET""$_HOSTNAME""$NO_COL"
case '*'
set _LP_HOST "$_LP_HOST""$_HOSTNAME"
end
functions -e _lp_connection
functions -e _lp_chroot
end
function _lp_jobs_hack --description 'Hack for jobs processing'
# Fish shell's builtin jobs command does not provide any option
# for running/stopped jobs at the moment. It displays all jobs.
# It is translated so we can't merely grep words such as “stopped”
# or “running”.
# At least, we know their position in the output.
# Any command that doesn't end until the user kills it is good.
# For now, let's use ping.
ping localhost > /dev/null &
set -g _LP_RUNNING_JOB_WORD (jobs -l | cut -f 4)
kill -9 (jobs -l -p)
end
function _lp_title_choose --description 'Choose the right _lp_title function'
## Local functions
function _lp_title_screen --description 'Title in screen TERMs'
set -l txt (_lp_as_text "$LP_PROMPT")
echo "$LP_SCREEN_TITLE_OPEN""$txt""$LP_SCREEN_TITLE_CLOSE"
end
function _lp_title_linux --description 'Title in linux TERMs'
end
function _lp_title_other --description 'Title in other TERMs'
echo (_lp_as_text "$LP_PROMPT")
end
functions -e _lp_title
switch "$TERM"
case screen'*'
functions -c _lp_title_screen _lp_title
case linux'*'
functions -c _lp_title_linux _lp_title
case '*'
functions -c _lp_title_other _lp_title
end
functions -e _lp_title_screen
functions -e _lp_title_linux
functions -e _lp_title_other
end
## Functions used at runtime
function _lp_as_text --description 'Get pure text from a string'
# Remove colors from the computed prompt.
set -l pst
switch $_LP_OS
case Linux FreeBSD SunOS
set pst (echo $argv[1] | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")
case Darwin
set pst (echo $argv[1] | sed -E "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")
end
# Remove escape sequences
# set -l op (printf "%q" $_LP_OPEN_ESC)
# set -l cl (printf "%q" $_LP_CLOSE_ESC)
# Replace all open _or_ close tags with nothing.
# set pst (echo $pst | sed "s,$op\|$cl,,g")
echo -n $pst
end
function _lp_title_default --description 'Default fish_title'
echo $_ ' '
pwd
end
function _lp_return_value --description 'Print the status'
if [ "$argv[1]" -ne 0 ]
echo "$LP_COLOR_ERR""$argv[1]""$NO_COL"" "
end
end
function _lp_color_map --description 'Color map'
set -l value "$argv[1]"
set -l scale
if [ (count "$argv") -eq 2 ]
# Custom scale
set scale "$argv[2]"
else
# Default scale 0..100
set scale 100
end
# - Transform the value to a 0..100 scale:
# value := argv[1] * 100 / scale
# - 0 <= value <= 100. So the colormap index is:
# index := value / 10
# Thus:
set -l index (math "$value * 10 / $scale")
eval "echo -ne \$LP_COLORMAP_$index"
end
function _lp_cpu_load_color --description 'Print the CPU load'
if not set -q LP_ENABLE_LOAD
return
end
# Remove the dot and all leading zeros.
set -l load (_lp_cpu_load | sed 's/\.//g;s/^0*//g' )
if [ -n "$load" ]
set load (math "$load / $_LP_CPUNUM")
else
set load 0
end
if [ "$load" -ge "$LP_LOAD_THRESHOLD" ]
set -l ret
if set -q LP_PERCENTS_ALWAYS
set ret "$NO_COL""$LP_MARK_LOAD"(_lp_color_map "$load" 200)"$load""%"
else
set ret (_lp_color_map "$load" 200)"$LP_MARK_LOAD"
end
echo -ne "$ret""$NO_COL"" "
end
end
function _lp_temp_sensors --description 'Auxiliary function for _lp_temperature'
# Return the average system temperature we get through the sensors command
set -l count 0
set -l temperature 0
for i in (sensors | grep -E "^(Core|temp)" | sed -r "s/.*: *\+([0-9]*)\..°.*/\1/g")
set temperature (math "$temperature + $i")
set count (math "$count + 1")
end
echo -ne (math "$temperature / $count")
end
function _lp_temperature --description 'Print the temperature'
# Will display the numeric value as we got it through the _lp_temp_function
# and colorize it through _lp_color_map.
if not set -q LP_ENABLE_TEMP
return
end
if not set -q LP_ENABLE_SENSORS
return
end
set -l temperature (_lp_temp_sensors)
if [ $temperature -ge $LP_TEMP_THRESHOLD ]
set -l ret
if set -q LP_PERCENTS_ALWAYS
echo -ne "$NO_COL""$LP_MARK_TEMP"(_lp_color_map "$temperature" 120)"$temperature""°"
else
set ret (_lp_color_map "$temperature" 120)"$LP_MARK_TEMP"
end
echo -ne "$ret""$NO_COL"" "
end
end
function _lp_battery --description 'Auxiliary function for _lp_battery_color'
if not set -q LP_ENABLE_BATT
return
end
# Extract the battery load value in percent.
set -l bat (acpi --battery 2> /dev/null | sed 's/\%.*$//;s/^Battery .*, //')
if [ -z "$bat" ]
# Battery level not found.
return 4
else if [ (acpi --ac-adapter | sed 's/^Adapter .*: //') = "off-line" ]
# The battery is discharging.
if [ "$bat" -le "$LP_BATTERY_THRESHOLD" ]
echo -n "$bat"
return 0
else
echo -n "$bat"
return 1
end
else
# The battery is charging.
if [ "$bat" -le "$LP_BATTERY_THRESHOLD" ]
echo -n "$bat"
return 2
else
echo -n "$bat"
return 3
end
end
end
function _lp_battery_color --description 'Print the battery state'
if not set -q LP_ENABLE_BATT
return
end
set -l mark "$LP_MARK_BATTERY"
set -l chargingmark "$LP_MARK_ADAPTER"
set -l bat (_lp_battery)
set -l ret "$status"
if [ "$ret" -eq 4 -o "$bat" -eq 100 ]
# No battery support or battery full: nothing displayed.
return
else if [ "$ret" -eq 3 -a "$bat" -ne 100 ]
# The battery is charging and above the threshold and not 100%
# Green ⏚
echo -ne "$LP_COLOR_CHARGING_ABOVE""$chargingmark""$NO_COL"" "
return
else if [ "$ret" -eq 2 ]
# The battery is charging but under the threshold.
# Yellow ⏚
echo -ne "$LP_COLOR_CHARGING_UNDER""$chargingmark""$NO_COL"" "
return
else if [ "$ret" -eq 1 ]
# The battery is discharging but above the threshold.
# Yellow ⌁
echo -ne "$LP_COLOR_DISCHARGING_ABOVE""$mark""$NO_COL"" "
return
else if [ -n "$bat" ]
# The battery is discharging and under the threshold.
if set -q LP_PERCENTS_ALWAYS
set ret "$NO_COL""$mark"
if [ "$bat" -le 100 -a "$bat" -gt 80 ]
set ret "$ret""$LP_COLORMAP_1"
else if [ "$bat" -le 80 -a "$bat" -gt 65 ]
set ret "$ret""$LP_COLORMAP_2"
else if [ "$bat" -le 65 -a "$bat" -gt 50 ]
set ret "$ret""$LP_COLORMAP_3"
else if [ "$bat" -le 50 -a "$bat" -gt 40 ]
set ret "$ret""$LP_COLORMAP_4"
else if [ "$bat" -le 40 -a "$bat" -gt 30 ]
set ret "$ret""$LP_COLORMAP_5"
else if [ "$bat" -le 30 -a "$bat" -gt 20 ]
set ret "$ret""$LP_COLORMAP_6"
else if [ "$bat" -le 20 -a "$bat" -gt 10 ]
set ret "$ret""$LP_COLORMAP_7"
else if [ "$bat" -le 10 -a "$bat" -gt 5 ]
set ret "$ret""$LP_COLORMAP_8"
else if [ "$bat" -le 5 -a "$bat" -gt 0 ]
set ret "$ret""$LP_COLORMAP_9"
else
# for debugging purpose
set ret "$ret""$LP_COLORMAP_0"
end
set ret "$ret""$bat""%"
else
set ret "$LP_COLOR_DISCHARGING_UNDER""$mark""$NO_COL"
end # LP_PERCENTS_ALWAYS
echo -ne "$ret""$NO_COL"" "
end # ret
end
function _lp_proxy --description 'Proxy'
if not set -q LP_ENABLE_PROXY
return
end
if [ -n "$http_proxy" ]
echo -ne "$LP_COLOR_PROXY""$LP_MARK_PROXY""$NO_COL"" "
end
end
function _lp_virtualenv --description 'Virtualenv'
if not set -q LP_ENABLE_VIRTUALENV
return
end
if [ -n "$VIRTUAL_ENV" ]
echo "[""$LP_COLOR_VIRTUALENV"(basename "$VIRTUAL_ENV")"$NO_COL""]"" "
end
end
function _lp_jobcount --description 'Print jobs info'
if not set -q LP_ENABLE_JOBS
return
end
set -l all_jobs (jobs | wc -l)
set -l running (jobs | grep "$_LP_RUNNING_JOB_WORD" | wc -l)
set -l stopped (math "$all_jobs - $running")
if set -q LP_ENABLE_SCREEN
set -l n_screen (screen -ls 2> /dev/null | grep -c -E "Detached|detached")
end
[ -z "$n_screen" ]; and set n_screen 0
if set -q LP_ENABLE_TMUX
set -l n_tmux (tmux list-sessions 2> /dev/null | grep -cv -E "attached")
end
[ -z "$n_tmux" ]; and set n_tmux 0
set -l detached (math "$n_screen + $n_tmux")
set -l m_detached "d"
set -l m_stop "z"
set -l m_run "&"
set -l ret
if [ "$detached" -ne 0 ]
set ret "$ret"(set_color -o)"$NO_COL""$LP_COLOR_JOB_D""$detached""$NO_COL""$LP_COLOR_JOB_D""$m_detached""$NO_COL"
end
if [ "$running" -ne 0 ]
[ -n "$ret" ]; and set ret "$ret""/"
set ret "$ret"(set_color -o)"$NO_COL""$LP_COLOR_JOB_R""$running""$NO_COL""$LP_COLOR_JOB_R""$m_run""$NO_COL"
end
if [ "$stopped" -ne 0 ]
[ -n "$ret" ]; and set ret "$ret""/"
set ret "$ret"(set_color -o)"$NO_COL""$LP_COLOR_JOB_Z""$stopped""$NO_COL""$LP_COLOR_JOB_Z""$m_stop""$NO_COL"
end
[ -n "$ret" ]; and set ret "$ret"" "
echo -ne "$ret"
end
function _lp_git --description 'git'
set -l branch (__fish_git_prompt | sed 's/^ (//g;s/)$//g')
if [ -n "$branch" ]
set -l ret
set -l end "$NO_COL"
if [ (git status 2> /dev/null | grep "# Untracked") ]
set end "$LP_COLOR_CHANGES""$LP_MARK_UNTRACKED""$end"
end
if [ (git stash list 2> /dev/null) ]
set end "$LP_COLOR_COMMITS""$LP_MARK_STASH""$end"
end
set -l remote (git config --get branch.{$branch}.remote 2> /dev/null)
set -l has_commit 0
if [ -n "$remote" ]
set -l remote_branch (git config --get branch.{$branch}.merge)
if [ -n "$remote_branch" ]
set -l tmp_string (echo $remote_branch | sed "s/refs\/heads/refs\/remotes\/$remote/")
set has_commit (git rev-list --no-merges --count {$tmp_string}..HEAD 2> /dev/null)
if [ -z "$has_commit" ]
set has_commit 0
end
end
end
set -l shortstat (git diff --shortstat 2> /dev/null)
if [ -n "$shortstat" ]
set -l has_lines (echo "$shortstat" | sed 's/^.*changed, //;s/ insert.*, /\/-/;s/ delet.*$//')
if [ (echo "$shortstat" | grep "insertion") ]
set has_lines "+""$has_lines"
else
set has_lines "-""$has_lines"
end
if [ "$has_commit" -gt 0 ]
set ret "$LP_COLOR_CHANGES""$branch""$NO_COL""(""$LP_COLOR_DIFF""$has_lines""$NO_COL"",""$LP_COLOR_COMMITS""$has_commit""$NO_COL"")"
else
set ret "$LP_COLOR_CHANGES""$branch""$NO_COL""(""$LP_COLOR_DIFF""$has_lines""$NO_COL"")"
end
else
if [ "$has_commit" -gt 0 ]
set ret "$LP_COLOR_COMMITS""$branch""$NO_COL""(""$LP_COLOR_COMMITS""$has_commit""$NO_COL"")"
else
set ret "$LP_COLOR_UP""$branch""$NO_COL"
end
end
echo "$ret""$end"" "
end
end
function _lp_hg --description 'mercurial'
set -l ret
set -l branch (hg branch 2> /dev/null)
if [ -z "$branch" ]
return
end
set -l has_untracked (hg status 2>/dev/null | grep '\(^\?\)' | wc -l)
if [ -n "$has_untracked" ]
set has_untracked "$LP_COLOR_CHANGES""$LP_MARK_UNTRACKED"
end
set -l has_commit (hg outgoing --no-merges "$branch" 2>/dev/null | grep '\(^changeset\:\)' | wc -l)
if [ -z "$has_commit" ]
set has_commit 0
end
if [ (hg status --quiet -n | wc -l) -eq 0 ]
if [ "$has_commit" -gt 0 ]
# some commit(s) to push
set ret "$LP_COLOR_COMMITS""$branch""$NO_COL""(""$LP_COLOR_COMMITS""$has_commit""$NO_COL"")""$has_untracked""$NO_COL"
else
set ret "$LP_COLOR_UP""$branch""$has_untracked""$NO_COL" # nothing to commit or push
end
else
set -l has_line (hg diff --stat 2>/dev/null | tail -n 1 | awk 'FS=" " {printf("+%s/-%s\n", $4, $6)}')
if [ "$has_commit" -gt 0 ]
# Changes to commit and commits to push
set ret "$LP_COLOR_CHANGES""$branch""$NO_COL""(""$LP_COLOR_DIFF""$has_lines""$NO_COL"",""$LP_COLOR_COMMITS""$has_commit""$NO_COL"")""$has_untracked""$NO_COL"
else
set ret "$LP_COLOR_CHANGES""$branch""$NO_COL""(""$LP_COLOR_DIFF""$has_lines""$NO_COL"")""$has_untracked""$NO_COL" # changes to commit
end
end
echo -ne "$ret"" "
end
function _lp_svn_branch --description 'Auxiliary function for _lp_svn'
set -l root
set -l url
set -l result
begin
set -l LANG C
set -l LC_ALL C
svn info 2> /dev/null | sed 's/^URL: \(.*\)/"\1"/g;s/^Repository Root: \(.*\)/"\1"/p' | read url root
end
# echo "eval (LANG=C LC_ALL=C svn info 2>/dev/null | sed -n 's/^URL: \(.*\)/url=\"\1\"/p;s/^Repository Root: \(.*\)/root=\"\1\"/p'; echo $url $root" | bash | read url root
if [ -z "$root" ]
return
end
# Make url relative to root
set url (expr substr $url (expr length $root + 1) length $url)
if [ (expr "$url" : ".*/trunk.*") -ne 0 ]
echo -n trunk
else
set result (expr "$url" : '.*/branches/\([^/]*\)'; or expr "$url" : '/\([^/]*\)'; or basename "$root")
echo -n "$result"
end
end
function _lp_svn --description 'subversion'
set -l branch (_lp_svn_branch)
if [ -n $branch ]
set -l changes (svn status $LP_SVN_STATUS_OPTIONS | grep -c -v "?")
if [ "$changes" -eq 0 ]
echo "$LP_COLOR_UP""$branch""$NO_COL"" "
else
echo "$LP_COLOR_CHANGES""$branch""$NO_COL""(""$LP_COLOR_DIFF""$changes""$NO_COL"")"" "
end
end
end
function _lp_fossil_branch --description 'Auxiliary function for _lp_fossil'
set -l branch (fossil status 2>/dev/null | grep tags: | cut -c17-)
if [ -n "$branch" ]
echo "$branch"
else
fossil info > /dev/null; and echo "no-tag"
end
end
function _lp_fossil --description 'fossil'
set -l branch (_lp_fossil_branch)
if [ -n $branch ]
# Modified files (added or edited)
set -l C2E (fossil changes | wc -l)
# Deleted files
set -l C2D (fossil changes | grep DELETED | wc -l)
# Extras files
set -l C2A (fossil extras | wc -l)
set -l ret
set C2E (math "$C2E" - "$C2D")
if [ "$C2E" -gt 0 ]
set ret "$ret""+""$C2E"
end
if [ "$C2D" -gt 0 ]
if [ -z "$ret" ]
set ret "$ret""-""$C2D"
else
set ret "$ret""/-""$C2D"
end
end
if [ "$C2A" -gt 0 ]
set C2A "$LP_MARK_UNTRACKED"
else
set -e C2A
end
if [ -n "$ret" ]
set ret "(""$LP_COLOR_DIFF""$ret""$NO_COL"")"
end
if [ "$branch" = "no-tag" ]
# Warning, your branch has no tag name !
set branch "$LP_COLOR_COMMITS""$branch""$NO_COL""$ret""$LP_COLOR_COMMITS""$C2A""$NO_COL"
else
if [ "$C2E" -eq 0 -a "$C2D" -eq 0 ]
# All is up-to-date
set branch "$LP_COLOR_UP""$branch""$C2A""$NO_COL"
else
# There're some changes to commit
branch "$LP_COLOR_CHANGES""$branch""$NO_COL""$ret""$LP_COLOR_CHANGES""$C2A""$NO_COL"
end
end
echo -ne "$branch"" "
end
end
function _lp_bzr --description 'bazaar'
set -l output (bzr version-info --check-clean --custom --template='{branch_nick} {revno} {clean}' 2> /dev/null)
if [ "$status" -ne 0 ]
return
end
set -l branch $output[1]
set -l revno $output[2]
set -l clean $output[3]
set -l ret
if [ -n "$branch" ]
if [ "$clean" -eq 0 ]
set ret "$LP_COLOR_CHANGES""$branch""$NO_COL""(""$LP_COLOR_COMMITS""$revno""$NO_COL"")"
else
set ret "$LP_COLOR_UP""$branch""$NO_COL""(""$LP_COLOR_COMMITS""$revno""$NO_COL"")"
end
end
echo -ne "$ret"" "
end
## Actual init
# OS specific
_lp_os_detect
_lp_cpu_count
_lp_cpu_load_choose
if [ (expr "$TERM" : "screen.*") -ne 0 ]
set -g LP_BRACKET_OPEN "$LP_COLOR_IN_MULTIPLEXER""$LP_MARK_BRACKET_OPEN""$NO_COL"
set -g LP_BRACKET_CLOSE "$LP_COLOR_IN_MULTIPLEXER""$LP_MARK_BRACKET_CLOSE""$NO_COL"" "
else
set -g LP_BRACKET_OPEN "$LP_MARK_BRACKET_OPEN"
set -g LP_BRACKET_CLOSE "$LP_MARK_BRACKET_CLOSE"" "
end
# Misc.
_lp_user
_lp_hostname
_lp_jobs_hack
_lp_title_choose
# Once the initialization is done, these functions are of no use.
functions -e _lp_init_clean
functions -e _lp_os_detect
functions -e _lp_cpu_count
functions -e _lp_cpu_load_choose
functions -e _lp_user
functions -e _lp_hostname
functions -e _lp_jobs_hack
functions -e _lp_title_choose
end
function _lp_config --description 'Configure liquidprompt'
function _lp_default_config --description 'Default config'
set -g _LP_OPEN_ESC "\["
set -g _LP_CLOSE_ESC "\]"
set -l BOLD (set_color -o)
set -l BLACK (set_color black)
set -l BOLD_GRAY (set_color -o black)
set -l WHITE (set_color white)
set -l BOLD_WHITE (set_color -o white)
set -l RED (set_color red)
set -l BOLD_RED (set_color -o red)
set -l WARN_RED (set_color -b red)(set_color black)
set -l CRIT_RED (set_color -b red)(set_color white)
set -l DANGER_RED (set_color -b red)(set_color -o yellow)
set -l GREEN (set_color green)
set -l BOLD_GREEN (set_color -o green)
set -l YELLOW (set_color yellow)
set -l BOLD_YELLOW (set_color -o yellow)
set -l BLUE (set_color blue)
set -l BOLD_BLUE (set_color -o blue)
set -l PURPLE (set_color purple)
set -l PINK (set_color -o purple)
set -l CYAN (set_color cyan)
set -l BOLD_CYAN (set_color -o cyan)
# NO_COL is special: it will be used at runtime, not just during config loading
set -g NO_COL (set_color normal)
# Default values (globals)
set -e LP_PS1
set -g LP_PROMPT_PREFIX ""
set -g LP_PROMPT_POSTFIX ""
set -g LP_TITLE_OPEN "\e]0;"
set -g LP_TITLE_CLOSE "\a"
set -g LP_SCREEN_TITLE_OPEN "\033k"
set -g LP_SCREEN_TITLE_CLOSE "\033\134"
set -g LP_MARK_DEFAULT ""
set -g LP_MARK_DEFAULT "\$"
set -g LP_MARK_ROOT "#"
set -g LP_MARK_BATTERY "⌁"
set -g LP_MARK_ADAPTER "⏚"
set -g LP_MARK_LOAD "⌂"
set -g LP_MARK_TEMP "θ"
set -g LP_MARK_PROXY "↥"
set -g LP_MARK_HG "☿"
set -g LP_MARK_SVN "‡"
set -g LP_MARK_GIT "±"
set -g LP_MARK_FOSSIL "⌘"
set -g LP_MARK_BZR "⚯"
set -g LP_MARK_DISABLED "⌀"
set -g LP_MARK_UNTRACKED "*"
set -g LP_MARK_STASH "+"
set -g LP_MARK_BRACKET_OPEN "["
set -g LP_MARK_BRACKET_CLOSE "]"
set -g LP_MARK_SHORTEN_PATH " … "
set -g LP_COLOR_PATH "$BOLD"
set -g LP_COLOR_PATH_ROOT "$BOLD_YELLOW"
set -g LP_COLOR_PROXY "$BOLD_BLUE"
set -g LP_COLOR_JOB_D "$YELLOW"
set -g LP_COLOR_JOB_R "$YELLOW"
set -g LP_COLOR_JOB_Z "$YELLOW"
set -g LP_COLOR_ERR "$PURPLE"
set -g LP_COLOR_MARK "$BOLD"
set -g LP_COLOR_MARK_ROOT "$BOLD_RED"
set -g LP_COLOR_USER_LOGGED ""
set -g LP_COLOR_USER_ALT "$BOLD"
set -g LP_COLOR_USER_ROOT "$BOLD_YELLOW"
set -g LP_COLOR_HOST ""
set -g LP_COLOR_SSH "$BLUE"
set -g LP_COLOR_SU "$BOLD_YELLOW"
set -g LP_COLOR_TELNET "$WARN_RED"
set -g LP_COLOR_X11_ON "$GREEN"
set -g LP_COLOR_X11_OFF "$YELLOW"
set -g LP_COLOR_WRITE "$GREEN"
set -g LP_COLOR_NOWRITE "$RED"
set -g LP_COLOR_UP "$GREEN"
set -g LP_COLOR_COMMITS "$YELLOW"
set -g LP_COLOR_CHANGES "$RED"
set -g LP_COLOR_DIFF "$PURPLE"
set -g LP_COLOR_CHARGING_ABOVE "$GREEN"
set -g LP_COLOR_CHARGING_UNDER "$YELLOW"
set -g LP_COLOR_DISCHARGING_ABOVE "$YELLOW"
set -g LP_COLOR_DISCHARGING_UNDER "$RED"
set -g LP_COLOR_TIME "$BLUE"
set -g LP_COLOR_IN_MULTIPLEXER "$BOLD_BLUE"
set -g LP_COLORMAP_0 ""
set -g LP_COLORMAP_1 "$GREEN"
set -g LP_COLORMAP_2 "$BOLD_GREEN"
set -g LP_COLORMAP_3 "$YELLOW"
set -g LP_COLORMAP_4 "$BOLD_YELLOW"
set -g LP_COLORMAP_5 "$RED"
set -g LP_COLORMAP_6 "$BOLD_RED"
set -g LP_COLORMAP_7 "$WARN_RED"
set -g LP_COLORMAP_8 "$CRIT_RED"
set -g LP_COLORMAP_9 "$DANGER_RED"
set -e LP_TIME_ANALOG
end
function _lp_source_config --description 'Source the config file'
set -l configfile
if [ -f "/etc/liquidpromptrc.fish" ]
. "/etc/liquidpromptrc.fish"
end
if [ -f "$HOME/.liquidpromptrc.fish" ]
set configfile "$HOME/.liquidpromptrc.fish"
else if [ -z "$XDG_HOME_DIR" ]
set configfile "$HOME/.liquidpromptrc.fish"
else
set configfile "$XDG_HOME_DIR/liquidpromptrc.fish"
end
if [ -f "$configfile" ]
. "$configfile"
end
end
_lp_default_config
_lp_source_config
functions -e _lp_default_config
functions -e _lp_source_config
end
function _lp_checks -e lp_feature_option_changed --description 'Checks'
function _lp_check_features --description 'Check whether a tool is installed, and enable/disable it'
# Disable features if the tool is not installed.
[ (command -v git) ]; or set -e LP_ENABLE_GIT
[ (command -v tmux) ]; or set -e LP_ENABLE_TMUX
[ (command -v screen) ]; or set -e LP_ENABLE_SCREEN
[ (command -v svn) ]; or set -e LP_ENABLE_SVN
[ (command -v fossil) ]; or set -e LP_ENABLE_FOSSIL
[ (command -v hg) ]; or set -e LP_ENABLE_HG
[ (command -v bzr) ]; or set -e LP_ENABLE_BZR
[ (command -v acpi) ]; or set -e LP_ENABLE_BATT
[ (command -v sensors) ]; or set -e LP_ENABLE_SENSORS
end
function _lp_choose_time --description 'Choose the right _lp_time'
if functions -q _lp_time
functions -e _lp_time
end
function _lp_time_analog --description 'Display an approximate analog clock'
if not set -q LP_ENABLE_TIME
return
end
# Get the date as "hours(12) minutes" in a single call.
# (We are inserting a new line in between so that d is an array.)
set -l d (date "+%I%n%M")
# Separate hours and minutes.
set -l hour "$d[1]"
set -l min "$d[2]"
# The targeted unicode characters are the "CLOCK FACE" ones
# They are located in the codepages between:
# U+1F550 (ONE OCLOCK) and U+1F55B (TWELVE OCLOCK), for the plain hours.
# U+1F55C (ONE-THIRTY) and U+1F567 (TWELVE-THIRTY), for the thirties.
set -l plain "🕐" "🕑" "🕒" "🕓" "🕔" "🕕" "🕖" "🕗" "🕘" "🕙" "🕚" "🕛"
set -l half "🕜" "🕝" "🕞" "🕟" "🕠" "🕡" "🕢" "🕣" "🕤" "🕥" "🕦" "🕧"
if [ "$min" -lt 15 ]
echo -n "$LP_COLOR_TIME"$plain[$hour]"$NO_COL"" "
else if [ "$min" -lt 45 ]
echo -n "$LP_COLOR_TIME"$half[$hour]"$NO_COL"" "
else
echo -n "$LP_COLOR_TIME"$plain[(math "$hour % 12 +1")]"$NO_COL"" "
end
end
function _lp_time_digital --description 'Display the time'
if not set -q LP_ENABLE_TIME
return
end
set -l time (date "+%H%n%M%n%S")
set -l sep "$NO_COL"":"
echo "$LP_COLOR_TIME"$time[1]"$sep""$LP_COLOR_TIME"$time[2]"$sep""$LP_COLOR_TIME"$time[3]"$NO_COL"" "
end
# Choose the _lp_time function.
if set -q LP_TIME_ANALOG
functions -c _lp_time_analog _lp_time
else
functions -c _lp_time_digital _lp_time
end
functions -e _lp_time_analog
functions -e _lp_time_digital
end
function _lp_check_user --description 'Check whether the user name should be displayed'
set -q _LP_USER_IS_ROOT; or set -q LP_USER_IS_NOT_LOGIN; or set -q LP_USER_ALWAYS
if [ "$status" -eq 0 ]
# If one of the conditions above is true.
set -g LP_USER "$_LP_USER"
else
set -e LP_USER
end
end
function _lp_check_hostname --description 'Check whether the hostname should be displayed'
set -q LP_HOST_LOCAL; and not set -q LP_HOSTNAME_ALWAYS
if [ "$status" -eq 0 ]
set -e LP_HOST
else
set -g LP_HOST "$_LP_HOST"
end
end
function _lp_choose_title --description 'Choose the right fish_title'
functions -e fish_title
if not set -q LP_ENABLE_TITLE
functions -c _lp_title_default fish_title
else
[ (expr "$TERM" : "screen.*") -ne 0 ]; or set -q LP_ENABLE_SCREEN_TITLE
if [ "$status" -eq 0 ]
functions -c _lp_title fish_title
end
end
end
_lp_check_features
_lp_choose_time
_lp_check_user
_lp_check_hostname
_lp_choose_title
functions -e _lp_check_features
functions -e _lp_choose_time
functions -e _lp_choose_title
end
function _lp_directory -v PWD -e lp_dir_option_changed --description 'Check things on directory change'
# This function will be called when $PWD's value changes or when the
# _lp_dir_option_changed event is fired.
## Local functions
function _lp_are_vcs_disabled --description 'Check whether VCS are disabled for the PWD'
if not set -q LP_DISABLED_VCS_PATH