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
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _bashmsg.is_color_defined {
|
||||||
|
PATH= command -v _bashmsg.${1} >/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
################# INITIALIZATION ################
|
################# INITIALIZATION ################
|
||||||
|
|
||||||
if [[ -z $_bashlib_bashcols ]]; then
|
if [[ -z $_bashlib_bashcols ]]; then
|
||||||
@@ -75,33 +79,30 @@ function bashmsg.def_color {
|
|||||||
_bashmsg.def_bashcols_color "$1" "$2"
|
_bashmsg.def_bashcols_color "$1" "$2"
|
||||||
}
|
}
|
||||||
|
|
||||||
# set_color <infp|warn|error> <col>
|
# set_color type=<info|warn|error|...> <col>
|
||||||
function bashmsg.set_color {
|
function bashmsg.set_color {
|
||||||
local colvar="_bashmsg_col_${1}"
|
local colvar="_bashmsg_col_${1}"
|
||||||
if [[ -z ${!colvar} ]]; then
|
if [[ -z ${!colvar} ]]; then
|
||||||
msg_error "${FUNCNAME}: unknown message type '${1}'"
|
msg_error "${FUNCNAME}: unknown message type '${1}'"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
if ! $(PATH= command -v _bashmsg.${2} >/dev/null); then
|
if ! _bashmsg.is_color_defined ${2}; then
|
||||||
msg_error "${FUNCNAME}: unknown color '${2}'"
|
msg_error "${FUNCNAME}: unknown color '${2}'"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
declare -g "_bashmsg_col_${1}"=${2}
|
declare -g "${colvar}"=${2}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# def_type <name> <prefix> <default color>
|
||||||
function msg_info {
|
function bashmsg.def_type {
|
||||||
_bashmsg.dyncolor info echo "[info] $*"
|
declare -g _bashmsg_col_${1}=${3}
|
||||||
}
|
eval "function msg_${1} { printf '%s %s\n' \"\$(_bashmsg.dyncolor ${1} echo -n \"${2}\")\" \"\${*}\" ; }"
|
||||||
|
|
||||||
function msg_warn {
|
|
||||||
_bashmsg.dyncolor warn echo "[warn] $*"
|
|
||||||
}
|
|
||||||
|
|
||||||
function msg_error {
|
|
||||||
_bashmsg.dyncolor error echo "[error] $*"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Default types
|
||||||
|
bashmsg.def_type info '[info]' grey
|
||||||
|
bashmsg.def_type warn '[warn]' orange
|
||||||
|
bashmsg.def_type error '[error]' red
|
||||||
|
|
||||||
_bashlib_bashmsg=1
|
_bashlib_bashmsg=1
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user