Files
bashlib/bashmsg.inc
2021-07-28 00:27:40 +02:00

108 lines
2.7 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
}
################# 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 <infp|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 ! $(PATH= command -v _bashmsg.${2} >/dev/null); then
msg_error "${FUNCNAME}: unknown color '${2}'"
return 1
fi
declare -g "_bashmsg_col_${1}"=${2}
}
function msg_info {
_bashmsg.dyncolor info echo "[info] $*"
}
function msg_warn {
_bashmsg.dyncolor warn echo "[warn] $*"
}
function msg_error {
_bashmsg.dyncolor error echo "[error] $*"
}
_bashlib_bashmsg=1