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
+31 -116
View File
@@ -1,120 +1,35 @@
# CONSTANTS
# TODO: build a global `declare -A __MIAOU_LOADED_LIBRARIES`
# MARGIN=0
PADDING=0
ANSI_RESET='\e[0m'
if [[ ! -v ANSI ]]; then
declare -A ANSI
ANSI[RESET]=0
BLOCK_CONTINUATION_COL=
BLOCK_CONTINUATION_SYMBOL=
ANSI[STYLE_ITALIC]=3
ANSI[STYLE_BOLD]=1
ANSI[STYLE_UNDERLINE]=4
ANSI[STYLE_STRIKETHROUGH]=9
declare -A STYLE_VARIANTS
STYLE_VARIANTS[BOLD]='\e[1m'
STYLE_VARIANTS[ITALIC]='\e[3m'
STYLE_VARIANTS[UNDERLINE]='\e[4m'
STYLE_VARIANTS[STRIKETHROUGH]='\e[9m'
ANSI[FG_BLACK]=30
ANSI[FG_RED]=31
ANSI[FG_GREEN]=32
ANSI[FG_YELLOW]=33
ANSI[FG_BLUE]=34
ANSI[FG_PURPLE]=35
ANSI[FG_CYAN]=36
ANSI[FG_WHITE]=37
ANSI[FG_GRAY]=90
ANSI[FG_LIGHTBLUE]=94
ANSI[FG_MAGENTA]=95
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_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')
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"
style_ansi "$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_variant {
[[ -n $1 ]] && echo "${STYLE_VARIANTS[$1]}" || true
}
function _style_bg {
if [[ -n "$1" ]]; then
[[ -v BG_COLORS[$1] ]] && echo "${BG_COLORS[$1]}" || >&2 echo "unknown BG value: $1"
fi
}
function style_ansi {
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 || -n $variant ]] && builtin echo -en "$ANSI_RESET"
true
}
function style_block {
local content length top bottom i
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}" && builtin echo
style_ansi "║"
style_ansi "${content}"
style_ansi "║" && builtin echo
bottom=$(
builtin echo -n "╚"
for i in $(seq 1 "${length}"); do
if [[ $i == "$BLOCK_CONTINUATION_COL" ]]; then
builtin echo -n "$BLOCK_CONTINUATION_SYMBOL"
else
builtin echo -n '═'
fi
done
builtin echo "╝"
)
style_ansi "${bottom}" && builtin echo
}
ANSI[BG_BLACK]=40
ANSI[BG_RED]=41
ANSI[BG_GREEN]=42
ANSI[BG_YELLOW]=43
ANSI[BG_BLUE]=44
ANSI[BG_PURPLE]=45
ANSI[BG_CYAN]=46
ANSI[BG_WHITE]=47
ANSI[BG_GRAY]=90
ANSI[BG_MAGENTA]=95
readonly ANSI
fi
+4 -7
View File
@@ -15,12 +15,8 @@ readonly BACKUP_SUFFIX='.ORIGINAL'
# FUNCTIONS
function usr_bin_miaou_bash {
ln -sf "$MIAOU_BASH_DIR/bin/miaou-bash" /usr/bin/miaou-bash
}
function install_required_packages {
. "$MIAOU_BASH_DIR"/tools/pkg_add "${REQUIRED_PKGS[@]}"
. "$MIAOU_BASH_DIR"/bin/pkg_add "${REQUIRED_PKGS[@]}"
}
function fix_users_bashrc {
@@ -198,8 +194,9 @@ function ghostty_global_config {
done
}
function link_miaou_bash {
function link_bin_scripts {
sudo ln -sf "$MIAOU_BASH_DIR"/bin/miaou-bash /usr/bin/
sudo ln -sf "$MIAOU_BASH_DIR"/bin/pkg_add /usr/bin/
}
function create_sudoers {
@@ -263,7 +260,7 @@ fix_terminfo_ghostty
fix_locales
ghostty_global_config
link_config_files
link_miaou_bash
link_bin_scripts
create_sudoers
fetch_mode
apply_mode
+1 -1
View File
@@ -22,10 +22,10 @@ function uninstall {
sudo rm -f /etc/profile.d/20-miaou-bash.sh # deprecated: for older version
sudo rm -f /etc/profile.d/zz-miaou-bash.sh
sudo rm -f /etc/vimrc/vimrc.{azerty,core,local,miaou,rookie}
sudo rm -f /usr/bin/miaou-bash
sudo rm -f /etc/sudoers.d/miaou-bash
sudo rm -rf /var/lib/miaou-bash
sudo rm -r /etc/bash_completion.d/miaou-bash.complete
sudo rm -f /usr/bin/{miaou-bash,pkg_add}
sudo rm -rf /opt/miaou-bash
cat <<EOF