Compare commits

6 Commits
master ... dev

Author SHA1 Message Date
8ae8018c43 bashmsg: prompt support 2022-02-13 13:50:37 +01:00
80b48702e8 use sha256sum for shasum 2021-10-25 23:17:42 +02:00
7ab0ebda17 bashmsg: add def_type 2021-07-28 03:13:39 +02:00
2573380c21 Add .inc extension to libs 2021-07-28 00:27:40 +02:00
b7a11fc4ee bashmsg: initial 2021-07-28 00:23:40 +02:00
4a96947caf bashcols: add background support 2021-07-28 00:23:28 +02:00
4 changed files with 172 additions and 6 deletions

View File

@@ -2,6 +2,15 @@
reset_esc='\e[0m'
# bgcol <color> <command>
# Use the specified color as background
function bgcol {
_bashcols_bgcol_conditionalsuffix=bg
"$@"
_bashcols_bgcol_conditionalsuffix=
}
# applies the given color id, executes the command, then resets the color
function colout() {
if [[ $# -eq 0 ]]; then
@@ -11,7 +20,11 @@ function colout() {
local color="$1"
shift
tput setaf "$color"
if [[ -z $_bashcols_bgcol_conditionalsuffix ]]; then
tput setaf "$color"
else
tput setab "$color"
fi
"$@"
tput sgr0
}
@@ -39,8 +52,8 @@ function dispcollist() {
local idx=0
for colid in {1..256}; do
colout $colid printf "%3s " $colid
idx=$(($idx+1))
if [[ $(($idx % $width)) -eq 0 ]]; then
let "idx++" || true
if ! (( $idx % $width )); then
printf "\n"
fi
done
@@ -49,7 +62,7 @@ function dispcollist() {
function colthis {
local name="$1"
shift
eval "printf \"%s\" \"\${${name}_esc}$@$reset_esc\""
eval "printf \"%s\" \"\${${name}_esc${_bashcols_bgcol_conditionalsuffix}}$@$reset_esc\""
}
# default colors
@@ -70,3 +83,5 @@ defcol cyanl 14
defcol whitel 15
defcol black 16
_bashlib_bashcols=1

150
bashmsg.inc Normal file
View File

@@ -0,0 +1,150 @@
#!/hint/bash
# defcolor <color name> <function>
function _bashmsg.defcolor {
local col=$1
local fn=$2
eval "function _bashmsg.${col} { ${fn} \${@}; }"
}
# dyncolor <type> <command>
function _bashmsg.dyncolor {
local typevar="_bashmsg_col_${1}"
local col=${!typevar}
shift
_bashmsg.${col} "$@"
}
# def_bashcols_color <color name> <color id>
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 <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
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=<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 ! _bashmsg.is_color_defined ${2}; then
msg_error "${FUNCNAME}: unknown color '${2}'"
return 1
fi
declare -g "${colvar}"=${2}
}
# 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}\")\" \"\${*}\" ; }"
}
# 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
bashmsg.def_type error '[error]' red
_bashlib_bashmsg=1

View File

@@ -1,4 +1,4 @@
#!/usr/bin/bash
#!/hint/bash
declare -A __params
@@ -20,3 +20,4 @@ function set_param {
}
_bashlib_bashparam=1

View File

@@ -1,7 +1,7 @@
#!/hint/bash
function shasum {
command shasum <<<"$1" | awk '{ print $1 }'
command sha256sum <<<"$1" | awk '{ print $1 }'
}
# once <command>