-
Notifications
You must be signed in to change notification settings - Fork 1
/
prompt_command.bash
1524 lines (1165 loc) · 42.8 KB
/
prompt_command.bash
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
# Copyright (c) 2023 Michael Federczuk
# SPDX-License-Identifier: MPL-2.0 AND Apache-2.0
# TODO: i really love this prompt, but having this all done by bash is starting to become noticeable in terms of speed
# (in preview mode, the prompt is shown pretty much instantly, meanwhile the non-preview prompt takes
# a few milliseconds to display)
# moving this all to an external program written in C or C++ would help very much, though
# the directory environment variables will probably be a bit more complicated to pull off, and i'd like to keep
# the config variables (e.g.: `DIR_ENV_VARS_ENABLED`, `PREVIEW_MODE_ENABLED`, ...) as non-environment variables,
# which means that we'd need to pass those values in from bash to the external program.
# something like this:
#
# eval "$(external_prompt_command bash init)"
#
# could set up the PROMPT_COMMAND variable exactly how it is needed:
#
# function ___external_prompt_command__bash__prompt_command() {
# local -ir prev_cmd_exc=$? || return;
#
# if ! command -v external_prompt_command > '/dev/null'; then
# printf '%s: external_prompt_command: program missing\n' "${FUNCNAME[0]}" >&2;
# return 27;
# fi
#
# local s || return;
# s="$(external_prompt_command bash prompt prev_cmd_exc=$prev_cmd_exc \
# dir_env_vars_enabled="${DIR_ENV_VARS_ENABLED-}" dir_env_vars="${DIR_ENV_VARS_FILENAME-}" \
# preview_mode_enabled="${PREVIEW_MODE_ENABLED-}" \
# dir_info_enabled="${DIR_INFO_ENABLED-}" dir_info_filename="${DIR_INFO_FILENAME-}" dir_info_line_prefix="${DIR_INFO_LINE_PREFIX-}")" || return;
# readonly s || return;
#
# eval "$s";
# }
#
# if declare -p PROMPT_COMMAND &> '/dev/null'; then
# if [[ ! "$(declare -p PROMPT_COMMAND)" =~ ^'declare -'([^'a']*'a'[^'a']*)+' ' ]]; then
# declare ___external_prompt_command__bash__tmp;
# ___external_prompt_command__bash__tmp="$PROMPT_COMMAND";
#
# unset -v PROMPT_COMMAND;
#
# declare -a PROMPT_COMMAND;
# PROMPT_COMMAND=("$___external_prompt_command__bash__tmp");
#
# unset -v ___external_prompt_command__bash__tmp;
# fi
# else
# declare -a PROMPT_COMMAND;
# PROMPT_COMMAND=();
# fi
#
# PROMPT_COMMAND+=(___external_prompt_command__bash__prompt_command);
#region helper functions
function __dotfiles_bash_funcs_prompt_command__escape_for_ps() {
local str || return
str="$1" || return
readonly str || return
local escaped_str || return
escaped_str="$str" || return
# shellcheck disable=1003
escaped_str="${escaped_str//'\'/'\\'}" || return
escaped_str="${escaped_str//'`'/'\`'}" || return
escaped_str="${escaped_str//'$'/'\\$'}" || return
escaped_str="${escaped_str//$'\n'/'\n'}" || return
printf '%s' "$escaped_str"
}
#region directory environment variables
declare DIR_ENV_VARS_ENABLED
DIR_ENV_VARS_ENABLED='yes'
declare DIR_ENV_VARS_FILENAME
DIR_ENV_VARS_FILENAME='.direnv'
function __dotfiles_bash_funcs_prompt_command__is_dir_env_vars_enabled() {
command -v is_truthy > '/dev/null' &&
is_truthy "${DIR_ENV_VARS_ENABLED-}"
}
function __dotfiles_bash_funcs_prompt_command__get_dir_env_vars_filename() {
if ! __dotfiles_bash_funcs_prompt_command__is_dir_env_vars_enabled; then
return 48
fi
local filename || return
filename="${DIR_ENV_VARS_FILENAME-}" || return
readonly filename || return
if [ -z "$filename" ] || [[ "$filename" =~ ('/'|^('.'|'..')$) ]]; then
return 49
fi
printf '%s' "$filename"
}
function __dotfiles_bash_funcs_prompt_command__is_dir_env_vars_file_ok() {
if ! __dotfiles_bash_funcs_prompt_command__is_dir_env_vars_enabled; then
return 32
fi
local dir_env_vars_filename || return
dir_env_vars_filename="$(__dotfiles_bash_funcs_prompt_command__get_dir_env_vars_filename && printf x)" || return
dir_env_vars_filename="${dir_env_vars_filename%x}" || return
readonly dir_env_vars_filename || return
test -e "$dir_env_vars_filename" &&
test -f "$dir_env_vars_filename" &&
test -r "$dir_env_vars_filename"
}
declare -A __dotfiles_bash_funcs_prompt_command__dir_env_var_map
__dotfiles_bash_funcs_prompt_command__dir_env_var_map=()
function __dotfiles_bash_funcs_prompt_command__unset_dir_env_vars_and_clear_dir_env_var_map() {
local var_name || return
for var_name in "${!__dotfiles_bash_funcs_prompt_command__dir_env_var_map[@]}"; do
local cached_var_value || return
cached_var_value="${__dotfiles_bash_funcs_prompt_command__dir_env_var_map["$var_name"]}" || return
if [ "${!var_name+"is_set"}" = 'is_set' ]; then
local real_var_value="${!var_name}" || return
if [ "$real_var_value" = "$cached_var_value" ]; then
unset -v "$var_name" || return
fi
unset -v real_var_value
fi
unset -v cached_var_value
done
__dotfiles_bash_funcs_prompt_command__dir_env_var_map=()
}
function __dotfiles_bash_funcs_prompt_command__update_dir_env_var_map() {
__dotfiles_bash_funcs_prompt_command__unset_dir_env_vars_and_clear_dir_env_var_map || return
if ! __dotfiles_bash_funcs_prompt_command__is_dir_env_vars_file_ok; then
return
fi
local dir_env_vars_filename || return
dir_env_vars_filename="$(__dotfiles_bash_funcs_prompt_command__get_dir_env_vars_filename && printf x)" || return
dir_env_vars_filename="${dir_env_vars_filename%x}" || return
readonly dir_env_vars_filename || return
local line || return
while read -r line; do
if [[ ! "$line" =~ ^([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]]; then
continue
fi
local var_name="${BASH_REMATCH[1]}" || return
if [ "${!var_name+"is_set"}" != 'is_set' ]; then
local var_value="${BASH_REMATCH[2]}" || return
__dotfiles_bash_funcs_prompt_command__dir_env_var_map+=(["$var_name"]="$var_value") || return
export "$var_name"="$var_value" || return
unset -v var_value
fi
unset -v var_name
done < "$dir_env_vars_filename"
}
#endregion
#region preview mode
declare PREVIEW_MODE_ENABLED
PREVIEW_MODE_ENABLED='no'
function __dotfiles_bash_funcs_prompt_command__is_preview_mode_enabled() {
command -v is_truthy > '/dev/null' &&
is_truthy "${PREVIEW_MODE_ENABLED-}"
}
#endregion
#region terminal title
function __dotfiles_bash_funcs_prompt_command__is_terminal_title_supported() {
[[ "${TERM-}" =~ ^('xterm'|'rxvt') ]]
}
function __dotfiles_bash_funcs_prompt_command__get_terminal_title() {
#region prefix
local term_title_prefix || return
term_title_prefix='' || return
if [ -n "${TERM_TITLE_PREFIX-}" ]; then
term_title_prefix="$(__dotfiles_bash_funcs_prompt_command__escape_for_ps "$TERM_TITLE_PREFIX: ")" || return
fi
readonly term_title_prefix || return
#endregion
#region infix
local term_title_infix || return
term_title_infix='' || return
if [ -n "${TERM_TITLE-}" ]; then
# TERM_TITLE won't be escaped
term_title_infix="$TERM_TITLE" || return
fi
if [ -z "$term_title_infix" ]; then
term_title_infix='\u@\H:\w' || return
fi
readonly term_title_infix || return
#endregion
printf '%s%s' "$term_title_prefix" "$term_title_infix"
}
#endregion
#region directory contents state
function __dotfiles_bash_funcs_prompt_command__get_cwd_hidden_entry_count() {
if ! command -v find > '/dev/null'; then
printf 0
return
fi
find . -mindepth 1 -maxdepth 1 -name '.*' -exec printf x \; | wc -c
}
function __dotfiles_bash_funcs_prompt_command__is_cwd_empty() {
test -z "$(find . -mindepth 1 -maxdepth 1 -exec printf x \;)"
}
function __dotfiles_bash_funcs_prompt_command__is_hidden_git_dir_present() {
local git_dir || return
git_dir="${GIT_DIR:-".git"}" || return
readonly git_dir || return
[[ ! "$git_dir" =~ ^('.'|'..')$ ]] &&
[[ "$git_dir" =~ ^'.' ]] &&
test -e "$git_dir" &&
{ ! command -v git > '/dev/null' || command git --no-pager status &> '/dev/null'; }
}
function __dotfiles_bash_funcs_prompt_command__is_hidden_dir_env_vars_file_ok() {
local dir_env_vars_filename || return
dir_env_vars_filename="$(__dotfiles_bash_funcs_prompt_command__get_dir_env_vars_filename && printf x)" || return
dir_env_vars_filename="${dir_env_vars_filename%x}" || return
readonly dir_env_vars_filename || return
[[ "$dir_env_vars_filename" =~ ^'.' ]] &&
__dotfiles_bash_funcs_prompt_command__is_dir_env_vars_file_ok
}
function __dotfiles_bash_funcs_prompt_command__get_dir_contents_state() {
local cwd || return
cwd="$(pwd -L && printf x)" || return
cwd="${cwd%$'\nx'}" || return
if [ ! -d "$cwd" ] || [ ! -x "$cwd" ] || [ ! -r "$cwd" ]; then
return
fi
unset -v cwd
local -i hidden_count || return
hidden_count=$(__dotfiles_bash_funcs_prompt_command__get_cwd_hidden_entry_count) || return
readonly hidden_count || return
if ((hidden_count == 0)); then
if __dotfiles_bash_funcs_prompt_command__is_cwd_empty; then
printf 'nothing'
fi
return
fi
if ((hidden_count == 1)); then
if __dotfiles_bash_funcs_prompt_command__is_hidden_git_dir_present; then
printf 'hidden_git_dir_only'
return
fi
if __dotfiles_bash_funcs_prompt_command__is_hidden_dir_env_vars_file_ok; then
printf 'hidden_dir_env_vars_file_only'
return
fi
fi
if __dotfiles_bash_funcs_prompt_command__is_hidden_dir_env_vars_file_ok; then
printf 'hidden_with_hidden_dir_env_vars_file'
return
fi
printf 'hidden'
}
#endregion
#region jobs
function __dotfiles_bash_funcs_prompt_command__get_job_count() {
jobs -pr | wc -l
}
#endregion
#region low storage space warning
#region math engine
# TODO: add support for `dc` (?) (not defined by POSIX btw)
# TODO: add support for `node`
#region checking bash long arithmetic support
declare __dotfiles_bash_funcs_prompt_command__math_engine__bash_long_arithmetic_supported
__dotfiles_bash_funcs_prompt_command__math_engine__bash_long_arithmetic_supported=false
if [ $((9223372036854775807)) = 9223372036854775807 ] && [ $((-9223372036854775808)) = '-9223372036854775808' ]; then
__dotfiles_bash_funcs_prompt_command__math_engine__bash_long_arithmetic_supported=true
fi
#endregion
#region engine names
function __dotfiles_bash_funcs_prompt_command__math_engine__get_available_integer_engine_names() {
local out || return
out='' || return
if command -v bc > '/dev/null'; then
out+='[bc]' || return
fi
# TODO: dc
if command -v python3 > '/dev/null'; then
out+='[python3]' || return
fi
if command -v python > '/dev/null'; then
out+='[python]' || return
fi
if command -v python2 > '/dev/null'; then
out+='[python2]' || return
fi
if $__dotfiles_bash_funcs_prompt_command__math_engine__bash_long_arithmetic_supported; then
out+='[bash]' || return
fi
if command -v awk > '/dev/null'; then
out+='[awk]' || return
fi
# TODO: node
if [[ "$out" != *'[bash]'* ]]; then
out+='[bash]' || return
fi
readonly out || return
printf '%s' "$out"
}
function __dotfiles_bash_funcs_prompt_command__math_engine__get_available_decimal_engine_names() {
local out || return
out='' || return
if command -v bc > '/dev/null'; then
out+='[bc]' || return
fi
# TODO: dc
if command -v python3 > '/dev/null'; then
out+='[python3]' || return
fi
if command -v python > '/dev/null'; then
out+='[python]' || return
fi
if command -v python2 > '/dev/null'; then
out+='[python2]' || return
fi
# TODO: node
if command -v awk > '/dev/null'; then
out+='[awk]' || return
fi
if [ -z "$out" ]; then
out+='[bash]' || return
fi
readonly out || return
printf '%s' "$out"
}
#endregion
#region operations
function __dotfiles_bash_funcs_prompt_command__math_engine__integer_multiply() {
local multiplier multiplicand || return
multiplier="$1" || return
multiplicand="$2" || return
readonly multiplicand multiplier || return
local engine_names || return
engine_names="$(__dotfiles_bash_funcs_prompt_command__math_engine__get_available_integer_engine_names)" || return
readonly engine_names || return
case "$engine_names" in
(*'[bc]'*)
# newline is needed here
printf '(%s) * (%s)\n' "$multiplier" "$multiplicand" | bc
return
;;
(*'[python3]'*)
python3 -c "print(int($multiplier) * int($multiplicand))"
return
;;
(*'[python]'*)
python -c "print(int($multiplier) * int($multiplicand))"
return
;;
('[python2]'*)
python2 -c "print(int($multiplier) * int($multiplicand))"
return
;;
(*'[bash]'*)
printf '%s' $((multiplier * multiplicand))
return
;;
(*'[awk]'*)
awk "BEGIN { print(($multiplier) * ($multiplicand)); }"
return
;;
(*)
printf '%s: no suitable integer math engine is available\n' "${FUNCNAME[0]}" >&2
return 48
;;
esac
}
function __dotfiles_bash_funcs_prompt_command__math_engine__decimal_multiply() {
local multiplier multiplicand || return
multiplier="$1" || return
multiplicand="$2" || return
readonly multiplicand multiplier || return
local engine_names || return
engine_names="$(__dotfiles_bash_funcs_prompt_command__math_engine__get_available_decimal_engine_names)" || return
readonly engine_names || return
case "$engine_names" in
(*'[bc]'*)
local product || return
# newline is needed here
product="$(printf '(%s) * (%s)\n' "$multiplier" "$multiplicand" | bc -l)" || return
if [[ "$product" =~ ^'.' ]]; then
product="0$product" || return
fi
readonly product || return
printf '%s' "$product"
return
;;
(*'[python3]'*)
python3 -c "print(float($multiplier) * float($multiplicand))"
return
;;
(*'[python]'*)
python -c "print(float($multiplier) * float($multiplicand))"
return
;;
(*'[python2]'*)
python2 -c "print(float($multiplier) * float($multiplicand))"
return
;;
(*'[awk]'*)
awk "BEGIN { print(($multiplier) * ($multiplicand)); }"
return
;;
(*'[bash]'*)
local multiplier_int multiplicand_int || return
multiplier_int="$(__dotfiles_bash_funcs_prompt_command__math_engine__round_to_integer "$multiplier")" || return
multiplicand_int="$(__dotfiles_bash_funcs_prompt_command__math_engine__round_to_integer "$multiplicand")" || return
readonly multiplicand_int multiplier_int || return
printf '%s' $((multiplicand_int * multiplier_int))
return
;;
(*)
printf '%s: no suitable decimal math engine is available\n' "${FUNCNAME[0]}" >&2
return 48
;;
esac
}
function __dotfiles_bash_funcs_prompt_command__math_engine__decimal_divide() {
local dividend divisor || return
dividend="$1" || return
divisor="$2" || return
readonly divisor dividend || return
local engine_names || return
engine_names="$(__dotfiles_bash_funcs_prompt_command__math_engine__get_available_decimal_engine_names)" || return
readonly engine_names || return
case "$engine_names" in
(*'[bc]'*)
local quotient || return
# newline is needed here
quotient="$(printf '(%s) / (%s)\n' "$dividend" "$divisor" | bc -l)" || return
if [[ "$quotient" =~ ^'.' ]]; then
quotient="0$quotient" || return
fi
readonly quotient || return
printf '%s' "$quotient"
return
;;
(*'[python3]'*)
python3 -c "print(float($dividend) / float($divisor))"
return
;;
(*'[python]'*)
python -c "print(float($dividend) / float($divisor))"
return
;;
(*'[python2]'*)
python2 -c "print(float($dividend) / float($divisor))"
return
;;
(*'[awk]'*)
awk "BEGIN { print(($multiplier) / ($multiplicand)); }"
return
;;
(*'[bash]'*)
local multiplier_int multiplicand_int || return
multiplier_int="$(__dotfiles_bash_funcs_prompt_command__math_engine__round_to_integer "$multiplier")" || return
multiplicand_int="$(__dotfiles_bash_funcs_prompt_command__math_engine__round_to_integer "$multiplicand")" || return
readonly multiplicand_int multiplier_int || return
printf '%s' $((multiplicand_int / multiplier_int))
return
;;
(*)
printf '%s: no suitable decimal math engine is available\n' "${FUNCNAME[0]}" >&2
return 48
;;
esac
}
function __dotfiles_bash_funcs_prompt_command__math_engine__integer_equals() {
local num1 num2 || return
num1="$1" || return
num2="$2" || return
readonly num2 num1 || return
local engine_names || return
engine_names="$(__dotfiles_bash_funcs_prompt_command__math_engine__get_available_integer_engine_names)" || return
readonly engine_names || return
case "$engine_names" in
(*'[bc]'*)
local result || return
# newline is needed here
result="$(printf '(%s) == (%s)\n' "$num1" "$num2" | bc)" || return
readonly result || return
if [ "$result" = '1' ]; then
return 0
else
return 32
fi
;;
(*'[python3]'*)
python3 -c "exit(0 if int($num1) == int($num2) else 32)"
return
;;
(*'[python]'*)
python -c "exit(0 if int($num1) == int($num2) else 32)"
return
;;
(*'[python2]'*)
python2 -c "exit(0 if int($num1) == int($num2) else 32)"
return
;;
(*'[bash]'*)
if ((num1 == num2)); then
return 0
else
return 32
fi
;;
(*'[awk]'*)
awk "BEGIN { exit((int($num1) == int($num2)) ? 0 : 32); }"
return
;;
(*)
printf '%s: no suitable integer math engine is available\n' "${FUNCNAME[0]}" >&2
return 48
;;
esac
}
function __dotfiles_bash_funcs_prompt_command__math_engine__decimal_greater_than() {
local num1 num2 || return
num1="$1" || return
num2="$2" || return
readonly num2 num1 || return
local engine_names || return
engine_names="$(__dotfiles_bash_funcs_prompt_command__math_engine__get_available_decimal_engine_names)" || return
readonly engine_names || return
case "$engine_names" in
(*'[bc]'*)
local result || return
# newline is needed here
result="$(printf '%s > %s\n' "$num1" "$num2" | bc)" || return
readonly result || return
if [ "$result" = '1' ]; then
return 0
else
return 32
fi
;;
(*'[python3]'*)
python3 -c "exit(0 if float($num1) > float($num2) else 32)"
return
;;
(*'[python]'*)
python -c "exit(0 if float($num1) > float($num2) else 32)"
return
;;
(*'[python2]'*)
python2 -c "exit(0 if float($num1) > float($num2) else 32)"
return
;;
(*'[awk]'*)
awk "BEGIN { exit(($num1 > $num2) ? 0 : 32); }"
return
;;
(*'[bash]'*)
local num1_int num2_int || return
num1_int=$(__dotfiles_bash_funcs_prompt_command__math_engine__round_to_integer "$num1") || return
num2_int=$(__dotfiles_bash_funcs_prompt_command__math_engine__round_to_integer "$num2") || return
readonly num2_int num1_int || return
if ((num1_int > num2_int)); then
return 0
else
return 32
fi
;;
(*)
printf '%s: no suitable decimal math engine is available\n' "${FUNCNAME[0]}" >&2
return 48
;;
esac
}
function __dotfiles_bash_funcs_prompt_command__math_engine__decimal_equal_or_greater_than() {
local num1 num2 || return
num1="$1" || return
num2="$2" || return
readonly num2 num1 || return
local engine_names || return
engine_names="$(__dotfiles_bash_funcs_prompt_command__math_engine__get_available_decimal_engine_names)" || return
readonly engine_names || return
case "$engine_names" in
(*'[bc]'*)
local result || return
# newline is needed here
result="$(printf '%s >= %s\n' "$num1" "$num2" | bc)" || return
readonly result || return
if [ "$result" = '1' ]; then
return 0
else
return 32
fi
;;
(*'[python3]'*)
python3 -c "exit(0 if float($num1) >= float($num2) else 32)"
return
;;
(*'[python]'*)
python -c "exit(0 if float($num1) >= float($num2) else 32)"
return
;;
(*'[python2]'*)
python2 -c "exit(0 if float($num1) >= float($num2) else 32)"
return
;;
(*'[awk]'*)
awk "BEGIN { exit(($num1 >= $num2) ? 0 : 32); }"
return
;;
(*'[bash]'*)
local num1_int num2_int || return
num1_int=$(__dotfiles_bash_funcs_prompt_command__math_engine__round_to_integer "$num1") || return
num2_int=$(__dotfiles_bash_funcs_prompt_command__math_engine__round_to_integer "$num2") || return
readonly num2_int num1_int || return
if ((num1_int >= num2_int)); then
return 0
else
return 32
fi
;;
(*)
printf '%s: no suitable decimal math engine is available\n' "${FUNCNAME[0]}" >&2
return 48
;;
esac
}
#endregion
#region round
function __dotfiles_bash_funcs_prompt_command__math_engine__round() {
local num scale || return
num="$1" || return
scale="$2" || return
readonly scale num || return
local engine_names || return
engine_names="$(__dotfiles_bash_funcs_prompt_command__math_engine__get_available_decimal_engine_names)" || return
readonly engine_names || return
case "$engine_names" in
(*'[python3]'*)
python3 -c "print(round(float($num), int($scale)))"
return
;;
(*'[python]'*)
python -c "print(round(float($num), int($scale)))"
return
;;
(*'[python2]'*)
python2 -c "print(round(float($num), int($scale)))"
return
;;
# TODO: other engines
(*)
printf '%s: no suitable decimal math engine is available\n' "${FUNCNAME[0]}" >&2
return 48
;;
esac
}
function __dotfiles_bash_funcs_prompt_command__math_engine__round_to_integer() {
local num || return
num="$1" || return
readonly num || return
local engine_names || return
engine_names="$(__dotfiles_bash_funcs_prompt_command__math_engine__get_available_decimal_engine_names)" || return
readonly engine_names || return
case "$engine_names" in
(*'[python3]'*)
python3 -c "print(int(round(float($num))))"
return
;;
(*'[python]'*)
python -c "print(int(round(float($num))))"
return
;;
(*'[python2]'*)
python2 -c "print(int(round(float($num))))"
return
;;
(*'[awk]'*)
awk "BEGIN { print(int($num)); }"
;;
# TODO: other engines
(*)
printf '%s: no suitable decimal math engine is available\n' "${FUNCNAME[0]}" >&2
return 48
;;
esac
}
#endregion
#endregion
function __dotfiles_bash_funcs_prompt_command__space_human_readable() {
local bytes || return
bytes="$1" || return
readonly bytes || return
local -a units || return
units=(
'B'
'KiB'
'MiB'
'GiB'
'TiB'
'PiB'
'EiB'
'ZiB'
'YiB'
) || return
readonly units || return
local value || return
value="$bytes" || return
local -i unit_index || return
unit_index=0 || return
while (((unit_index + 1) < ${#units[@]})) &&
__dotfiles_bash_funcs_prompt_command__math_engine__decimal_equal_or_greater_than "$value" 1024; do
value="$(__dotfiles_bash_funcs_prompt_command__math_engine__decimal_divide "$value" 1024)" || return
((++unit_index)) || return
done
value="$(__dotfiles_bash_funcs_prompt_command__math_engine__round_to_integer "$value")" || return
readonly unit_index value || return
printf '%s%s' "$value" "${units[unit_index]}"
}
function __dotfiles_bash_funcs_prompt_command__build_low_storage_space_warning_string() {
local total_bytes used_bytes avail_bytes || return
local df_line || return
df_line="$(POSIXLY_CORRECT=yes POSIX_ME_HARDER=yes command df -kP '.' | tail -n1)" || return
if [[ ! "$df_line" =~ [[:space:]]+(0|[1-9][0-9]*)[[:space:]]+(0|[1-9][0-9]*)[[:space:]]+((0|[1-9][0-9]*))[[:space:]]+((0|[1-9][0-9]*)%|-) ]]; then
return
fi
total_bytes="$(__dotfiles_bash_funcs_prompt_command__math_engine__integer_multiply "${BASH_REMATCH[1]}" 1024)" || return
used_bytes="$(__dotfiles_bash_funcs_prompt_command__math_engine__integer_multiply "${BASH_REMATCH[2]}" 1024)" || return
avail_bytes="$(__dotfiles_bash_funcs_prompt_command__math_engine__integer_multiply "${BASH_REMATCH[3]}" 1024)" || return
unset -v df_line
readonly avail_bytes used_bytes total_bytes || return
# on virtual filesystems (e.g.: '/proc') the total size will be 0, which means we can ignore it
if __dotfiles_bash_funcs_prompt_command__math_engine__integer_equals "$total_bytes" 0; then
return
fi
local usage_ratio || return
usage_ratio="$(__dotfiles_bash_funcs_prompt_command__math_engine__decimal_divide "$used_bytes" "$total_bytes")" || return
readonly usage_ratio || return
if ! __dotfiles_bash_funcs_prompt_command__math_engine__decimal_greater_than "$usage_ratio" 0.95; then
return
fi
local avail_human_readable || return
avail_human_readable="$(__dotfiles_bash_funcs_prompt_command__space_human_readable "$avail_bytes")" || return
readonly avail_human_readable || return
local total_human_readable || return
total_human_readable="$(__dotfiles_bash_funcs_prompt_command__space_human_readable "$total_bytes")" || return
readonly total_human_readable || return
local usage_percent || return
usage_percent="$(__dotfiles_bash_funcs_prompt_command__math_engine__decimal_multiply "$usage_ratio" 100)" || return
usage_percent="$(__dotfiles_bash_funcs_prompt_command__math_engine__round "$usage_percent" 2)" || return
readonly usage_percent || return
printf '!!! Available %s of %s (%s%%) !!!' "$avail_human_readable" "$total_human_readable" "$usage_percent"
}
#endregion
#region directory information
declare DIR_INFO_ENABLED
DIR_INFO_ENABLED='yes'
declare DIR_INFO_FILENAME
DIR_INFO_FILENAME='.dirinfo'
declare DIR_INFO_LINE_PREFIX
DIR_INFO_LINE_PREFIX='(i) '
function __dotfiles_bash_funcs_prompt_command__get_dir_info() {
if ! command -v is_truthy > '/dev/null' || ! is_truthy "${DIR_INFO_ENABLED-}"; then
return
fi
local filename || return
filename="${DIR_INFO_FILENAME-}" || return
readonly filename || return
if [ -z "$filename" ] || [[ "$filename" =~ ('/'|^('.'|'..')$) ]] ||
[ ! -e "$filename" ] || [ ! -f "$filename" ] || [ ! -r "$filename" ]; then
return
fi
local line_prefix || return
line_prefix="${DIR_INFO_LINE_PREFIX-}" || return
readonly line_prefix || return
local dirinfo || return
dirinfo='' || return
local line || return
# readling line-by-line to automatically trim whitespace
while read -rs line; do
if [ -z "$line" ]; then
continue
fi
if [ -n "$dirinfo" ]; then
dirinfo+=$'\n' || return
fi
dirinfo+="${line_prefix}${line}" || return
done < "$filename"
unset -v line
readonly dirinfo || return
printf '%s' "$dirinfo"
}
#endregion
#region current working directory