Compare commits

...

12 Commits

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
cfcb399d20 Add bashparam 2021-07-27 18:56:03 +02:00
Micyp
4183072ea8 add bashutils 2021-07-27 15:32:31 +02:00
e1b576a4b7 fix typos in default color names 2021-02-10 20:39:52 +01:00
f49c74f12b syntax improvements 2021-01-14 10:37:13 +01:00
Micyp
4cf9387cb8 replace shebang with vim modeline, improve syntax 2021-01-13 19:17:58 +01:00
6dbb80d10f bashcols: replace tabs with spaces in default color list 2020-12-09 20:28:25 +01:00
4 changed files with 228 additions and 24 deletions

View File

@@ -1,6 +1,15 @@
#!/bin/bash # vim: syntax=bash:
reset_esc='\e[0m'
# bgcol <color> <command>
# Use the specified color as background
function bgcol {
_bashcols_bgcol_conditionalsuffix=bg
"$@"
_bashcols_bgcol_conditionalsuffix=
}
eval "reset_esc=\"\\e[0m\""
# applies the given color id, executes the command, then resets the color # applies the given color id, executes the command, then resets the color
function colout() { function colout() {
@@ -9,9 +18,13 @@ function colout() {
return return
fi fi
local color=$1 local color="$1"
shift shift
tput setaf $color if [[ -z $_bashcols_bgcol_conditionalsuffix ]]; then
tput setaf "$color"
else
tput setab "$color"
fi
"$@" "$@"
tput sgr0 tput sgr0
} }
@@ -37,10 +50,10 @@ function defcol() {
function dispcollist() { function dispcollist() {
local width=${1:-16} local width=${1:-16}
local idx=0 local idx=0
for colid in `seq 1 256`; do for colid in {1..256}; do
colout $colid printf "%3s " $colid colout $colid printf "%3s " $colid
idx=$(($idx+1)) let "idx++" || true
if [[ $(($idx % $width)) -eq 0 ]]; then if ! (( $idx % $width )); then
printf "\n" printf "\n"
fi fi
done done
@@ -49,24 +62,26 @@ function dispcollist() {
function colthis { function colthis {
local name="$1" local name="$1"
shift shift
eval "printf \"\${${name}_esc}$@$reset_esc\"" eval "printf \"%s\" \"\${${name}_esc${_bashcols_bgcol_conditionalsuffix}}$@$reset_esc\""
} }
# default colors # default colors
defcol red 1 defcol red 1
defcol green 2 defcol green 2
defcol brown 3 defcol brown 3
defcol blue 4 defcol blue 4
defcol magenta 5 defcol magenta 5
defcol cyan 6 defcol cyan 6
defcol white 7 defcol white 7
defcol grey 8 defcol grey 8
defcol redl 9 defcol redl 9
defcol greenl 10 defcol greenl 10
defcol yellow 11 defcol yellow 11
defcol bluel 12 defcol bluel 12
defcol magenta 13 defcol magenta 13
defcol cyanl 14 defcol cyanl 14
defcol whitel 15 defcol whitel 15
defcol black 16 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

23
bashparam.inc Normal file
View File

@@ -0,0 +1,23 @@
#!/hint/bash
declare -A __params
function has_param {
[[ -z ${__params[$1]} ]]
}
function get_param {
echo "${__params[$1]}"
}
# set_param <longname> <value>
function set_param {
if [[ -z $2 ]]; then
>&2 echo "${FUNCNAME}: non-zero value expected"
return 1
fi
$__params[${1:2}]=${2}
}
_bashlib_bashparam=1

16
bashutils.inc Normal file
View File

@@ -0,0 +1,16 @@
#!/hint/bash
function shasum {
command sha256sum <<<"$1" | awk '{ print $1 }'
}
# once <command>
function once {
local flagname="__onceflag_$(shasum "$*")"
if [[ -z ${!flagname} ]]; then
declare -g "${flagname}"=1
"$@"
fi
}