Feat: Process all requestd modules before drawing

This commit is contained in:
Narvin Singh 2021-01-02 00:19:55 -05:00
parent 8669361595
commit 1ad7087552
2 changed files with 29 additions and 20 deletions

36
avdd
View File

@ -96,18 +96,22 @@ draw_status() {
# Draw the initial status
draw_status
# If the module value is in the cache, indicating that the module controls
# part of the status bar, execute the module function and redraw the status
# bar if that part of the status bar has changed
process_cmd () {
local -r mod="$1"
if [[ -v stat_cache[${mod}] ]]; then
local -r new_val="$(eval "$(mod_to_fn "${mod}")")"
if [[ "${new_val}" != stat_cache["${mod}"] ]]; then
stat_cache["${mod}"]="${new_val}"
draw_status
# Process a list of mods. If a mod is in the status cache, call the module
# function to get the new value for that section of the status bar. If any
# of the new values are different from the cached ones, update the cache
# and redraw the status bar once after all the mods are processed.
process_mods () {
local mod new_val is_changed
for mod in "$@"; do
if [[ -v stat_cache[${mod}] ]]; then
new_val="$(eval "$(mod_to_fn "${mod}")")"
if [[ "${new_val}" != stat_cache["${mod}"] ]]; then
stat_cache["${mod}"]="${new_val}"
is_changed=1
fi
fi
fi
done
if [[ -v is_changed ]]; then draw_status; fi
}
# Setup the named pipe to receive commands
@ -118,15 +122,19 @@ trap "rm -f ${FIFO}" EXIT
# Each time the pipe is emptied out, the inner while loop will finish, so
# wrap it in an infinte loop to keep blocking until there is data on the pipe
while :; do
while read -r cmd; do
case "${cmd}" in
while read -r fifo_mod_list; do
case "${fifo_mod_list}" in
res_all)
process_mods "${mods[@]}"
;;
res_quit)
exit 0
;;
res_*)
;;
*)
process_cmd "${cmd}"
IFS=', ' read -r -a fifo_mods <<< "${fifo_mod_list}"
process_mods "${fifo_mods[@]}"
;;
esac
done < "${FIFO}"

13
avds
View File

@ -51,29 +51,30 @@ ms_to_s () {
printf '%.3f' "${1}e-3"
}
# Validate the arguments
# Validate the number arguments
if [[ "$#" -lt 1 || "$#" -gt 3 ]]; then
printf '%s' "${USAGE}" 1>&2
exit 128
fi
IFS=', ' read -r -a mods <<< "$1"
mod_list="$1"
# Check if the user needs help
# shellcheck disable=SC2128
if [[ "${mods}" =~ ^(-h|-(-)?help)$ ]]; then
if [[ "${mod_list}" =~ ^(-h|-(-)?help)$ ]]; then
printf '%s' "${USAGE}" 1>&2
exit 0
fi
when="${2:-0}"
repeat="$3"
# Validate when
if [[ ! "${when}" =~ ^[0-9]+|[mhd]$ ]]; then
printf 'Invalid argument <when>: %s\n' "${when}" 1>&2
exit 128
fi
repeat="$3"
# Write to the pipe if this is the first run or if repeat is on
first_run=1
while [[ "${first_run}" -eq 1 || -n "${repeat}" ]]; do
@ -107,6 +108,6 @@ while [[ "${first_run}" -eq 1 || -n "${repeat}" ]]; do
printf 'The daemon %s is not running\n' "${DAEMON}" 1>&2
exit 1
fi
for mod in "${mods[@]}"; do printf '%s\n' "${mod}" >> "${FIFO}"; done
printf '%s\n' "${mod_list}" >> "${FIFO}"
done