From 7ab0ebda1717b69e9745e1af90335ad9665344e6 Mon Sep 17 00:00:00 2001 From: fabs Date: Wed, 28 Jul 2021 03:13:39 +0200 Subject: [PATCH] bashmsg: add def_type --- bashmsg.inc | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/bashmsg.inc b/bashmsg.inc index bb8a5fc..be339f0 100644 --- a/bashmsg.inc +++ b/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 +# set_color type= 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 +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