Browse Source

ansi style

main
pvincent 1 week ago
parent
commit
43f28765a9
  1. 55
      lib/ansi.bash
  2. 5
      test/stub/scenario.bash

55
lib/ansi.bash

@ -3,14 +3,30 @@
# 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[93m'
FG_COLORS["YELLOW"]='\e[33m'
FG_COLORS["BLUE"]='\e[34m'
FG_COLORS["PURPLE"]='\e[35m'
FG_COLORS["CYAN"]='\e[36m'
FG_COLORS["MAGENTA"]='\e[95m'
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
@ -21,42 +37,59 @@ function ansi_length {
}
function style_padding {
local left right
local padding_left=${PADDING_LEFT:-$PADDING}
local padding_right=${PADDING_RIGHT:-$PADDING}
for _ in $(seq 1 "${padding_left}"); do builtin echo -n ' '; done
left=$(for _ in $(seq 1 "${padding_left}"); do builtin echo -n ' '; done)
style_ansi "$left"
builtin echo -n "$1"
for _ in $(seq 1 "${padding_right}"); do builtin echo -n ' '; done
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 {
[[ -n $1 ]] && echo "${BG_COLORS[$1]}" || true
}
function style_ansi {
local fg
local fg bg
fg=$(_style_fg "${FG:-}")
# local bg=${BG:-}
bg=$(_style_bg "${BG:-}")
[[ -n $fg ]] && builtin echo -en "$fg"
[[ -n $bg ]] && builtin echo -en "$bg"
builtin echo -en "$1"
[[ -n $fg ]] && builtin echo -en "$ANSI_RESET"
[[ -n $fg || -n $bg ]] && builtin echo -en "$ANSI_RESET"
true
}
function style_block {
local content length
local content length top bottom
content=$(style_padding "$1")
length=$(ansi_length "$content")
length=$((length + 2))
top=$(
builtin echo -n "╔"
for _ in $(seq 1 "${length}"); do builtin echo -n '═'; done
builtin echo "╗"
)
builtin echo "${top}"
builtin echo "$content"
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
}

5
test/stub/scenario.bash

@ -16,8 +16,9 @@ function do_success {
echo ']'
source "${MIAOU_BASH_DIR}/lib/ansi.bash"
local content
content=$(FG=MAGENTA style_ansi "this is success")
FG=GRAY PADDING=8 style_block "$content"
content=$(PADDING_LEFT=2 PADDING_RIGHT=6 style_padding 'this is success')
content=$(FG=PURPLE style_ansi "$content")
BG=RED PADDING=8 style_block "$content"
}
function do_unbound_variable {

Loading…
Cancel
Save