Files
bashlib/bashmsg.inc
2021-07-28 03:13:39 +02:00

109 lines
2.9 KiB
Bash

#!/hint/bash
# defcolor <color name> <function>
function _bashmsg.defcolor {
local col=$1
local fn=$2
eval "function _bashmsg.${col} { ${fn} \${@}; }"
}
# dyncolor <type> <command>
function _bashmsg.dyncolor {
local typevar="_bashmsg_col_${1}"
local col=${!typevar}
shift
_bashmsg.${col} "$@"
}
# def_bashcols_color <color name> <color id>
function _bashmsg.def_bashcols_color {
if [[ -n $_bashlib_bashcols ]]; then
defcol "$1" "$2"
_bashmsg.defcolor "$1" "$1"
else
msg_error "${FUNCNAME}: bashcols is not loaded, color support unavailable"
return 1
fi
}
function _bashmsg.is_color_defined {
PATH= command -v _bashmsg.${1} >/dev/null
}
################# INITIALIZATION ################
if [[ -z $_bashlib_bashcols ]]; then
function _bashmsg.nocolor { "$@"; }
_bashmsg.defcolor red _bashmsg.nocolor
_bashmsg.defcolor green _bashmsg.nocolor
_bashmsg.defcolor brown _bashmsg.nocolor
_bashmsg.defcolor blue _bashmsg.nocolor
_bashmsg.defcolor magenta _bashmsg.nocolor
_bashmsg.defcolor cyan _bashmsg.nocolor
_bashmsg.defcolor white _bashmsg.nocolor
_bashmsg.defcolor grey _bashmsg.nocolor
_bashmsg.defcolor redl _bashmsg.nocolor
_bashmsg.defcolor greenl _bashmsg.nocolor
_bashmsg.defcolor yellow _bashmsg.nocolor
_bashmsg.defcolor bluel _bashmsg.nocolor
_bashmsg.defcolor magenta _bashmsg.nocolor
_bashmsg.defcolor cyanl _bashmsg.nocolor
_bashmsg.defcolor whitel _bashmsg.nocolor
_bashmsg.defcolor black _bashmsg.nocolor
else
_bashmsg.defcolor red red
_bashmsg.defcolor green green
_bashmsg.defcolor brown brown
_bashmsg.defcolor blue blue
_bashmsg.defcolor magenta magenta
_bashmsg.defcolor cyan cyan
_bashmsg.defcolor white white
_bashmsg.defcolor grey grey
_bashmsg.defcolor redl redl
_bashmsg.defcolor greenl greenl
_bashmsg.defcolor yellow yellow
_bashmsg.defcolor bluel bluel
_bashmsg.defcolor magenta magenta
_bashmsg.defcolor cyanl cyanl
_bashmsg.defcolor whitel whitel
_bashmsg.defcolor black black
_bashmsg.def_bashcols_color orange 202
fi
_bashmsg_col_info=grey
_bashmsg_col_warn=orange
_bashmsg_col_error=red
function bashmsg.def_color {
_bashmsg.def_bashcols_color "$1" "$2"
}
# set_color type=<info|warn|error|...> <col>
function bashmsg.set_color {
local colvar="_bashmsg_col_${1}"
if [[ -z ${!colvar} ]]; then
msg_error "${FUNCNAME}: unknown message type '${1}'"
return 1
fi
if ! _bashmsg.is_color_defined ${2}; then
msg_error "${FUNCNAME}: unknown color '${2}'"
return 1
fi
declare -g "${colvar}"=${2}
}
# def_type <name> <prefix> <default color>
function bashmsg.def_type {
declare -g _bashmsg_col_${1}=${3}
eval "function msg_${1} { printf '%s %s\n' \"\$(_bashmsg.dyncolor ${1} echo -n \"${2}\")\" \"\${*}\" ; }"
}
# Default types
bashmsg.def_type info '[info]' grey
bashmsg.def_type warn '[warn]' orange
bashmsg.def_type error '[error]' red
_bashlib_bashmsg=1