MIAOU-BASH is a collection of settings and helpers for leveraging BASH. Developer-friendly, it may be used as solo package with or without the miaou project.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

98 lines
2.2 KiB

# CONSTANTS
# MARGIN=0
PADDING=0
ANSI_RESET='\e[0m'
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'
declare -A BG_COLORS
BG_COLORS["BLACK"]='\e[40m'
BG_COLORS["RED"]='\e[41m'
BG_COLORS["GREEN"]='\e[42m'
BG_COLORS["YELLOW"]='\e[43m'
BG_COLORS["BLUE"]='\e[44m'
BG_COLORS["PURPLE"]='\e[45m'
BG_COLORS["CYAN"]='\e[46m'
BG_COLORS["WHITE"]='\e[47m'
# BG_COLORS["GRAY"]='\e[90m'
# BG_COLORS["MAGENTA"]='\e[95m'
# FUNCTIONS
function ansi_length {
local clean_text
clean_text=$(echo "$1" | sed 's/\x1B\[[0-9;]*m//g')
echo "${#clean_text}"
}
function style_padding {
local left right
local padding_left=${PADDING_LEFT:-$PADDING}
local padding_right=${PADDING_RIGHT:-$PADDING}
left=$(for _ in $(seq 1 "${padding_left}"); do builtin echo -n ' '; done)
style_ansi "$left"
builtin echo -n "$1"
right=$(for _ in $(seq 1 "${padding_right}"); do builtin echo -n ' '; done)
style_ansi "$right"
}
function _style_fg {
[[ -n $1 ]] && echo "${FG_COLORS[$1]}" || true
}
function _style_bg {
if [[ -n "$1" ]]; then
[[ -v BG_COLORS[$1] ]] && echo "${BG_COLORS[$1]}" && return
>&2 echo "unknown BG value: $1" && return 1
# false
fi
}
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"
builtin echo -en "$1"
[[ -n $fg || -n $bg ]] && builtin echo -en "$ANSI_RESET"
true
}
function style_block {
local content length top bottom
content=$(style_padding "$1")
length=$(ansi_length "$content")
top=$(
builtin echo -n "╔"
for _ in $(seq 1 "${length}"); do builtin echo -n '═'; done
builtin echo "╗"
)
style_ansi "${top}" && echo
style_ansi "║"
style_ansi "${content}"
style_ansi "║" && echo
bottom=$(
builtin echo -n "╚"
for _ in $(seq 1 "${length}"); do builtin echo -n '═'; done
builtin echo "╝"
)
style_ansi "${bottom}" && echo
}