#!/hint/bash # defcolor function _bashmsg.defcolor { local col=$1 local fn=$2 eval "function _bashmsg.${col} { ${fn} \${@}; }" } # dyncolor function _bashmsg.dyncolor { local typevar="_bashmsg_col_${1}" local col=${!typevar} shift _bashmsg.${col} "$@" } # def_bashcols_color 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 } # prompt_generic function _bashmsg.prompt_generic { local mode=$1 local pref=$2 local name=$3 local msg=$4 local msg_w_pref=$(printf '%s %s' "$(_bashmsg.dyncolor "_p_${name}" echo -n "${pref}")" "${msg}") case "$mode" in ydefault) if [[ -n $ZSH_VERSION ]]; then read -r "?${msg_w_pref} [Y/n] " CHOICE else read -r -p "${msg_w_pref} [Y/n] " CHOICE fi [[ -z $CHOICE || $CHOICE =~ ^[yY]$ ]] ;; ndefault) if [[ -n $ZSH_VERSION ]]; then read -r "?${msg_w_pref} [y/N] " CHOICE else read -r -p "${msg_w_pref} [y/N] " CHOICE fi [[ $CHOICE =~ ^[yY]$ ]] ;; *) msg_error "Unknown prompt mode: ${mode}" return 1 ;; esac } ################# 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= 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 function bashmsg.def_type { declare -g _bashmsg_col_${1}=${3} eval "function msg_${1} { printf '%s %s\n' \"\$(_bashmsg.dyncolor ${1} echo -n \"${2}\")\" \"\${*}\" ; }" } # def_prompt_type [] function bashmsg.def_prompt_type { declare -g _bashmsg_col__p_${1}=${3} if [[ -z $4 ]]; then eval "function prompt_${1} { _bashmsg.prompt_generic \"\${1}\" \"${2}\" \"${1}\" \"\${2}\" ; }" else eval "function prompt_${1} { _bashmsg.prompt_generic \"${4}\" \"${2}\" \"${1}\" \"\${1}\" ; }" fi } # Default types bashmsg.def_type info '[info]' grey bashmsg.def_type warn '[warn]' orange bashmsg.def_type error '[error]' red _bashlib_bashmsg=1