-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackme.sh
executable file
·12240 lines (10529 loc) · 338 KB
/
stackme.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
#!/usr/bin/env bash
# Disable canonical mode, set immediate input
stty -icanon min 1 time 0
# Ensure terminal settings are restored on script exit
trap 'stty sane; exit' SIGINT SIGTERM EXIT
export TERM=xterm-256color
############################### BEGIN OF DISPLAY-RELATED CONSTANTS ############################
# Define colors with consistent names
declare -A COLORS=(
[yellow]="\e[33m"
[light_yellow]="\e[93m"
[green]="\e[32m"
[light_green]="\e[92m"
[white]="\e[97m"
[beige]="\e[93m"
[red]="\e[91m"
[light_red]="\e[31m"
[blue]="\e[34m"
[light_blue]="\e[94m"
[cyan]="\e[36m"
[light_cyan]="\e[96m"
[magenta]="\e[35m"
[light_magenta]="\e[95m"
[black]="\e[30m"
[gray]="\e[90m"
[dark_gray]="\e[37m"
[light_gray]="\x1b[38;5;245m"
[orange]="\x1b[38;5;214m"
[purple]="\x1b[38;5;99m"
[pink]="\x1b[38;5;200m"
[brown]="\x1b[38;5;94m"
[teal]="\x1b[38;5;80m"
[gold]="\x1b[38;5;220m"
[lime]="\x1b[38;5;154m"
[reset]="\e[0m"
)
# Define text styles
declare -A STYLES=(
[bold]="\e[1m"
[dim]="\e[2m"
[italic]="\e[3m"
[underline]="\e[4m"
[hidden]="\e[8m"
[reverse]="\e[7m"
[strikethrough]="\e[9m"
[double_underline]="\e[21m"
[overline]="\x1b[53m"
[bold_italic]="\e[1m\e[3m"
[underline_bold]="\e[4m\e[1m"
[dim_italic]="\e[2m\e[3m"
[reset]="\e[0m"
)
# Dictionary of arrows
declare -A ARROWS=(
["simple"]="→"
["sharp"]="➜"
["double"]="⇒"
["curved"]="↪"
["dash"]="➳"
["star"]="⋆"
["angle"]="▸"
["triangle_filled"]="▲"
["triangle"]="△"
["small_square_filled"]="▪"
["medium_empty_square"]="□"
["big_empty_square"]="▢"
["filled_square"]="■"
["square_filled_empty"]="▣"
["horiz_crossed_square"]="▤"
["vert_crossed_square"]="▥"
["crossed_square"]="▦"
["diag_square"]="▧"
["diag_crossed_square"]="▨"
["diamond"]="◆"
["cross"]="✗"
["dot"]="•"
["circle_filled"]="●"
["circle_empty"]="○"
["circle_filled_empty"]="⊙"
["circle_empty_filled"]="⊚"
)
# Highlight and color variables for styling keyboard options
highlight_color="\033[1;32m" # Highlight color (Bright Green)
faded_color="\033[2m" # Faded color (Dark gray)
select_color="\033[1;34m" # Blue for select (↵)
warning_color="\033[1;33m" # Warning color (Yellow)
back_color="\033[2m" # Magenta for return (r)
quit_color="\033[1;31m" # Red for quit (q)
exit_color="\033[1;31m" # Exit color (x)
search_color="\033[1;36m" # Search color (/)
goto_color="\033[1;36m" # Go-to page color (g)
help_color="\033[1;35m" # Help color (h)
error_color="\033[1;31m" # Error color (Dark red)
title_color="\033[1;36m" # Title color (Cyan)
reset_color="\033[0m" # Reset color
# Color definitions for styling
bold="\033[1m"
red="\033[31m"
green="\033[32m"
yellow="\033[33m"
blue="\033[34m"
magenta="\033[35m"
cyan="\033[35m"
normal="\033[0m"
# Define arrow keys for navigation
up_key="[A" # Up Arrow
down_key="[B" # Down Arrow
left_key="[D" # Left Arrow
right_key="[C" # Right Arrow
# Default menu options
TRUNCATED_DEFAULT_LENGTH=50
HAS_TIMESTAMP=true
HEADER_LENGTH=120
# Define a global associative array for storing menu items and stacks
declare -A MENUS
declare -A STACKS
# Define a global array for storing navigation history
menu_navigation_history=()
# PID of the currently running process
current_pid=0
# Default arrow
USER_DEFINED_ARROW=""
DEFAULT_ARROW_OPTION='angle'
# Default page size
DEFAULT_PAGE_SIZE=5
DEFAULT_DISK_THREHOLD=85
############################## END OF DISPLAY-RELATED CONSTANTS ################################
########################### BEGIN OF DEPLOYMENT-RELATED CONSTANTS ##############################
# Tools constants
TOOL_NAME="StackMe"
TOOL_LOGO_URL="https://raw.githubusercontent.com/whosbash/stackme/main/images/stackme_tiny.png"
TOOL_REPOSITORY_URL='https://github.com/whosbash/stackme'
TOOL_STACKS_TEMPLATE_URL="https://api.github.com/repos/whosbash/stackme/contents/stacks"
TOOL_STACKS_OBJECT_URL="https://raw.githubusercontent.com/whosbash/stackme/main/stacks/stacks.json"
TOOL_BASE_DIR="/opt/stackme"
TOOL_STACKS_DIR="$TOOL_BASE_DIR/stacks"
TOOL_TEMPLATES_DIR="$TOOL_BASE_DIR/templates"
############################ END OF DEPLOYMENT-RELATED CONSTANTS #############################
############################# BEGIN OF DISPLAY-RELATED FUNCTIONS #############################
# Function to display a message with improved formatting
display() {
local type="$1"
local text="$2"
local timestamp="${3:-$HAS_TIMESTAMP}"
echo -e "$(format "$type" "$text" $timestamp)"
}
# Function to apply color and style to a string, even if it contains existing color codes
colorize() {
local text="$1"
local color_name=$(echo "$2" | tr '[:upper:]' '[:lower:]')
local style_name=$(echo "$3" | tr '[:upper:]' '[:lower:]')
# Remove any existing ANSI escape sequences (colors or styles) from the text
text=$(strip_color "$text")
# Get color code, default to reset if not found
local color_code="${COLORS[$color_name]:-${COLORS[reset]}}"
# If no style name is provided, use "reset" style as default
if [[ -z "$style_name" ]]; then
local style_code="${STYLES[reset]}"
else
local style_code="${STYLES[$style_name]:-${STYLES[reset]}}"
fi
# Print the text with the chosen color and style
echo -e "${style_code}${color_code}${text}${STYLES[reset]}${COLORS[reset]}"
}
get_status_icon() {
local type="$1"
case "$type" in
"success") echo "🌟" ;; # Bright star for success
"error") echo "🔥" ;; # Fire icon for error
"warning") echo "⚠️" ;; # Lightning for warning
"info") echo "💡" ;; # Light bulb for info
"highlight") echo "🌈" ;; # Rainbow for highlight
"debug") echo "🔍" ;; # Magnifying glass for debug
"critical") echo "💀" ;; # Skull for critical
"note") echo "📌" ;; # Pushpin for note
"important") echo "⚡" ;; # Rocket for important
"hold_on") echo "⌛" ;; # Hourglass for waiting
"question") echo "🤔" ;; # Thinking face for question
"celebrate") echo "🎉" ;; # Party popper for celebration
"progress") echo "📈" ;; # Upwards chart for progress
"failure") echo "💔" ;; # Broken heart for failure
"tip") echo "🍀" ;; # Four-leaf clover for additional success
*) echo "🌀" ;; # Cyclone for undefined type
esac
}
# Function to get the color code based on the message type
get_status_color() {
local type="$1"
case "$type" in
"success") echo "green" ;; # Green for success
"error") echo "light_red" ;; # Light Red for error
"warning") echo "yellow" ;; # Yellow for warning
"info") echo "teal" ;; # White for info
"highlight") echo "cyan" ;; # Cyan for highlight
"debug") echo "blue" ;; # Blue for debug
"critical") echo "light_magenta" ;; # Light Magenta for critical
"note") echo "pink" ;; # Gray for note
"important") echo "gold" ;; # Orange for important
"hold_on") echo "light_yellow" ;; # Light Yellow for waiting
"question") echo "purple" ;; # Purple for question
"celebrate") echo "green" ;; # Green for celebration
"progress") echo "lime" ;; # Blue for progress
"failure") echo "light_red" ;; # Red for failure
"tip") echo "light_cyan" ;; # Light Green for tips
*) echo "white" ;; # Default to white for unknown types
esac
}
# Function to get the style code based on the message type
get_status_style() {
local type="$1"
case "$type" in
"success") echo "bold" ;; # Bold for success
"info") echo "italic" ;; # Italic for info
"error") echo "bold,italic" ;; # Bold and italic for errors
"critical") echo "bold,underline" ;; # Bold and underline for critical
"warning") echo "italic" ;; # Underline for warnings
"highlight") echo "bold,underline" ;; # Bold and underline for highlights
"hold_on") echo "dim,italic" ;; # Dim and italic for pending
"important") echo "bold,underline,overline" ;; # Bold, underline, overline for important
"question") echo "italic,underline" ;; # Italic and underline for questions
"celebrate") echo "bold" ;; # Bold for celebration
"progress") echo "italic" ;; # Italic for progress
"failure") echo "bold,italic" ;; # Bold and italic for failure
"tip") echo "bold,italic" ;; # Bold and italic for tips
*) echo "normal" ;; # Default to normal style for unknown types
esac
}
# Function to colorize a message based on its type
colorize_by_type() {
local type="$1"
local text="$2"
local color="$(get_status_color "$type")"
local style="$(get_status_style "$type")"
colorize "$text" "$color" "$style"
}
# Function to format a message
format() {
local type="$1" # Message type (success, error, etc.)
local text="$2" # Message text
local has_timestamp="${3:-$HAS_TIMESTAMP}" # Option to display timestamp (default is false)
# Get icon based on status
local icon
icon=$(get_status_icon "$type")
local color
color=$(get_status_color "$type")
# Add timestamp if enabled
local timestamp=""
if [ "$has_timestamp" = true ]; then
timestamp="[$(date '+%Y-%m-%d %H:%M:%S')] "
# Only colorize the timestamp
timestamp="$(colorize "$timestamp" "$color" "normal")"
fi
# Colorize the main message
local colorized_message
colorized_message="$(colorize_by_type "$type" "$text")"
# Display the message with icon, timestamp, and colorized message
echo -e "$icon $timestamp$colorized_message"
}
# Function to format an array of messages
format_array() {
local type="$1"
local -n arr="$2" # Use reference to the array
for i in "${!arr[@]}"; do
# Apply format to each message in the array
arr[$i]=$(format "$type" "${arr[$i]}")
done
}
# Function to display styled and optionally centered text with custom or terminal width
display_text() {
local text="$1" # The text to display
local custom_width="$2" # Custom width for the text
local padding=0 # Optional padding around the text
local center=false # Set to true to center the text
local style="${bold_color}" # Optional style (e.g., bold or colored)
# Check if additional options are provided
shift 2 # Skip the first two arguments (text and custom_width)
while [[ $# -gt 0 ]]; do
case "$1" in
--center) center=true ;;
--style)
style="$2"
shift
;;
--padding)
padding="$2"
shift
;;
*)
echo "Unknown option: $1" >&2
return 1
;;
esac
shift
done
# If custom width is not provided, use terminal width (tput cols)
if [[ -z "$custom_width" ]]; then
custom_width=$(tput cols)
fi
# Ensure padding is valid
if ((padding < 0)); then
error "Padding must be non-negative."
return 1
fi
# Calculate text length with padding
local padded_text=$(printf "%${padding}s%s%${padding}s" "" "$text" "")
local text_length=${#padded_text}
# Ensure the custom width is at least the length of the text
if ((custom_width < text_length)); then
error "Custom width ($custom_width) is smaller than text length ($text_length)."
return 1
fi
# Center the text if needed
if [[ "$center" == true ]]; then
local left_padding=$(((custom_width - text_length) / 2))
padded_text=$(printf "%${left_padding}s%s" "" "$padded_text")
fi
# Ensure the text fits within the custom width
local final_text=$(printf "%-${custom_width}s" "$padded_text")
# Apply styling and display
echo -e "${style}${final_text}${reset_color}"
echo # Newline
return 0
}
# Function to display success formatted messages
success() {
local message="$1" # Step message
local timestamp="${2:-$HAS_TIMESTAMP}" # Optional timestamp flag
display 'success' "$message" $timestamp >&2
}
# Function to display error formatted messages
error() {
local message="$1" # Step message
local timestamp=""${2:-$HAS_TIMESTAMP}"" # Optional timestamp flag
display 'error' "$message" $timestamp >&2
}
# Function to display warning formatted messages
warning() {
local message="$1" # Step message
local timestamp=""${2:-$HAS_TIMESTAMP}"" # Optional timestamp flag
display 'warning' "$message" $timestamp >&2
}
# Function to display info formatted messages
info() {
local message="$1" # Step message
local timestamp=""${2:-$HAS_TIMESTAMP}"" # Optional timestamp flag
display 'info' "$message" $timestamp >&2
}
# Function to display highlight formatted messages
highlight() {
local message="$1" # Step message
local timestamp="${2:-$HAS_TIMESTAMP}" # Optional timestamp flag
display 'highlight' "$message" $timestamp >&2
}
# Function to display debug formatted messages
debug() {
local message="$1" # Step message
local timestamp="${2:-$HAS_TIMESTAMP}" # Optional timestamp flag
display 'debug' "$message" $timestamp >&2
wait_for_input
}
# Function to display critical formatted messages
critical() {
local message="$1" # Step message
local timestamp="${2:-$HAS_TIMESTAMP}" # Optional timestamp flag
display 'critical' "$message" $timestamp >&2
}
# Function to display note formatted messages
note() {
local message="$1" # Step message
local timestamp="${2:-$HAS_TIMESTAMP}" # Optional timestamp flag
display 'note' "$message" $timestamp >&2
}
# Function to display important formatted messages
important() {
local message="$1" # Step message
local timestamp="${2:-$HAS_TIMESTAMP}" # Optional timestamp flag
display 'important' "$message" $timestamp >&2
}
# Function to display wait formatted messages
hold_on() {
local message="$1" # Step message
local timestamp="${2:-$HAS_TIMESTAMP}" # Optional timestamp flag
display 'hold_on' "$message" $timestamp >&2
}
# Function to display wait formatted messages
question() {
local message="$1" # Step message
local timestamp="${2:-$HAS_TIMESTAMP}" # Optional timestamp flag
display 'question' "$message" $timestamp >&2
}
# Function to display celebrate formatted messages
celebrate() {
local message="$1" # Step message
local timestamp="${2:-$HAS_TIMESTAMP}" # Optional timestamp flag
display 'celebrate' "$message" $timestamp >&2
}
# Function to display progress formatted messages
progress() {
local message="$1" # Step message
local timestamp="${2:-$HAS_TIMESTAMP}" # Optional timestamp flag
display 'progress' "$message" $timestamp >&2
}
# Function to display failure formatted messages
failure() {
local message="$1" # Step message
local timestamp="${2:-$HAS_TIMESTAMP}" # Optional timestamp flag
display 'failure' "$message" $timestamp >&2
wait_for_input
}
# Function to display tip formatted messages
tip() {
local message="$1" # Step message
local timestamp="${2:-$HAS_TIMESTAMP}" # Optional timestamp flag
display 'tip' "$message" $timestamp >&2
}
# Function to display a step with improved formatting
step() {
local current_step="$1" # Current step number
local total_steps="$2" # Total number of steps
local message="$3" # Step message
local type="${4:-DEFAULT_TYPE}" # Status type (default to 'info')
local timestamp="${5:-$HAS_TIMESTAMP}" # Optional timestamp flag
# If 'timestamp' is passed as an argument, prepend the timestamp to the message
if [ -n "$timestamp" ]; then
local formatted_message=$(format "$type" "$step_message" true)
else
local formatted_message=$(format "$type" "$step_message" false)
fi
# Format the step message with the specified color and style
local message="[$current_step/$total_steps] $message"
formatted_message=$(format "$type" "$message" $timestamp)
# Print the formatted message with the icon and message
echo -e "$formatted_message" >&2
}
# Function to display step info message
step_info() {
local current=$1
local total=$2
local message="$3"
local has_timestamp=${4:-$HAS_TIMESTAMP}
step $current $total "$message" "info" $has_timestamp
}
# Function to display step success message
step_success() {
local current=$1
local total=$2
local message="$3"
local has_timestamp=${4:-$HAS_TIMESTAMP}
step $current $total "$message" "success" $has_timestamp
}
# Function to display step failure message
step_failure() {
local current=$1
local total=$2
local message="$3"
local has_timestamp=${4:-$HAS_TIMESTAMP}
step $current $total "$message" "failure" $has_timestamp
}
# Function to display step error message
step_error() {
local current=$1
local total=$2
local message="$3"
local has_timestamp=${4:-$HAS_TIMESTAMP}
step $current $total "$message" "error" $has_timestamp
}
# Function to display step warning message
step_warning() {
local current=$1
local total=$2
local message="$3"
local has_timestamp=${4:-$HAS_TIMESTAMP}
step $current $total "$message" "warning" $has_timestamp
}
# Function to display step success message
step_progress() {
local current=$1
local total=$2
local message="$3"
local has_timestamp=${4:-$HAS_TIMESTAMP}
step $current $total "$message" "progress" $has_timestamp
}
stack_step() {
stack_name="$1"
type="$2"
step="$3"
message="$4"
stack_message="[$stack_name] $message"
step $step $total_steps "$stack_message" "$type"
}
stack_step_progress() {
stack_step "$1" "progress" "$2" "$3"
}
stack_step_warning() {
stack_step "$1" "warning" "$2" "$3"
}
stack_step_error() {
stack_step "$1" "error" "$2" "$3"
}
# Function to display a boxed text
boxed_text() {
local word=${1:-"Hello"} # Default word to render
local text_style=${2:-"highlight"} # Default text style
local border_style=${3:-"simple"} # Default border style
local font=${4:-"slant"} # Default font
local min_width=${5:-$(($(tput cols) - 28))} # Default minimum width
local has_timestamp=${6:-$HAS_TIMESTAMP}
# Ensure `figlet` exists
if ! command -v figlet &>/dev/null; then
error "'figlet' command not found. Please install it to use this function."
return 1
fi
# Define the border styles
declare -A border_styles=(
["simple"]="- - | | + + + +"
["asterisk"]="* * * * * * * *"
["equal"]="= = | | + + + +"
["hash"]="# # # # # # # #"
["dotted"]=". . . . . . . ."
["starred"]="* * * * * * * *"
["boxed-dashes"]="- - - - - - - -"
["wave"]="~ ~ ~ ~ ~ ~ ~ ~"
["angled"]="/ / \\ \\ / \\ \\ /"
["arrowed"]="< > ^ v < > ^ v"
["zigzag"]="z z Z Z z Z Z z"
["spiky"]="x x x x X X X X"
["none"]=" "
)
# Extract the border characters
IFS=' ' read -r \
top_fence bottom_fence left_fence right_fence \
top_left_corner top_right_corner \
bottom_left_corner bottom_right_corner <<< \
"${border_styles[$border_style]:-${border_styles["simple"]}}"
# Generate ASCII art
local ascii_art=$(figlet -f "$font" "$word")
local art_width=$(echo "$ascii_art" | head -n 1 | wc -c)
art_width=$((art_width - 1)) # Subtract newline
# Get terminal width and calculate box width
local terminal_width=$(tput cols)
local total_width=$((min_width > art_width ? min_width : art_width))
total_width=$((total_width > (terminal_width - 2) ? (terminal_width - 2) : total_width))
# Generate borders
local top_border="${top_left_corner}$(
printf "%-${total_width}s" | tr ' ' "$top_fence"
)${top_right_corner}"
fmt_top_border="$(colorize_by_type "$text_style" "$top_border")"
local bottom_border="${bottom_left_corner}$(
printf "%-${total_width}s" | tr ' ' "$bottom_fence"
)${bottom_right_corner}"
fmt_bottom_border="$(colorize_by_type "$text_style" "$bottom_border")"
# Buffer all lines to an array
local -a lines=()
# Add the top border
lines+=("$fmt_top_border")
while IFS= read -r line; do
local total_padding=$((total_width - ${#line}))
local left_padding=$((total_padding / 2))
local right_padding=$((total_padding - left_padding)) # Ensures total_padding is fully distributed
line="$(
printf "%s%*s%s%*s%s" \
"$left_fence" "$left_padding" "" "$line" "$right_padding" "" "$right_fence"
)"
fmt_line="$(colorize_by_type "$text_style" "$line")"
lines+=("$fmt_line")
done <<<"$ascii_art"
# Add the bottom border
fmt_bottom_border=$(
colorize_by_type "$text_style" "$bottom_border"
)
lines+=("$fmt_bottom_border")
# Display the lines in parallel
display_parallel lines
}
header() {
local word=${1:-"Hello"} # Default word to render
local text_style=${2:-"highlight"} # Default text style
local border_style=${3:-"simple"} # Default border style
local font=${4:-"slant"} # Default font
local min_width=${5:-$(($(tput cols) - 28))} # Default minimum width
local has_timestamp=${6:-false} # Default has_timestamp
boxed_text "$word" "$text_style" "$border_style" "$font" "$min_width" "$has_timestamp"
}
rows_count(){
local text="$1"
echo "$text" | awk 'END {print NR}'
}
get_header_height() {
local title="$1"
local page_width="$2"
header_string=$(\
header "$title" "highlight" "simple" "slant" "$page_width" false
)
# Height of the header
header_height=$(rows_count "$header_string")
echo "$header_height"
}
render_header() {
local title="$1"
local page_width="$2"
header_string=$(\
header "$title" "highlight" "simple" "slant" "$page_width" false
)
echo -e "$header_string" >&2
}
display_header() {
local title="$1"
display_text "$title" 40 --center --style "${bold_color}${green}"
}
# Function to set the arrow based on user input
set_arrow() {
if [[ -n "$USER_DEFINED_ARROW" ]]; then
# If the user provides an arrow option, validate and set it
if [[ -n "${ARROWS[$USER_DEFINED_ARROW]}" ]]; then
ARROW_OPTION="$USER_DEFINED_ARROW"
else
# Handle invalid user-defined arrow options
warning "'$USER_DEFINED_ARROW' is not a valid arrow option."
echo "Available options: ${!ARROWS[@]}"
warning "Falling back to default arrow: $DEFAULT_ARROW_OPTION"
ARROW_OPTION="$DEFAULT_ARROW_OPTION"
sleep 2
fi
else
ARROW_OPTION="$DEFAULT_ARROW_OPTION"
fi
SELECTED_ARROW="${ARROWS[$ARROW_OPTION]}"
COLORED_ARROW="${highlight_color}${SELECTED_ARROW}${reset_color}"
}
################################## END OF DISPLAY-RELATED FUNCTIONS ###############################
################################## BEGIN OF JSON-RELATED FUNCTIONS ################################
# Recursive function to validate JSON against a schema
validate_json_recursive() {
local json="$1"
local schema="$2"
local parent_path="$3" # Track the JSON path for better error reporting
local valid=true
local errors=()
# Extract required keys and properties from the schema
local required_keys=$(echo "$schema" | jq -r '.required[]? // empty')
local properties=$(echo "$schema" | jq -c '.properties // empty')
local additional_properties=$(
echo "$schema" |
jq -r 'if has("additionalProperties") then .additionalProperties else "true" end'
)
# Check if required keys are present
for key in $required_keys; do
if ! echo "$json" | jq -e ". | has(\"$key\")" >/dev/null; then
errors+=("Missing required key: ${parent_path}${key}")
valid=false
fi
done
# Validate each property
for key in $(echo "$properties" | jq -r 'keys[]'); do
local expected_type
local actual_type
local sub_schema
local value
expected_type=$(echo "$properties" | jq -r ".\"$key\".type // empty")
sub_schema=$(echo "$properties" | jq -c ".\"$key\"")
value=$(echo "$json" | jq -c ".\"$key\"")
actual_type=$(echo "$value" | jq -r 'type // empty')
if [ "$expected_type" = "object" ]; then
# Recursively validate nested objects
if [ "$actual_type" = "object" ]; then
validate_json_recursive "$value" "$sub_schema" "${parent_path}${key}."
else
errors+=("Key '${parent_path}${key}' expected type 'object', but got '$actual_type'")
valid=false
fi
elif [ "$expected_type" = "array" ]; then
# Validate array elements
validate_array "$value" "$sub_schema" "${parent_path}${key}" errors valid
else
# Validate primitive types and handle constraints
validate_primitive "$value" "$sub_schema" \
"$expected_type" "$actual_type" "${parent_path}${key}" errors valid
fi
done
# Handle additionalProperties
if [ "$additional_properties" = "false" ]; then
for key in $(echo "$json" | jq -r 'keys[]'); do
if ! echo "$properties" | jq -e "has(\"$key\")" >/dev/null; then
errors+=(
"Extra property '${parent_path}${key}' found, but additionalProperties is false."
)
valid=false
fi
done
fi
# Print errors if any
if [ "$valid" = false ]; then
for error in "${errors[@]}"; do
echo "$error"
done
fi
}
# Validate array elements
validate_array() {
local array="$1"
local schema="$2"
local path="$3"
local -n errors_ref=$4
local -n valid_ref=$5
local items_schema=$(echo "$schema" | jq -c '.items // empty')
local array_length=$(echo "$array" | jq 'length')
for ((i = 0; i < array_length; i++)); do
local element=$(echo "$array" | jq -c ".[$i]")
local element_type=$(echo "$element" | jq -r 'type')
local expected_type=$(echo "$items_schema" | jq -r '.type // empty')
if [ "$element_type" != "$expected_type" ] && [ "$expected_type" != "null" ]; then
errors_ref+=(
"Array element ${path}[$i] expected type '$expected_type', but got '$element_type'"
)
valid_ref=false
fi
# Recursively validate array elements
validate_json_recursive "$element" "$items_schema" "${path}[$i]."
done
}
# Validate primitive types and handle constraints
validate_primitive() {
local value="$1"
local schema="$2"
local expected_type="$3"
local actual_type="$4"
local path="$5"
local -n errors_ref=$6
local -n valid_ref=$7
if [ "$expected_type" != "$actual_type" ] &&
[ "$actual_type" != "null" ]; then
errors_ref+=("Key '${path}' expected type '$expected_type', but got '$actual_type'")
valid_ref=false
fi
# Handle additional constraints (pattern, enum, etc.)
handle_constraints "$value" "$schema" "$path" errors_ref valid_ref
}
# Handle additional constraints
handle_constraints() {
local value="$1"
local schema="$2"
local path="$3"
local -n errors_ref=$4
local -n valid_ref=$5
local pattern=$(echo "$schema" | jq -r '.pattern // empty')
local enum_values=$(echo "$schema" | jq -c '.enum // empty')
local multiple_of=$(echo "$schema" | jq -r '.multipleOf // empty')
# Pattern matching
if [ -n "$pattern" ] &&
! [[ "$value" =~ $pattern ]]; then
errors_ref+=("Key '${path}' does not match pattern '$pattern'")
valid_ref=false
fi
# Enum validation
if [ "$enum_values" != "null" ] &&
! echo "$enum_values" | jq -e ". | index($value)" >/dev/null; then
errors_ref+=("Key '${path}' value '$value' is not in the allowed values: $enum_values")
valid_ref=false
fi
# MultipleOf constraint
if [ -n "$multiple_of" ] &&
(($(echo "$value % $multiple_of" | bc) != 0)); then
errors_ref+=("Key '${path}' value '$value' is not a multiple of $multiple_of")
valid_ref=false
fi
}
# Main function to validate a JSON file against a schema
validate_json_from_schema() {
local json="$1"
local schema="$2"
validate_json_recursive "$json" "$schema" ""
}
# Function to decode JSON and base64
query_json64() {
local item="$1"
local field="$2"
echo "$item" | base64 --decode | jq -r "$field" || {
error "Invalid JSON or base64 input!"
return 1
}
}
# Function to convert each element of a JSON array to base64
convert_json_array_to_base64_array() {
local json_array="$1"
# Convert each element of the JSON array to base64 using jq
echo "$json_array" | jq -r '.[] | @base64'
}
# Function to search for an object in a JSON array
search_on_json_array() {
local json_array_string="$1"
local search_key="$2"
local search_value="$3"
# Validate JSON
if ! echo "$json_array_string" | jq . >/dev/null 2>&1; then
echo "Invalid JSON array."
return 1
fi
# Search for an object in the array with the specified key-value pair
if [[ -n "$search_key" && -n "$search_value" ]]; then
local matched_item
matched_item=$(
echo "$json_array_string" |
jq -c --arg key "$search_key" --arg value "$search_value" \
'.[] | select(.[$key] == $value)'
)
if [[ -n "$matched_item" ]]; then
echo "$matched_item"
return 0
else
echo "No matching item found for key '$search_key' and value '$search_value'."
return 1
fi
fi
}
# Function to add JSON objects or arrays
add_json_objects() {
local json1="$1" # First JSON input
local json2="$2" # Second JSON input
# Get the types of the input JSON values
local type1
local type2
type1=$(echo "$json1" | jq -e type 2>/dev/null | tr -d '"')
type2=$(echo "$json2" | jq -e type 2>/dev/null | tr -d '"')
# Check if both types were captured successfully
if [ -z "$type1" ] || [ -z "$type2" ]; then
echo "One or both inputs are invalid JSON."
return 1
fi
# Perform different operations based on the types of inputs
local merged
case "$type1-$type2" in
object-object)
# Merge the two JSON objects
merged=$(jq -sc '.[0] * .[1]' <<<"$json1"$'\n'"$json2")
;;
object-array)
# Append the object to the array
merged=$(jq -c '. + [$json1]' --argjson json1 "$json1" <<<"$json2")
;;
array-object)
# Append the object to the array
merged=$(jq -c '. + [$json2]' --argjson json2 "$json2" <<<"$json1")
;;
array-array)
# Concatenate the two arrays