ansi_error

This commit is contained in:
pvincent
2026-07-20 11:53:30 +04:00
parent 560aef5e26
commit 4b91a11d04
6 changed files with 77 additions and 100 deletions
+30 -16
View File
@@ -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
}