From 4b91a11d04023645615dcf2c134ca7b5ea7b2cdb Mon Sep 17 00:00:00 2001 From: pvincent Date: Mon, 20 Jul 2026 11:53:30 +0400 Subject: [PATCH] ansi_error --- lib/ansi.bash | 46 +++++++++++------- test/ansi.test.bash | 33 ------------- test/increment.test.bash | 19 -------- test/stub/ansi_error.bash | 47 +++++++++++++++++++ .../associative.bash} | 0 test/subarrays.test.bash | 32 ------------- 6 files changed, 77 insertions(+), 100 deletions(-) delete mode 100755 test/ansi.test.bash delete mode 100755 test/increment.test.bash create mode 100755 test/stub/ansi_error.bash rename test/{associative.test.bash => stub/associative.bash} (100%) delete mode 100755 test/subarrays.test.bash diff --git a/lib/ansi.bash b/lib/ansi.bash index 9c9c3d1..30f07ca 100644 --- a/lib/ansi.bash +++ b/lib/ansi.bash @@ -4,17 +4,23 @@ PADDING=0 ANSI_RESET='\e[0m' +declare -A STYLE_VARIANTS +STYLE_VARIANTS[BOLD]='\e[1m' +STYLE_VARIANTS[ITALIC]='\e[3m' +STYLE_VARIANTS[UNDERLINE]='\e[4m' +STYLE_VARIANTS[STRIKETHROUGH]='\e[9m' + declare -A FG_COLORS -FG_COLORS["BLACK"]='\e[30m' -FG_COLORS["RED"]='\e[31m' -FG_COLORS["GREEN"]='\e[32m' -FG_COLORS["YELLOW"]='\e[33m' -FG_COLORS["BLUE"]='\e[34m' -FG_COLORS["PURPLE"]='\e[35m' -FG_COLORS["CYAN"]='\e[36m' -FG_COLORS["WHITE"]='\e[37m' -FG_COLORS["GRAY"]='\e[90m' -FG_COLORS["MAGENTA"]='\e[95m' +FG_COLORS[BLACK]='\e[30m' +FG_COLORS[RED]='\e[31m' +FG_COLORS[GREEN]='\e[32m' +FG_COLORS[YELLOW]='\e[33m' +FG_COLORS[BLUE]='\e[34m' +FG_COLORS[PURPLE]='\e[35m' +FG_COLORS[CYAN]='\e[36m' +FG_COLORS[WHITE]='\e[37m' +FG_COLORS[GRAY]='\e[90m' +FG_COLORS[MAGENTA]='\e[95m' declare -A BG_COLORS BG_COLORS["BLACK"]='\e[40m' @@ -30,6 +36,11 @@ BG_COLORS["WHITE"]='\e[47m' # FUNCTIONS +function ansi_reset { + PADDING=0 + unset PADDING_LEFT PADDING_RIGHT BG FG +} + function ansi_length { local clean_text clean_text=$(echo "$1" | sed 's/\x1B\[[0-9;]*m//g') @@ -54,6 +65,10 @@ function _style_fg { [[ -n $1 ]] && echo "${FG_COLORS[$1]}" || true } +function _style_variant { + [[ -n $1 ]] && echo "${STYLE_VARIANTS[$1]}" || true +} + function _style_bg { if [[ -n "$1" ]]; then [[ -v BG_COLORS[$1] ]] && echo "${BG_COLORS[$1]}" && return @@ -63,13 +78,12 @@ function _style_bg { } function style_ansi { - local fg bg - fg=$(_style_fg "${FG:-}") - bg=$(_style_bg "${BG:-}") - [[ -n $fg ]] && builtin echo -en "$fg" - [[ -n $bg ]] && builtin echo -en "$bg" + local fg bg variant + fg=$(_style_fg "${FG:-}") && [[ -n $fg ]] && builtin echo -en "$fg" + bg=$(_style_bg "${BG:-}") && [[ -n $bg ]] && builtin echo -en "$bg" + variant=$(_style_variant "${STYLE:-}") && [[ -n $variant ]] && builtin echo -en "$variant" builtin echo -en "$1" - [[ -n $fg || -n $bg ]] && builtin echo -en "$ANSI_RESET" + [[ -n $fg || -n $bg || -n $variant ]] && builtin echo -en "$ANSI_RESET" true } diff --git a/test/ansi.test.bash b/test/ansi.test.bash deleted file mode 100755 index f2a1090..0000000 --- a/test/ansi.test.bash +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env miaou-bash - -. "$MIAOU_BASH_DIR/lib/ansi.bash" - -TYPE='SUBSHELL TERM' -PAYLOAD='blabla' - -stack_sources=('file.bash' './lib/utility.bash' 'file.bash') -stack_lines=(18 56 12) -stack_functions=('f2' 'f1' '
') - -BG=RED -FG=BLACK -PADDING=2 -content=$(style_ansi "${TYPE}") -content=$(style_padding "$content") -detail=$(BG=BLACK style_padding "${PAYLOAD}") -style_block "$content$detail" - -# block continuation -tput sc -tput cuu 1 -style_ansi "\t╦" -tput rc - -style_ansi "\t║" -echo - -for i in "${!stack_sources[@]}"; do - style_ansi "\t╟" - # echo "─⟶ ${sources[$i]}:${lines[$i]} in ${functions[$i]}" - printf "─⟶ %40s\t%s\n" "${stack_sources[$i]}:${stack_lines[$i]}" "${stack_functions[$i]}" -done diff --git a/test/increment.test.bash b/test/increment.test.bash deleted file mode 100755 index f1cdb45..0000000 --- a/test/increment.test.bash +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env miaou-bash - -function f1 { - if [[ -v nested_count ]]; then - >/dev/tty echo already defined $nested_count - ((nested_count++)) - else - >/dev/tty echo brand new - nested_count=1 - fi - echo $nested_count -} - -echo "v=$(f1)" -echo "v=$(f1)" -echo "v=$(f1)" -f1 -f1 -echo $nested_count diff --git a/test/stub/ansi_error.bash b/test/stub/ansi_error.bash new file mode 100755 index 0000000..16f6915 --- /dev/null +++ b/test/stub/ansi_error.bash @@ -0,0 +1,47 @@ +#!/usr/bin/env miaou-bash + +. "$MIAOU_BASH_DIR/lib/ansi.bash" + +TYPE='SUBSHELL TERM' +PAYLOAD='blabla' +stack_sources=('file.bash' './lib/utility.bash' 'file.bash') +stack_lines=(18 56 12) +stack_functions=('f2' 'f1' '
') + +function dump_failure { + BG=RED + FG=BLACK + PADDING=2 + + content=$(style_ansi "${TYPE}") + content=$(style_padding "$content") + detail=$(BG=BLACK style_padding "${PAYLOAD}") + style_block "$content$detail" + + ansi_reset +} + +function dump_traces { + local optional location + # block continuation + tput sc + tput cuu 1 + BG=RED FG=BLACK style_ansi "\t╦" + tput rc + + FG=RED style_ansi "\t║\n" + + optional=false + for i in "${!stack_sources[@]}"; do + FG=RED style_ansi "\t╟─⟶" + location="${stack_sources[$i]}:${stack_lines[$i]}" + is_true "$optional" && + location=$(FG=GRAY STYLE=ITALIC style_ansi "\t$location") || # FIXME: \t should not be required! + location=$(STYLE=ITALIC style_ansi "$location") + printf "%40s\t%s\n" "${location}" "${stack_functions[$i]}" + optional=true + done +} + +dump_failure +dump_traces diff --git a/test/associative.test.bash b/test/stub/associative.bash similarity index 100% rename from test/associative.test.bash rename to test/stub/associative.bash diff --git a/test/subarrays.test.bash b/test/subarrays.test.bash deleted file mode 100755 index e28a7e0..0000000 --- a/test/subarrays.test.bash +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env miaou-bash - -ArrayOfArray_1=("Alan" "24") -ArrayOfArray_2=("Walker" "31") -MainArray=() - -array_count=0 - -array_count=$(( array_count + 1 )) -array_name="array_$array_count" -declare -a "$array_name" -declare -n ref="$array_name" -ref+=("Bernard") -ref+=(46) -MainArray+=(ref[@]) - -array_count=$(( array_count + 1 )) -array_name="array_$(( array_count + 1 ))" -declare -a "$array_name" -declare -n ref2="$array_name" -ref2+=("Ferland") -ref2+=(17) -MainArray+=(ref2[@]) - - - -for sub_array in "${MainArray[@]}"; do - elements=("${!sub_array}") # Get the elements of the sub-array - name=${elements[0]} - age=${elements[1]} - echo "${name,,} $age" -done