bashmsg: prompt support

This commit is contained in:
2022-02-13 13:50:37 +01:00
parent 80b48702e8
commit 8ae8018c43

View File

@@ -30,6 +30,38 @@ function _bashmsg.is_color_defined {
PATH= command -v _bashmsg.${1} >/dev/null
}
# prompt_generic <ydefault/ndefault> <prefix> <name> <message>
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
@@ -99,6 +131,16 @@ function bashmsg.def_type {
eval "function msg_${1} { printf '%s %s\n' \"\$(_bashmsg.dyncolor ${1} echo -n \"${2}\")\" \"\${*}\" ; }"
}
# def_prompt_type <name> <prefix> <default color> [<ydefault/ndefault>]
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