bashmsg: add def_type
This commit is contained in:
29
bashmsg.inc
29
bashmsg.inc
@@ -26,6 +26,10 @@ function _bashmsg.def_bashcols_color {
|
||||
fi
|
||||
}
|
||||
|
||||
function _bashmsg.is_color_defined {
|
||||
PATH= command -v _bashmsg.${1} >/dev/null
|
||||
}
|
||||
|
||||
################# INITIALIZATION ################
|
||||
|
||||
if [[ -z $_bashlib_bashcols ]]; then
|
||||
@@ -75,33 +79,30 @@ function bashmsg.def_color {
|
||||
_bashmsg.def_bashcols_color "$1" "$2"
|
||||
}
|
||||
|
||||
# set_color <infp|warn|error> <col>
|
||||
# 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 ! $(PATH= command -v _bashmsg.${2} >/dev/null); then
|
||||
if ! _bashmsg.is_color_defined ${2}; then
|
||||
msg_error "${FUNCNAME}: unknown color '${2}'"
|
||||
return 1
|
||||
fi
|
||||
declare -g "_bashmsg_col_${1}"=${2}
|
||||
declare -g "${colvar}"=${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] $*"
|
||||
# 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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user