ansi colors

This commit is contained in:
pvincent
2026-08-31 20:34:51 +04:00
parent 9ca22efd42
commit bdf977e78a
11 changed files with 123 additions and 266 deletions
+51 -19
View File
@@ -8,15 +8,47 @@ TMP_ERR="$TMP_DIRECTORY/_stderr"
TMP_MIAOU="$TMP_DIRECTORY/_miaou"
readonly TMP_DIRECTORY TMP_OUT TMP_ERR TMP_MIAOU
source "$MIAOU_BASH_DIR"/lib/ansi.bash # ANSI associative array
# functions
function ansi_code {
local code
for code in "$@"; do
if [[ -v ANSI[$code] ]]; then
echo -ne "\e[${ANSI[$code]}m"
else
>&2 echo "unknown ansi key: <$code>"
break
fi
done
}
function print_passed {
ansi_code FG_GREEN
echo -en '.'
ansi_code RESET
}
function print_bypassed {
ansi_code FG_YELLOW
echo -en 'b'
ansi_code RESET
}
function print_failed {
ansi_code FG_RED
echo -en 'F'
ansi_code RESET
}
function assert_tty {
((test_count++))
if [[ -w /dev/tty ]]; then
echo -n .
print_passed
return
fi
echo -n F
print_failed
failures+=("test -w /dev/tty")
return 1
}
@@ -26,11 +58,11 @@ function assert {
cmd="$MIAOU_BASH_DIR/test/stub/scenario.bash $1"
if $cmd >/dev/null 2>/dev/null 3>"$TMP_MIAOU"; then
if [[ -s "$TMP_MIAOU" ]]; then
echo -n .
print_passed
return
fi
fi
echo -n F
print_failed
failures+=("$cmd")
return 1
}
@@ -48,11 +80,11 @@ function assert_grep {
cmd="$MIAOU_BASH_DIR/test/stub/scenario.bash $script"
if $cmd "${extra_args[@]}" >"$TMP_OUT" 2>/dev/null 3>/dev/null; then
if grep -q "$content" "$TMP_OUT"; then
echo -n .
print_passed
return
fi
fi
echo -n F
print_failed
#TODO: quote extra_args
for i in "${extra_args[@]}"; do safe_args+=$(printf '%q ' "$i"); done
failures+=("$cmd $safe_args")
@@ -78,7 +110,7 @@ function fail_type {
if ! $locale_found; then
((test_bypassed++))
BYPASSED_LOCALES["$TEST_LANG"]=true
echo -n b
print_bypassed
return
fi
fi
@@ -95,10 +127,10 @@ function fail_type {
fi
if [[ $success == true ]]; then
echo -n .
print_passed
return
fi
echo -n F
print_failed
failures+=("${lang_options[*]} $cmd")
return 1
}
@@ -109,11 +141,11 @@ function fail_stderr_grep {
stderr_line="$2"
if ! $cmd 2>"$TMP_ERR" && grep -q "$stderr_line" "$TMP_ERR"; then
echo -n .
print_passed
return
fi
echo -n F
print_failed
failures+=("$cmd 2>&1 | grep -q \"$stderr_line\"")
return 1
}
@@ -126,12 +158,12 @@ function fail_stderr_count {
if ! $cmd >/dev/null 2>"$TMP_ERR" 3>/dev/null; then
mapfile -t stderr <"$TMP_ERR"
if [[ "${#stderr[@]}" == "$count" ]]; then
echo -n .
print_passed
return
fi
fi
echo -n F
print_failed
failures+=("$cmd 2>$TMP_ERR")
return 1
}
@@ -144,12 +176,12 @@ function fail_miaou_count {
if ! $cmd >/dev/null 2>/dev/null 3>"$TMP_MIAOU"; then
mapfile -t miaou <"$TMP_MIAOU"
if [[ "${#miaou[@]}" == "$count" ]]; then
echo -n .
print_passed
return
fi
fi
echo -n F
print_failed
failures+=("$cmd 3>$TMP_MIAOU")
return 1
}
@@ -159,7 +191,7 @@ function assert_locales_equally_populated {
local locales_dir="$MIAOU_BASH_DIR"/locales
local main_locale_file="$locales_dir"/"$1".sh
if [[ ! -f $main_locale_file ]]; then
echo -n F
print_failed
failures+=("missing locale file: $main_locale_file for locale: $1")
return 1
fi
@@ -169,7 +201,7 @@ function assert_locales_equally_populated {
for extra in "$@"; do
extra_locale_file="$locales_dir"/"$extra".sh
if [[ ! -f $extra_locale_file ]]; then
echo -n F
print_failed
failures+=("missing locale file: $extra_locale_file for locale: $extra")
return 1
fi
@@ -181,7 +213,7 @@ function assert_locales_equally_populated {
if [[ ${#difference[@]} -gt 0 ]]; then
variable_name=$(echo "${difference[1]}" | cut -d' ' -f2)
change_type="${difference[0]}"
echo -n F
print_failed
if [[ "$change_type" =~ d ]]; then
failures+=("locale=$extra (in $extra_locale_file) misses variable (to add): ${variable_name}")
else
@@ -191,7 +223,7 @@ function assert_locales_equally_populated {
fi
done
fi
echo -n .
print_passed
}
# main
-80
View File
@@ -1,80 +0,0 @@
#!/usr/bin/env miaou-bash
code=118
failure_type='SUBSHELL TERM'
failure_payload='blabla'
stack_sources=('file.bash' './lib/utility.bash' 'file.bash')
stack_lines=(18 56 12)
stack_functions=('f2' 'f1' '<main>')
# function ansi_block_continuation {
# tput sc
# tput cuu 1
# BG=RED FG=BLACK style_ansi "\t╦"
# tput rc
# FG=RED style_ansi "\t║\n"
# }
function ansi_block_error {
BG=RED
FG=BLACK
PADDING=2
local content detail exit_code
content=$(style_ansi "$1")
content=$(style_padding "$content")
detail=$(BG=BLACK style_padding "$2")
exit_code=$(FG=CYAN style_ansi " $3")
BLOCK_CONTINUATION_COL=5 style_block "$content\t$detail$exit_code"
ansi_reset
FG=RED style_ansi " ║"
echo
}
function ansi_traces {
local optional location sources lines functions
declare -n sources=$1
declare -n lines=$2
declare -n functions=$3
optional=false
for i in "${!sources[@]}"; do
FG=RED style_ansi " ╟─⟶"
location=$(printf "%40s" "${sources[$i]}:${lines[$i]}")
is_true "$optional" &&
location=$(FG=GRAY STYLE=ITALIC style_ansi "$location") ||
location=$(STYLE=ITALIC style_ansi "$location")
builtin echo "${location} ${functions[$i]}"
optional=true
done
}
function show_error {
>&2 printf '\r'
>&2 ansi_block_error "$failure_type" "$failure_payload" "$code"
>&2 ansi_traces stack_sources stack_lines stack_functions
}
function echo_return {
echo TEXT &&
return 11
return 1
}
declare -F style_ansi >/dev/null || source "$MIAOU_BASH_DIR/lib/ansi.bash"
echo START
if echo_return; then
echo YOUPI
else
echo ERROR
fi
if echo_return; then
echo YOUPI
else
echo ERROR
fi
# echo -n "standard flow:" && show_error
# echo -n "starting subshell: $(show_error)"