Move state management into its own method

This commit is contained in:
Nick Chambers 2021-11-22 00:44:48 -06:00
parent d07ad42320
commit 1524b56fae
1 changed files with 23 additions and 33 deletions

56
rowbot
View File

@ -320,23 +320,28 @@ unset key opts old_set file new_set new found old setting
# state management
###
state_resolve() {
local ns=${NS-global} managed found=0
declare -gA __rowbot_state_store_"$ns"
declare -n ns_config=__rowbot_state_store_"$ns"
state_manage() {
local managed found=0
declare -gA __rowbot_state_store_"$1"
if [[ $ns != global ]]; then
if [[ $1 != global ]]; then
for managed in "${states_managed[@]}"; do
if [[ $managed = "$ns" ]]; then
if [[ $managed = "$1" ]]; then
found=1
break
fi
done
if (( !found )); then
states_managed+=( "$ns" )
states_managed+=( "$1" )
fi
fi
}
state_resolve() {
local ns=${NS-global}
declare -n ns_config=__rowbot_state_store_"$ns"
state_manage "$ns"
if [[ -v config[$1] ]]; then
ns_config[$1]=${config[$1]}
@ -348,40 +353,25 @@ state_resolve() {
}
state_put() {
local ns=${NS-global} managed found=0
declare -gA __rowbot_state_store_"$ns"
if [[ $ns != global ]]; then
for managed in "${states_managed[@]}"; do
if [[ $managed = "$ns" ]]; then
found=1
break
fi
done
if (( !found )); then
states_managed+=( "$ns" )
fi
fi
# The `ns_config` variable is a reference to an array
# shellcheck disable=SC2178
declare -n ns_config=__rowbot_state_store_"$ns"
ns_config[$1]=$2
}
state_get() {
local ns=${NS-global}
# The `ns_config` variable is a reference to an array
# shellcheck disable=SC2178
declare -n ns_config=__rowbot_state_store_"$ns"
state_manage "$ns"
ns_config[$1]=$2
}
if [[ -v ns_config["$1"] ]]; then
state_get() {
# The `ns_config` variable is a reference to an array
# shellcheck disable=SC2178
declare -n ns_config=__rowbot_state_store_"${NS-global}"
if [[ -v ns_config[$1] ]]; then
if [[ ! -v QUIET || $QUIET = no ]]; then
printf %s "${ns_config["$1"]}"
printf %s "${ns_config[$1]}"
fi
if [[ ${ns_config["$1"]} = no ]]; then
if [[ ${ns_config[$1]} = no ]]; then
return 1
fi
elif [[ -v DEFAULT ]]; then