-
Notifications
You must be signed in to change notification settings - Fork 1
/
mystic.bash
2267 lines (1860 loc) · 60.2 KB
/
mystic.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
#===============================================================================
MYSTIC_VERSION="0.0.5i7"
#===============================================================================
#
# A responsive semantic bash prompt in the style of MysticBBS software.
#
# 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 3 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, see <https://www.gnu.org/licenses/>.
#
#===============================================================================
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
printf '%s\n' "MysticPrompt requires BASH 4.x+"
exit 1
fi
MYSTIC_BASH="$0"
MYSTIC_HOSTNAME="$(hostname -s)"
# This is necessary to avoid deforming the prompt when using up/down keys
shopt -s checkwinsize
#####
# Mystic Prompt Configuration
#####
# This is a mess.
# todo: clean up mess.
## System
# Render segments in parallel to speed up.
# Uses mktemp(1) temporary files. Best used
# with tmpfs or other in-memory fs to avoid
# unnecessary spam on your SSD.
MYSTIC_BGRENDER=false
## General display parameters
# Configure visual elements for each supported $TERM
MYSTIC_PLATFORM_NAME="$(uname -s)"
MYSTIC_TERM_SIGNATURE="${MYSTIC_PLATFORM_NAME}${LANG}${TERM}${COLORTERM}"
# MYSTIC_VTY_SIGNATURE_OPENBSD='OpenBSDvt220'
# MYSTIC_VTY_SIGNATURE_FREEBSD='FreeBSDC.UTF-8xterm'
# MYSTIC_VTY_SIGNATURE_LINUX='Linuxen_US.UTF-8linux'
MYSTIC_GLOBAL_TEXT_ATTR="bold"
#MYSTIC_GLOBAL_TEXT_ATTR=""
# Regardless of how many widgets are enabled,
# this is the minimum length of the top frame.
# Default: 23
# Consider:
#
# .-----[o^B]-[0OoL]-[oM.S]- -[Sun-30-02:30]-
# : 127 [root@samadhi] - [.../usr/local/sbin]# ls -la
#
# .--[0OoL]----------------- -[Sun-30-02:30]-
# : ...-issue-1987 3:0 idx.notup:4 tree.rm:2 untracked:7
# :. [root@samadhi] - [.../usr/local/sbin]# ls -la
#
MYSTIC_MIN_FRAME_LENGTH=23
# Max PWD length
MYSTIC_MAXLENGTH_PWD=27
# Max length of branch name to display
MYSTIC_MAXLENGTH_GIT=23
# Outshells
MYSTIC_GLYPH_RANGER='rg'
MYSTIC_GLYPH_MC='mc'
# Base Frame
MYSTIC_GLYPH_LB='['
MYSTIC_GLYPH_RB=']'
MYSTIC_GLYPH_AT='@'
MYSTIC_GLYPH_SLASH='/'
# git
MYSTIC_GLYPH_GIT_WHOSEP='.'
MYSTIC_GLYPH_GIT_NUMSEP=':'
# Input
MYSTIC_GLYPH_ROOT_INPUT='# '
# Select by known terminal signatures
# @TODO: Needs a lot of work yet
case ${MYSTIC_TERM_SIGNATURE} in
# Assume if it can do at least 256color then it
# can also do UTF-8. Same w/ $TRUECOLOR.
# Why?
# Because *2*0*2*1*, that's why.
# Because if you're not spooning with a DEC Alpha
# you have probably C-A-F7'd out of your framebuffer
# vty, nice though it is to reminisce, back into
# present times where we have 256color xterms and
# are sufficiently internationalized and inclusive
# for GNU/Linux to find itself distributed with
# UTF-8 enabled everywhere by default.
# My oh my what a time to be alive.
# I am a unicorn. Behold my glitter.
# *truecolor*)
# todo: ... sooon ...
# MYSTIC_COLOR_MODE=24BIT
# ;;
*256color*)
MYSTIC_COLOR_MODE=256
### Outshells
MYSTIC_COLOR_RANGER="${MYSTIC_GLOBAL_TEXT_ATTR} xt 550"
MYSTIC_COLOR_MC="${MYSTIC_GLOBAL_TEXT_ATTR} xt 550"
#
### Base Frame
MYSTIC_GLYPH_ELLIPSIS='…'
MYSTIC_GLYPH_LINE_H='─'
MYSTIC_GLYPH_CORNER_TL='┌'
MYSTIC_GLYPH_LINE_V='│'
MYSTIC_GLYPH_CORNER_BL='└'
MYSTIC_COLOR_PUNCTUATION="${MYSTIC_GLOBAL_TEXT_ATTR} xt 222"
#
### Graphing
MYSTIC_GRAPH_GLYPHS='▁▂▂▃▃▄▅▆▇█'
#
# Battery graph
MYSTIC_GLYPH_BATTERY='ᵇ'
MYSTIC_GLYPH_SPACER='·'
MYSTIC_GLYPH_CHARGING='↑'
MYSTIC_GLYPH_DISCHARGING='↓'
MYSTIC_GLYPH_FULL='☻'
MYSTIC_GLYPH_UNKNOWN='‽'
MYSTIC_GRADIENT_BATTERY_CODE="BCA"
declare -a MYSTIC_GRADIENT_BATTERY
#MYSTIC_GRADIENT_BATTERY_CODE="AAB"
MYSTIC_COLOR_CHARGING="${MYSTIC_GLOBAL_TEXT_ATTR} xt 055"
MYSTIC_COLOR_DISCHARGING="${MYSTIC_GLOBAL_TEXT_ATTR} xt 033"
MYSTIC_COLOR_GRAPH_LABEL="${MYSTIC_GLOBAL_TEXT_ATTR} xt 222"
#
# Load graph
MYSTIC_GLYPH_LOAD='ˡ'
declare -a MYSTIC_GRADIENT_LOAD
MYSTIC_GRADIENT_LOAD_CODE="ABC"
#
# Memory graph
MYSTIC_GLYPH_MEMORY='ᵐ'
MYSTIC_GLYPH_SWAP='ˢ'
declare -a MYSTIC_GRADIENT_MEMORY
MYSTIC_GRADIENT_MEMORY_CODE="ABC"
declare -a MYSTIC_GRADIENT_SWAP
MYSTIC_GRADIENT_SWAP_CODE="ACB"
#
### Clock
MYSTIC_GLYPH_TIMESEP=':'
MYSTIC_COLOR_DAYNAME="${MYSTIC_GLOBAL_TEXT_ATTR} xt 115"
MYSTIC_COLOR_DATE="${MYSTIC_GLOBAL_TEXT_ATTR} xt 005"
MYSTIC_COLOR_TIME="${MYSTIC_GLOBAL_TEXT_ATTR} xt 115"
#
### Git
MYSTIC_COLOR_GITBRANCH_TEXT="${MYSTIC_GLOBAL_TEXT_ATTR} xt 033"
MYSTIC_COLOR_GITBRANCH_CAPS="${MYSTIC_GLOBAL_TEXT_ATTR} xt 044"
MYSTIC_COLOR_GITBRANCH_NUM="${MYSTIC_GLOBAL_TEXT_ATTR} xt 055"
MYSTIC_COLOR_GITSTATUS_NUM="${MYSTIC_GLOBAL_TEXT_ATTR} xt 550"
MYSTIC_COLOR_GITSTATUS_WHO="${MYSTIC_GLOBAL_TEXT_ATTR} xt 505"
MYSTIC_COLOR_GITSTATUS_WHAT="${MYSTIC_GLOBAL_TEXT_ATTR} xt 303"
#
### Exit Code
MYSTIC_COLOR_OK_HIGH="${MYSTIC_GLOBAL_TEXT_ATTR} xt 555"
MYSTIC_COLOR_OK_MID="${MYSTIC_GLOBAL_TEXT_ATTR} xt 005"
MYSTIC_COLOR_OK_LOW="${MYSTIC_GLOBAL_TEXT_ATTR} xt 003"
MYSTIC_COLOR_ERROR_HIGH="${MYSTIC_GLOBAL_TEXT_ATTR} xt 555"
MYSTIC_COLOR_ERROR_MID="${MYSTIC_GLOBAL_TEXT_ATTR} xt 500"
MYSTIC_COLOR_ERROR_LOW="${MYSTIC_GLOBAL_TEXT_ATTR} xt 300"
MYSTIC_COLOR_EXITCODE="${MYSTIC_GLOBAL_TEXT_ATTR} xt 555"
#
### PWD
MYSTIC_COLOR_PATH_PWD="${MYSTIC_GLOBAL_TEXT_ATTR} xt 050"
MYSTIC_COLOR_PATH_PATH="${MYSTIC_GLOBAL_TEXT_ATTR} xt 030"
#
### User@Host & Input Pip
MYSTIC_COLOR_HOSTNAME="${MYSTIC_GLOBAL_TEXT_ATTR} xt 303"
MYSTIC_COLOR_ROOT_USERNAME="bxt 500 ${MYSTIC_GLOBAL_TEXT_ATTR} xt 550"
MYSTIC_COLOR_ROOT_INPUT="${MYSTIC_GLOBAL_TEXT_ATTR} xt 500"
MYSTIC_COLOR_USERNAME="${MYSTIC_GLOBAL_TEXT_ATTR} xt 505"
MYSTIC_COLOR_INPUT="${MYSTIC_GLOBAL_TEXT_ATTR} xt 005"
MYSTIC_GLYPH_INPUT='∙ '
#
;;
# Default to 16 color, basic charset
*)
MYSTIC_COLOR_MODE=16
MYSTIC_COLOR_RANGER="${MYSTIC_GLOBAL_TEXT_ATTR} bright yellow"
MYSTIC_COLOR_MC="${MYSTIC_GLOBAL_TEXT_ATTR} bright yellow"
MYSTIC_GLYPH_SPACER='-'
MYSTIC_GLYPH_CHARGING='^'
MYSTIC_GLYPH_DISCHARGING='.'
MYSTIC_GLYPH_FULL='*'
MYSTIC_GLYPH_UNKNOWN='?'
MYSTIC_GLYPH_ELLIPSIS='...'
MYSTIC_GLYPH_LINE_H='-'
MYSTIC_GLYPH_CORNER_TL='.'
MYSTIC_GLYPH_LINE_V=':'
MYSTIC_GLYPH_CORNER_BL='.'
MYSTIC_GLYPH_TIMESEP=':'
MYSTIC_GLYPH_MEMORY='m'
MYSTIC_GLYPH_SWAP='s'
MYSTIC_GLYPH_LOAD='l'
MYSTIC_GLYPH_BATTERY='b'
MYSTIC_GRAPH_GLYPHS='..oooOOO00'
MYSTIC_GRADIENT_COLORS=("${MYSTIC_GLOBAL_TEXT_ATTR} green" \
"${MYSTIC_GLOBAL_TEXT_ATTR} bright yellow" \
"${MYSTIC_GLOBAL_TEXT_ATTR} bright purple" \
"${MYSTIC_GLOBAL_TEXT_ATTR} bright red")
MYSTIC_GRADIENT_BATTERY_CODE='3322211100'
MYSTIC_GRADIENT_MEMORY_CODE='0011122233'
MYSTIC_GRADIENT_SWAP_CODE='0011122233'
MYSTIC_GRADIENT_LOAD_CODE='0011122233'
MYSTIC_COLOR_PUNCTUATION="${MYSTIC_GLOBAL_TEXT_ATTR} bright black"
MYSTIC_COLOR_FRAME=''
MYSTIC_COLOR_CHARGING="${MYSTIC_GLOBAL_TEXT_ATTR} bright cyan"
MYSTIC_COLOR_DISCHARGING="${MYSTIC_GLOBAL_TEXT_ATTR} cyan"
MYSTIC_COLOR_GRAPH_LABEL="${MYSTIC_GLOBAL_TEXT_ATTR} bright black"
MYSTIC_COLOR_DAYNAME="${MYSTIC_GLOBAL_TEXT_ATTR} bright blue"
MYSTIC_COLOR_DATE="${MYSTIC_GLOBAL_TEXT_ATTR} blue"
MYSTIC_COLOR_TIME="${MYSTIC_GLOBAL_TEXT_ATTR} bright blue"
MYSTIC_COLOR_OK_HIGH="${MYSTIC_GLOBAL_TEXT_ATTR} bright white"
MYSTIC_COLOR_OK_MID="${MYSTIC_GLOBAL_TEXT_ATTR} bright blue"
MYSTIC_COLOR_OK_LOW="${MYSTIC_GLOBAL_TEXT_ATTR} blue"
MYSTIC_COLOR_ERROR_HIGH="${MYSTIC_GLOBAL_TEXT_ATTR} bright white"
MYSTIC_COLOR_ERROR_MID="${MYSTIC_GLOBAL_TEXT_ATTR} bright red"
MYSTIC_COLOR_ERROR_LOW="${MYSTIC_GLOBAL_TEXT_ATTR} red"
MYSTIC_COLOR_EXITCODE="${MYSTIC_GLOBAL_TEXT_ATTR} bright red"
MYSTIC_COLOR_GITBRANCH_TEXT="${MYSTIC_GLOBAL_TEXT_ATTR} cyan"
MYSTIC_COLOR_GITBRANCH_CAPS="${MYSTIC_GLOBAL_TEXT_ATTR} bright cyan"
MYSTIC_COLOR_GITBRANCH_NUM="${MYSTIC_GLOBAL_TEXT_ATTR} bright white"
MYSTIC_COLOR_GITSTATUS_NUM="${MYSTIC_GLOBAL_TEXT_ATTR} bright yellow"
MYSTIC_COLOR_GITSTATUS_WHO="${MYSTIC_GLOBAL_TEXT_ATTR} bright purple"
MYSTIC_COLOR_GITSTATUS_WHAT="${MYSTIC_GLOBAL_TEXT_ATTR} purple"
MYSTIC_COLOR_HOSTNAME="${MYSTIC_GLOBAL_TEXT_ATTR} purple"
MYSTIC_COLOR_PATH_PWD="${MYSTIC_GLOBAL_TEXT_ATTR} bright green"
MYSTIC_COLOR_PATH_PATH="${MYSTIC_GLOBAL_TEXT_ATTR} green"
MYSTIC_COLOR_ROOT_USERNAME="${MYSTIC_GLOBAL_TEXT_ATTR} bred bright yellow"
MYSTIC_COLOR_ROOT_INPUT="${MYSTIC_GLOBAL_TEXT_ATTR} bright red"
MYSTIC_GLYPH_ROOT_INPUT='# '
MYSTIC_COLOR_USERNAME="${MYSTIC_GLOBAL_TEXT_ATTR} bright purple"
MYSTIC_COLOR_INPUT="${MYSTIC_GLOBAL_TEXT_ATTR} bright blue"
MYSTIC_GLYPH_INPUT='$ '
;;
esac
##
#####
#
# Mystic TermLib v1.0.0
# A fast, light, and fluenty bash library for using terminal escape sequences.
#
# 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 3 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, see <https://www.gnu.org/licenses/>.
#####
#####
# This is a blend of VTx, ANSI, and non-standard escape sequences for controlling terminal
# colors, cursor movement, xterm window headers, and more. It is set up to be used with bash's
# PROMPT_COMMAND= environment variable (see MYSTIC_PC_START and _STOP), meaning it encapsulates
# escape sequences in bytes which prevent bash from counting escape sequence bytes as readable
# characters for the purposes of knowing a prompt's line length, which prevents issues with
# line editing, for example backspacing over the prompt.
#
# It is assumed these are generally available for any "modern" terminals,
# including some tty devices. If your daily-driver terminal chokes on these,
# please file a github issue.
#
# This library likely has bugs and is incomplete. Your PR can help improve it.
#
# References:
# https://en.wikipedia.org/wiki/ANSI_escape_code
#
#####
# Prefix and suffix for PROMPT_COMMAND
MYSTIC_PC_START='\x01'
MYSTIC_PC_STOP='\x02'
# You can clear these if not used in PROMPT_COMMAND
#MYSTIC_PC_START=
#MYSTIC_PC_STOP=
# Terminal Control Codes
MYSTIC_TC_ESC="\x1b"
MYSTIC_TC_BELL="\a"
MYSTIC_TC_NEWLINE="\n"
MYSTIC_TC_BACKSPACE="\b"
MYSTIC_TC_HTAB="\t"
MYSTIC_TC_VTAB="\v"
MYSTIC_TC_FORMFEED="\f"
MYSTIC_TC_RETURN="\r"
MYSTIC_TC_CLEAR_AFTER="${MYSTIC_TC_ESC}[0K"
MYSTIC_TC_CLEAR_BEFORE="${MYSTIC_TC_ESC}[1K"
MYSTIC_TC_CLEAR_LINE="${MYSTIC_TC_ESC}[2K"
MYSTIC_TC_SCREEN_SAVE="${MYSTIC_TC_ESC}[?47h"
MYSTIC_TC_SCREEN_LOAD="${MYSTIC_TC_ESC}[?47l"
MYSTIC_TC_SCREEN_CLEAR="${MYSTIC_TC_ESC}[2J"
# Terminal Attribute Codes
MYSTIC_AT_BOLD="${MYSTIC_TC_ESC}[1m"
MYSTIC_AT_DIM="${MYSTIC_TC_ESC}[2m"
MYSTIC_AT_UNDERLINE="${MYSTIC_TC_ESC}[4m"
MYSTIC_AT_BLINK="${MYSTIC_TC_ESC}[5m"
MYSTIC_AT_RESET="${MYSTIC_TC_ESC}[0m"
## Color stuff
MYSTIC_COLOR_PREFIX="${MYSTIC_PC_START}${MYSTIC_TC_ESC}["
MYSTIC_COLOR_SUFFIX="m${MYSTIC_PC_STOP}"
# ANSI 16-color Forground
MYSTIC_FG_BLACK=30
MYSTIC_FG_RED=31
MYSTIC_FG_GREEN=32
MYSTIC_FG_YELLOW=33
MYSTIC_FG_BLUE=34
MYSTIC_FG_MAGENTA=35
MYSTIC_FG_CYAN=36
MYSTIC_FG_WHITE=37
# ANSI 16-color Background
MYSTIC_BG_BLACK=40
MYSTIC_BG_RED=41
MYSTIC_BG_GREEN=42
MYSTIC_BG_YELLOW=43
MYSTIC_BG_BLUE=44
MYSTIC_BG_MAGENTA=45
MYSTIC_BG_CYAN=46
MYSTIC_BG_WHITE=47
# =============================================================================
# Set the XTerm titlebar text
#
# Arguments:
# str "text" puts provided text into titlebar
#
# =============================================================================
function mystic_xterm_titlebar() {
printf '\033]0;%s\007' "${*}"
}
# =============================================================================
# Attempt to sweet talk terminal into supporting unicode
# =============================================================================
function mystic_setup_unicode() {
# Available on some Linux
if hash unicode_start 2>/dev/null; then
unicode_start
else
# Limited fallback, does not work well with *BSD
printf '\033%%G'
fi
}
# =============================================================================
# Rings the terminal bell (maybe)
#
# Effects vary from blinking and beeping to
# playing audio files and doing nothing.
#
# This is broken in many terminals, but maybe it'll work for you.
# =============================================================================
mystic_bell() {
printf '%s' "${MYSTIC_TC_BELL}"
}
# =============================================================================
# Tells the terminal to report its rows and cols into variables.
# Should be implied by 'shopt -s checkwinsize' but it's here in case.
# =============================================================================
mystic_update_termsize() {
MYSTIC_ROWS="$(tput lines)"
MYSTIC_COLS="$(tput cols)"
export MYSTIC_ROWS MYSTIC_COLS
}
# =============================================================================
# Prints a string one or more times contiguously
#
# Arguments:
# int count repeat count
# str "text" text to repeat
#
# Examples:
#
# mystic_fill 10 '#'
# ##########
#
# mystic_fill 5 '-='
# -=-=-=-=-=
#
# =============================================================================
function mystic_fill() {
# shellcheck disable=SC2183
printf -v fill '%*s' "${1}"
printf '%s' "${fill// /${2}}"
}
# =============================================================================
# Moves the cursor around, handles tabs and line clearing, and saves to and
# loads from the terminal buffer.
#
# Arguments:
# A list of commands as positional arguments.
#
# Usage:
# mystic_cursor <command(s)>
# mcursor <command(s)>
#
# =============================================================================
mystic_cursor() {
local _print
local _pos _row _col
local _nl
_nl="${MYSTIC_TC_NEWLINE}"
while (($# > 0)); do
case "$1" in
up)
# Notes about up, down, right, left, and move/home:
#
# If an integer argument is provided to the command, then its value will be used.
# Otherwise, the cursor will move one character in the direction indicated by
# the command.
#
# In the case of move/home, if integer argument is not provided then the
# cursor will move to position 1,1, which is the top-left corner of the terminal.
_pos=1
[[ $2 == ?(-)+([0-9]) ]] && _pos=$2 && shift
_print+="${MYSTIC_PC_START}${MYSTIC_TC_ESC}[${_pos}A${MYSTIC_PC_STOP}"
;;
down | dn)
_pos=1
[[ $2 == ?(-)+([0-9]) ]] && _pos=$2 && shift
_print+="${MYSTIC_PC_START}${MYSTIC_TC_ESC}[${_pos}B${MYSTIC_PC_STOP}"
;;
right | rt)
_pos=1
[[ $2 == ?(-)+([0-9]) ]] && _pos=$2 && shift
_print+="${MYSTIC_PC_START}${MYSTIC_TC_ESC}[${_pos}C${MYSTIC_PC_STOP}"
;;
left | lt)
_pos=1
[[ $2 == ?(-)+([0-9]) ]] && _pos=$2 && shift
_print+="${MYSTIC_PC_START}${MYSTIC_TC_ESC}[${_pos}D${MYSTIC_PC_STOP}"
;;
move | home)
_row=1
_col=1
[[ $2 == ?(-)+([0-9]) ]] && _row=$2 && shift
[[ $2 == ?(-)+([0-9]) ]] && _col=$2 && shift
_print+="${MYSTIC_PC_START}${MYSTIC_TC_ESC}[${_row:-1};${_col:-1}H${MYSTIC_PC_STOP}"
;;
newline | nl | linefeed | lf)
_print+="${MYSTIC_TC_NEWLINE}"
;;
backspace | bs)
_print+="${MYSTIC_TC_BACKSPACE}"
;;
htab)
_print+="${MYSTIC_TC_HTAB}"
;;
vtab)
_print+="${MYSTIC_TC_VTAB}"
;;
formfeed)
_print+="${MYSTIC_TC_FORMFEED}"
;;
return | cr)
_print+="${MYSTIC_TC_RETURN}"
;;
save)
# Notes about save and load:
#
# The terminal has a second buffer into which bytes can be written and
# read. Save and load write the terminal contents into the buffer and
# restore it to the terminal accordingly.
#
# If you have exited from `less`, `vim`, or any other terminal app which
# returns the terminal contents as they were before the app was run,
# you will have seen the behavior of save and load.
_print+="${MYSTIC_PC_START}${MYSTIC_TC_SCREEN_SAVE}${MYSTIC_PC_STOP}"
;;
load)
_print+="${MYSTIC_PC_START}${MYSTIC_TC_SCREEN_LOAD}${MYSTIC_PC_STOP}"
;;
clearline)
_print+="${MYSTIC_PC_START}${MYSTIC_TC_CLEAR_LINE}${MYSTIC_PC_STOP}"
;;
clearbefore)
_print+="${MYSTIC_PC_START}${MYSTIC_TC_CLEAR_BEFORE}${MYSTIC_PC_STOP}"
;;
clearafter)
_print+="${MYSTIC_PC_START}${MYSTIC_TC_CLEAR_AFTER}${MYSTIC_PC_STOP}"
;;
clearscreen | clear | cls)
_print+="${MYSTIC_PC_START}${MYSTIC_TC_SCREEN_CLEAR}${MYSTIC_PC_STOP}"
;;
space | spc)
_print+='\x20'
;;
*)
_print+="${1}"
;;
esac
shift
done
_print+="${MYSTIC_PC_START}${MYSTIC_AT_RESET}${MYSTIC_PC_STOP}"
printf "%b" "${_print}"
}
mcursor() { mystic_cursor "${@}"; }
# =============================================================================
# Throws bits approximating valid, encapsulated, color escape sequences at
# the terminal and then yolos out.
#
# Arguments:
# A list of commands as positional arguments.
#
# Usage:
# mystic_echo <command(s)>
#
# WARNING:
# Built for speed, not safety. Does not check for valid or clean input.
#
# Terminal emulators are, at their core, a writhing mass of ancient serpents
# and a chaos of compromises. Bash quoting, string manipulation, and expansion
# are.. imperfect. You get -i and -a types, and those are hints.
#
# What I'm saying is: There are inescapable holes in this cheese, Bobby.
# ...and we're going in.
#
# =============================================================================
mystic_echo() {
local _print
local _r _b _g
local _color_offset_fg _color_offset_bg
local _nl
_nl="${MYSTIC_TC_NEWLINE}"
while (($# > 0)); do
case "$1" in
# Basic text control
bold)
_print+="${MYSTIC_PC_START}${MYSTIC_AT_BOLD}${MYSTIC_PC_STOP}"
;;
unbold)
_print+="${MYSTIC_PC_START}${MYSTIC_AT_DIM}${MYSTIC_PC_STOP}"
;;
underline | line)
_print+="${MYSTIC_PC_START}${MYSTIC_AT_UNDERLINE}${MYSTIC_PC_STOP}"
;;
text)
# Quote "your text" to avoid splitting
shift
_print+="${1}"
;;
no-newline | nn | nnl)
_nl=''
;;
newline | nl | linefeed | lf)
_print+="${MYSTIC_TC_NEWLINE}"
;;
space | spc)
# Sometimes you need an explicit space
_print+='\x20'
;;
# ANSI 16-color
bright)
_color_offset_fg=60
;;
bbright)
_color_offset_bg=60
;;
dim)
_color_offset_fg=0
;;
bdim)
_color_offset_bg=0
;;
blink | flash)
_print+="${MYSTIC_PC_START}${MYSTIC_AT_BLINK}${MYSTIC_PC_STOP}"
;;
reset)
_print+="${MYSTIC_PC_START}${MYSTIC_AT_RESET}${MYSTIC_PC_STOP}"
_color_offset_fg=0
_color_offset_bg=0
;;
black | bk)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_FG_BLACK + _color_offset_fg))${MYSTIC_COLOR_SUFFIX}"
;;
gray | grey)
# This is a shortcut to "bright black".
# If bright was specified, this outputs dim white.
case "${_color_offset_fg}" in
60)
_print+="${MYSTIC_COLOR_PREFIX}${MYSTIC_FG_WHITE}${MYSTIC_COLOR_SUFFIX}"
;;
*)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_FG_BLACK + 60))${MYSTIC_COLOR_SUFFIX}"
;;
esac
;;
red | rd)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_FG_RED + _color_offset_fg))${MYSTIC_COLOR_SUFFIX}"
;;
green | gn)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_FG_GREEN + _color_offset_fg))${MYSTIC_COLOR_SUFFIX}"
;;
yellow | yl)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_FG_YELLOW + _color_offset_fg))${MYSTIC_COLOR_SUFFIX}"
;;
blue | bl)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_FG_BLUE + _color_offset_fg))${MYSTIC_COLOR_SUFFIX}"
;;
magenta | pink | mg)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_FG_MAGENTA + _color_offset_fg))${MYSTIC_COLOR_SUFFIX}"
;;
cyan | cn)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_FG_CYAN + _color_offset_fg))${MYSTIC_COLOR_SUFFIX}"
;;
white | wt)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_FG_WHITE + _color_offset_fg))${MYSTIC_COLOR_SUFFIX}"
;;
bblack | bbk)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_BG_BLACK + _color_offset_bg))${MYSTIC_COLOR_SUFFIX}"
;;
bred | brd)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_BG_RED + _color_offset_bg))${MYSTIC_COLOR_SUFFIX}"
;;
bgreen | bgn)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_BG_GREEN + _color_offset_bg))${MYSTIC_COLOR_SUFFIX}"
;;
byellow | byl)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_BG_YELLOW + _color_offset_bg))${MYSTIC_COLOR_SUFFIX}"
;;
bblue | bbl)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_BG_BLUE + _color_offset_bg))${MYSTIC_COLOR_SUFFIX}"
;;
bmagenta | bpink | bmg)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_BG_MAGENTA + _color_offset_bg))${MYSTIC_COLOR_SUFFIX}"
;;
bcyan | bcn)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_BG_CYAN + _color_offset_bg))${MYSTIC_COLOR_SUFFIX}"
;;
bwhite | bwt)
_print+="${MYSTIC_COLOR_PREFIX}$((MYSTIC_BG_WHITE + _color_offset_bg))${MYSTIC_COLOR_SUFFIX}"
;;
# Xterm's weird indexed 8-bit color: \033[38;255m
#
# Usage:
# xterm <000-255>
# xterm 255 (white)
#
# Reference:
# https://en.wikipedia.org/wiki/File:Xterm_256color_chart.svg
xterm | xt)
_print+="${MYSTIC_COLOR_PREFIX}38;5;$((16 + ${2:0:1} * 36 + ${2:1:1} * 6 + ${2:2:1}))${MYSTIC_COLOR_SUFFIX}"
shift
;;
bxterm | bxt)
_print+="${MYSTIC_COLOR_PREFIX}48;5;$((16 + ${2:0:1} * 36 + ${2:1:1} * 6 + ${2:2:1}))${MYSTIC_COLOR_SUFFIX}"
shift
;;
# 24-bit RGB does it like this: \033[38;R;G;B;128m
#
# Usage:
# rgb <red> <green> <blue>
# rgb 000 128 255
# rgb 00 80 FF
# you can use hex or dec because I love you.
# num args <3 are interpreted as hex (use 00-FF)
# num args >2 are interpreted as dec (use 000-255)
# values not in [0-9a-fA-F] may summon death
# values 0x00 (000) < or > 0xff (255) may summon dragons
# explanation: values outside of the above ranges have no standardized function
# and your terminal emulator may/not handle it or handle it poorly. Might be
# a playground for security researchers.
rgb)
if [[ ${#2} -lt 3 ]]; then
_r=$((0x${2})) && shift
elif [[ ${#2} -gt 2 ]]; then
_r=$2 && shift
else
_r=255
fi
if [[ ${#2} -lt 3 ]]; then
_g=$((0x${2})) && shift
elif [[ ${#2} -gt 2 ]]; then
_g=$2 && shift
else
_g=255
fi
if [[ ${#2} -lt 3 ]]; then
_b=$((0x${2})) && shift
elif [[ ${#2} -gt 2 ]]; then
_b=$2 && shift
else
_b=255
fi
_print+="${MYSTIC_COLOR_PREFIX}38;2;${_r:-255};${_g:-255};${_b:-255}${MYSTIC_COLOR_SUFFIX}"
;;
brgb)
if [[ ${#2} -lt 3 ]]; then
_r=$((0x${2})) && shift
elif [[ ${#2} -gt 2 ]]; then
_r=$2 && shift
else
_r=255
fi
if [[ ${#2} -lt 3 ]]; then
_g=$((0x${2})) && shift
elif [[ ${#2} -gt 2 ]]; then
_g=$2 && shift
else
_g=255
fi
if [[ ${#2} -lt 3 ]]; then
_b=$((0x${2})) && shift
elif [[ ${#2} -gt 2 ]]; then
_b=$2 && shift
else
_b=255
fi
_print+="${MYSTIC_COLOR_PREFIX}48;2;${_r:-255};${_g:-255};${_b:-255}${MYSTIC_COLOR_SUFFIX}"
;;
# Just print anything that doesn't match a command.
# Behaves like a more dangerous variant of the 'text' command.
# Quoting works and is recommended.
*)
_print+="${1}"
;;
esac
shift
done
_print+="${MYSTIC_PC_START}${MYSTIC_AT_RESET}${MYSTIC_PC_STOP}"
printf "%b" "${_print}${_nl}"
}
mecho() { mystic_echo "${@}"; }
## eof
#####
# A collection of output filter functions
#####
# =============================================================================
# Shorten "text" to given length by inserting an ellipsis (or other string)
# between the first 1/4 "hint" and the remainder of "text".
#
# Try to gracefully deal with unreasonably small values of $length,
# but c'mon dude, be reasonable.
#
# You can tweak the hint percent by changing '25' to 1-99.
# hint=$(( ( 25 * length ) / 100 ))
#
# Arguments:
# int length trim to this length
# str ellipsis ellipsis char/str to use
# str "text" text to ellipsify
#
# Example:
# mystic_ellipsify 20 '...' "$(mystic_pwd)"
#
# Quirks:
# Don't use long ellipsis strings.
# =============================================================================
function mystic_ellipsify() {
local -i length hint
local ellipsis input
local output
length="$1"
ellipsis="$2"
shift 2
input="$*"
output=''
hint=$(( ( 25 * length ) / 100 ))
# short circuit
if (( ${#input} <= length )); then
printf '%s\n' "${input}"
return 0
fi
# include root hint if hint >= 1
if (( hint > ${#ellipsis} )); then
output+="${input:0:${hint}}"
output+="${ellipsis}"
length=$(( length - ${#output} ))
# or ellipsify at the front when $length is tiny
elif (( length > ( 2 * ${#ellipsis} ) )); then
output+="${ellipsis}"
length=$(( length - ${#output} ))
fi
# trim remainder to $length
output+=${input: -${length} }
printf '%s\n' "${output}"
}
# =============================================================================
# Walks "text" emitting mystic_echo() commands appropriate to each character.
#
# Arguments:
# str base_color mystic_echo() commands for default output
# str cap_color mystic_echo() commands to output CAPITALS
# str num_color mystic_echo() commands to output Numb3rs222
# str pun_color mystic_echo() commands to output Pun(tu@t!on
# str "text" the text to filter
#
# Examples:
# mystic_style_filter "bright blue" "bright white" \
# "bright yellow" "blue" "my Text 2 Filter!"
# mystic_style_filter "xt 005" "xt 555" "xt 550" "xt 003" "my Text 2 Filter!"
# =============================================================================
function mystic_style_filter() {
local cap_color num_color pun_color base_color input
local filter_tmp output
base_color="${1}"
cap_color="${2}"
num_color="${3}"
pun_color="${4}"
shift 4
input="${*}"
output=''
for (( i = 0; i < ${#input}; i++ )); do
filter_tmp="${input:${i}:1}"
case "${filter_tmp}" in
[a-z])
output+="${base_color} text ${filter_tmp} "
;;
[A-Z])
output+="${cap_color} text ${filter_tmp} "
;;
[0-9])
output+="${num_color} text ${filter_tmp} "
;;
[[:space:]])
output+="${filter_tmp} space "
;;
*)
output+="${pun_color} text ${filter_tmp} "
;;
esac
done
printf '%s' "${output}"
}
#####
# A small collection of utility functions
#####
# =============================================================================
# Return true if shelled out from Midnight Commander
# =============================================================================
function in_midnight_commander() {
if [[ -n "${MC_SID}${MC_TMPDIR}" ]]; then
return 0
else
return 1
fi
}
# =============================================================================
# Return true if shelled out from Ranger
# =============================================================================
function in_ranger() {
if [[ -n "${RANGER_LEVEL}" ]]; then
return 0
else
return 1
fi
}
# =============================================================================
# Small debugging utility to display a list of variables and their contents
#
# Arguments:
# str ${@} as list of variables to display
# =============================================================================
mystic_ckv() {
local arg
printf '%s\n' "$(mystic_fill 40 "-")" >&2
for arg in "${@}"; do
echo -n "${arg}: " >&2
echo "${!arg}" >&2
done
printf '%s\n' "$(mystic_fill 40 "-")" >&2
}
# Uncomment for short alias
# ckv() { mystic_ckv $@; }
#####
# Frame pieces and parts
#####
# =============================================================================
# Renders and prints top-left-corner with subshell indicators for a few popular
# programs that can "shell out".
#
# Globals:
# gets:
# MYSTIC_EXITCODE
# MYSTIC_COLOR_ERROR_MID
# MYSTIC_COLOR_OK_MID
# MYSTIC_GLYPH_LINE_V
#
# =============================================================================
function mystic_top_corner() {
local output