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
# by mod_list) of the keys so we can loop over the cache in display order
# when generating the full status
local -a stat_ordered_keys
local mod mod_file key
local -a stat_cache_ordered_mods
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
# Check if the module file exists
mod_file="${MOD_DIR}/${mod}"
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
source "${mod_file}"
key="${mod//-/_}"
stat_ordered_keys+=("${key}")
stat_cache["${key}"]="$("mod_${key}")"
stat_cache_ordered_mods+=("${mod}")
stat_cache["${mod}"]="$("$(mod_to_fn "${mod}")")"
fi
done
draw_status() {
local stat
# Construct the status by looping over the cached values in display order
for key in "${stat_ordered_keys[@]}"; do
# Construct and display the status by looping over the cached values in order
draw_status() {
local mod stat
for mod in "${stat_cache_ordered_mods[@]}"; do
printf -v stat '%b%b%b%b' \
"${stat}" "${sep_l}" "${stat_cache[${key}]}" "${sep_r}"
"${stat}" "${sep_l}" "${stat_cache[${mod}]}" "${sep_r}"
done
# Trim the leading left separator and trailing right separator, and
@ -64,25 +66,22 @@ main() {
# Draw the initial 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 () {
local -a actions
local path do_redraw
# Get the actions and purge the action directory
readarray -d '' actions < \
local -a action_paths
local action_path mod is_changed
readarray -d '' action_paths< \
<(find "${ACTION_DIR}" -maxdepth 1 -type f -exec rm -f {} + -print0)
# Process each action
for path in "${actions[@]}"; do
key="${path:$((ACTION_DIR_LEN + 1))}"
# Call the module function if the cache entry for the module is defined
if [[ -v stat_cache[${key}] ]]; then
stat_cache["${key}"]="$("mod_${key}")"
do_redraw=1
for action_path in "${action_paths[@]}"; do
mod="${action_path:$((ACTION_DIR_LEN + 1))}"
if [[ -v stat_cache[${mod}] ]]; then
stat_cache["${mod}"]="$("$(mod_to_fn "${mod}")")"
is_changed=1
fi
done
if [[ -v do_redraw ]]; then draw_status; fi
if [[ -v is_changed ]]; then draw_status; fi
}
# Wait for signals