Refactor: Make variable names more descriptive

Change variable names and revise and consolidate comments so the code
is clearer.
This commit is contained in:
Narvin Singh 2020-12-29 11:13:34 -05:00
parent 0e8ca10665
commit 8a512754e5

57
xrsbd
View File

@ -27,31 +27,33 @@ main() {
# Since stat_cache is hash ordered, maintain the display order (as defined # Since stat_cache is hash ordered, maintain the display order (as defined
# by mod_list) of the keys so we can loop over the cache in display order # by mod_list) of the keys so we can loop over the cache in display order
# when generating the full status # when generating the full status
local -a stat_ordered_keys local -a stat_cache_ordered_mods
local mod mod_file key local mod mod_file
# Process the module list # Map the module file name to the module function
mod_to_fn() {
printf 'mod_%s' "${1//-/_}"
}
# For each module in the list, if the module file exists then source it, add
# its name to the ordered array, and call its function and cache the value
for mod in ${mod_list}; do for mod in ${mod_list}; do
# Check if the module file exists
mod_file="${MOD_DIR}/${mod}" mod_file="${MOD_DIR}/${mod}"
if [[ -r "${mod_file}" ]]; then if [[ -r "${mod_file}" ]]; then
# Source the module, compute its key and add it to the ordered array,
# and call the module function and store the value in the cache
# shellcheck source=/dev/null # shellcheck source=/dev/null
source "${mod_file}" source "${mod_file}"
key="${mod//-/_}" stat_cache_ordered_mods+=("${mod}")
stat_ordered_keys+=("${key}") stat_cache["${mod}"]="$("$(mod_to_fn "${mod}")")"
stat_cache["${key}"]="$("mod_${key}")"
fi fi
done done
draw_status() {
local stat
# Construct the status by looping over the cached values in display order # Construct and display the status by looping over the cached values in order
for key in "${stat_ordered_keys[@]}"; do draw_status() {
local mod stat
for mod in "${stat_cache_ordered_mods[@]}"; do
printf -v stat '%b%b%b%b' \ printf -v stat '%b%b%b%b' \
"${stat}" "${sep_l}" "${stat_cache[${key}]}" "${sep_r}" "${stat}" "${sep_l}" "${stat_cache[${mod}]}" "${sep_r}"
done done
# Trim the leading left separator and trailing right separator, and # Trim the leading left separator and trailing right separator, and
@ -64,25 +66,22 @@ main() {
# Draw the initial status # Draw the initial status
draw_status draw_status
# For each file in the action directory, remove the file, and if a module for
# the action is cached, call the module function and update the cache. If
# any cache entries were updated, redraw the status.
process_signal () { process_signal () {
local -a actions local -a action_paths
local path do_redraw local action_path mod is_changed
readarray -d '' action_paths< \
# Get the actions and purge the action directory
readarray -d '' actions < \
<(find "${ACTION_DIR}" -maxdepth 1 -type f -exec rm -f {} + -print0) <(find "${ACTION_DIR}" -maxdepth 1 -type f -exec rm -f {} + -print0)
for action_path in "${action_paths[@]}"; do
# Process each action mod="${action_path:$((ACTION_DIR_LEN + 1))}"
for path in "${actions[@]}"; do if [[ -v stat_cache[${mod}] ]]; then
key="${path:$((ACTION_DIR_LEN + 1))}" stat_cache["${mod}"]="$("$(mod_to_fn "${mod}")")"
# Call the module function if the cache entry for the module is defined is_changed=1
if [[ -v stat_cache[${key}] ]]; then
stat_cache["${key}"]="$("mod_${key}")"
do_redraw=1
fi fi
done done
if [[ -v is_changed ]]; then draw_status; fi
if [[ -v do_redraw ]]; then draw_status; fi
} }
# Wait for signals # Wait for signals