Add miscellanious order to the utility functions
This commit is contained in:
parent
18f291fe07
commit
bc88492ac1
170
rowbot
170
rowbot
|
@ -11,87 +11,7 @@ stty -echoctl
|
|||
# utility and helper functions
|
||||
###
|
||||
|
||||
any_file() {
|
||||
local files=( "${1:-.}"/* ) max idx
|
||||
|
||||
if (( ${#files[@]} > 1 )); then
|
||||
(( max = ${#files[@]} - 1 ))
|
||||
idx=$(random 0 "$max")
|
||||
printf %s "${files[$idx]}"
|
||||
fi
|
||||
}
|
||||
|
||||
die() {
|
||||
local fmt=$1
|
||||
shift
|
||||
# This is a wrapper around printf so the format string isn't known ahead of
|
||||
# time.
|
||||
# shellcheck disable=SC2059
|
||||
printf "FATAL: $fmt\n" "$@" >&2
|
||||
exit "${STATUS:-42}"
|
||||
}
|
||||
|
||||
get_array() {
|
||||
local entry idx_size
|
||||
declare -n scalar=ARR_${1^^}
|
||||
declare -n array=$1
|
||||
|
||||
while read -r entry; do
|
||||
idx_size=${entry%%:*} entry=${entry#"$idx_size":}
|
||||
array[${entry:0:idx_size}]=${entry:idx_size}
|
||||
done <<< "$scalar"
|
||||
}
|
||||
|
||||
get_option() {
|
||||
if (( ! $# )); then
|
||||
return 1
|
||||
fi
|
||||
|
||||
local var_name=${1//-/_}
|
||||
local env_var=${var_name^^}
|
||||
|
||||
if [[ -v $env_var ]]; then
|
||||
config[$1]=${!env_var}
|
||||
elif [[ -v opts[$1] ]]; then
|
||||
config[$1]=${opts[$1]}
|
||||
elif [[ -v $var_name ]]; then
|
||||
config[$1]=${!var_name}
|
||||
elif (( $# > 1 )); then
|
||||
config[$1]=$2
|
||||
fi
|
||||
}
|
||||
|
||||
has() {
|
||||
if (( $# )); then
|
||||
hash "$1" 2>/dev/null
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
is_action() {
|
||||
[[ ${msg[cmd]} = PRIVMSG && ${msg_args[-1]:0:${#config[trigger]}} = "${config[trigger]}" ]]
|
||||
}
|
||||
|
||||
is_chan() {
|
||||
[[ ${msg_args[${1:-0}]:0:1} = \# ]]
|
||||
}
|
||||
|
||||
is_good_variable() {
|
||||
[[ $1 =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]
|
||||
}
|
||||
|
||||
is_parent() {
|
||||
(( BASHPID == $$ ))
|
||||
}
|
||||
|
||||
is_reloaded() {
|
||||
[[ $RELOADED = yes ]] || (( LORE_LIVES > 1 ))
|
||||
}
|
||||
|
||||
is_running () {
|
||||
kill -0 "$1" 2>/dev/null
|
||||
}
|
||||
# cerealizers
|
||||
|
||||
put_array() {
|
||||
# The variable named array is a nameref to an array
|
||||
|
@ -108,11 +28,99 @@ put_array() {
|
|||
export "ARR_${1^^}"
|
||||
}
|
||||
|
||||
get_array() {
|
||||
declare -n scalar=ARR_${1^^}
|
||||
declare -n array=$1
|
||||
local entry idx_size
|
||||
|
||||
while read -r entry; do
|
||||
idx_size=${entry%%:*} entry=${entry#"$idx_size":}
|
||||
array[${entry:0:idx_size}]=${entry:idx_size}
|
||||
done <<< "$scalar"
|
||||
}
|
||||
|
||||
is_good_variable() {
|
||||
[[ $1 =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]
|
||||
}
|
||||
|
||||
# code reloading helpers
|
||||
|
||||
get_option() {
|
||||
local var_name=${1//-/_}
|
||||
local env_var=${var_name^^}
|
||||
|
||||
if [[ ! -v config[$1] || -v OPT_OW ]]; then
|
||||
if [[ -v $var_name ]]; then
|
||||
config[$1]=${!var_name}
|
||||
elif [[ -v $env_var ]]; then
|
||||
config[$1]=${!env_var}
|
||||
elif [[ -v opts[$1] ]]; then
|
||||
config[$1]=${opts[$1]}
|
||||
elif (( $# > 1 )); then
|
||||
config[$1]=$2
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
is_parent() {
|
||||
(( BASHPID == $$ ))
|
||||
}
|
||||
|
||||
is_reloaded() {
|
||||
[[ $RELOADED = yes ]] || (( LORE_LIVES > 1 ))
|
||||
}
|
||||
|
||||
# message classification
|
||||
|
||||
is_action() {
|
||||
[[ ${msg[cmd]} = PRIVMSG && ${msg_args[-1]:0:${#config[trigger]}} = "${config[trigger]}" ]]
|
||||
}
|
||||
|
||||
is_chan() {
|
||||
[[ ${msg_args[${1:-0}]:0:1} = \# ]]
|
||||
}
|
||||
|
||||
# cryptography secure (almost probably) random number generators
|
||||
|
||||
random() {
|
||||
local min=$1 max=$2
|
||||
printf %d "$(( (RANDOM % max) + min ))"
|
||||
}
|
||||
|
||||
any_file() {
|
||||
local files=( "${1:-.}"/* ) max idx
|
||||
|
||||
if (( ${#files[@]} > 1 )); then
|
||||
(( max = ${#files[@]} - 1 ))
|
||||
idx=$(random 0 "$max")
|
||||
printf %s "${files[$idx]}"
|
||||
fi
|
||||
}
|
||||
|
||||
# misc
|
||||
|
||||
is_running () {
|
||||
kill -0 "$1" 2>/dev/null
|
||||
}
|
||||
|
||||
die() {
|
||||
local fmt=$1
|
||||
shift
|
||||
# This is a wrapper around printf so the format string isn't known ahead of
|
||||
# time.
|
||||
# shellcheck disable=SC2059
|
||||
printf "FATAL: $fmt\n" "$@" >&2
|
||||
exit "${STATUS:-42}"
|
||||
}
|
||||
|
||||
has() {
|
||||
if (( $# )); then
|
||||
hash "$1" 2>/dev/null
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
run_callbacks() {
|
||||
if (( ! $# )); then
|
||||
return 1
|
||||
|
|
Loading…
Reference in New Issue