Refactor: Lifted code outside of main function
It seemed like overkill to enclose the entire script in a function just so readonly variables would work correctly.
This commit is contained in:
parent
7a2f2bbf08
commit
bf2f67a28d
81
xrsbd
81
xrsbd
@ -1,9 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Global readonly variables can't be shadowed by local variables so wrap
|
USAGE="
|
||||||
# our code in a function so we can declare all variables local
|
|
||||||
main() {
|
|
||||||
local -r USAGE="
|
|
||||||
USAGE: xrsbd [<mod_list>='cpu mem bl vol-amixer bat dt']
|
USAGE: xrsbd [<mod_list>='cpu mem bl vol-amixer bat dt']
|
||||||
[<pre>=' '] [<sep_l>='| '] [<sep_r>=' '] [<suf>=' ']
|
[<pre>=' '] [<sep_l>='| '] [<sep_r>=' '] [<suf>=' ']
|
||||||
|
|
||||||
@ -39,31 +36,24 @@ main() {
|
|||||||
from the output, so if you want them, simply include them in the
|
from the output, so if you want them, simply include them in the
|
||||||
prefix and suffix as shown here.
|
prefix and suffix as shown here.
|
||||||
|
|
||||||
xrsbd 'vol-amixer dt' '[<' '<' '>' '>]' &"
|
xrsbd 'vol-amixer dt' '[<' '<' '>' '>]' &
|
||||||
|
"
|
||||||
# Customizable configuration constants
|
# Customizable configuration constants
|
||||||
local -r DEFAULT_MOD_LIST='cpu mem bl vol-amixer bat dt'
|
DEFAULT_MOD_LIST='cpu mem bl vol-amixer bat dt'
|
||||||
local -r DEFAULT_PRE=' '
|
DEFAULT_PRE=' '
|
||||||
local -r DEFAULT_SEP_L='| '
|
DEFAULT_SEP_L='| '
|
||||||
local -r DEFAULT_SEP_R=' '
|
DEFAULT_SEP_R=' '
|
||||||
local -r DEFAULT_SUF=' '
|
DEFAULT_SUF=' '
|
||||||
|
|
||||||
local -r mod_list="${1-${DEFAULT_MOD_LIST}}"
|
mod_list="${1-${DEFAULT_MOD_LIST}}"
|
||||||
local -r pre="${2-${DEFAULT_PRE}}"
|
pre="${2-${DEFAULT_PRE}}"
|
||||||
local -r sep_l="${3-${DEFAULT_SEP_L}}"
|
sep_l="${3-${DEFAULT_SEP_L}}"
|
||||||
local -r sep_r="${4-${DEFAULT_SEP_R}}"
|
sep_r="${4-${DEFAULT_SEP_R}}"
|
||||||
local -r suf="${5-${DEFAULT_SUF}}"
|
suf="${5-${DEFAULT_SUF}}"
|
||||||
|
|
||||||
local -r MOD_DIR="$(dirname "$0")"/module
|
MOD_DIR="$(dirname "$0")"/module
|
||||||
local -r ACTION_DIR=/tmp/xrsb-action
|
ACTION_DIR=/tmp/xrsb-action
|
||||||
local -i ACTION_DIR_LEN=${#ACTION_DIR}
|
ACTION_DIR_LEN=${#ACTION_DIR}
|
||||||
|
|
||||||
# Cache module values so we can reuse them without recomputing them
|
|
||||||
local -A stat_cache
|
|
||||||
# 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_cache_ordered_mods mods
|
|
||||||
local mod mod_file
|
|
||||||
|
|
||||||
# Map the module file name to the module function
|
# Map the module file name to the module function
|
||||||
mod_to_fn() {
|
mod_to_fn() {
|
||||||
@ -72,12 +62,13 @@ main() {
|
|||||||
|
|
||||||
# Check if the user needs help
|
# Check if the user needs help
|
||||||
if [[ "${mod_list}" =~ ^(-h|-(-)?help)$ ]]; then
|
if [[ "${mod_list}" =~ ^(-h|-(-)?help)$ ]]; then
|
||||||
printf '%s\n' "${USAGE}" 1>&2
|
printf '%s' "${USAGE}" 1>&2
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# For each module in the list, if the module file exists then source it, add
|
# For each module in the list, if the module file exists then source it,
|
||||||
# its name to the ordered array, and call its function and cache the value
|
# add its name to the ordered array, and call its function and cache the value
|
||||||
|
declare -A stat_cache
|
||||||
IFS=', ' read -r -a mods <<< "${mod_list}"
|
IFS=', ' read -r -a mods <<< "${mod_list}"
|
||||||
for mod in "${mods[@]}"; do
|
for mod in "${mods[@]}"; do
|
||||||
mod_file="${MOD_DIR}/${mod}"
|
mod_file="${MOD_DIR}/${mod}"
|
||||||
@ -97,8 +88,8 @@ main() {
|
|||||||
"${stat}" "${sep_l}" "${stat_cache[${mod}]}" "${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 display
|
||||||
# display the status
|
# the status
|
||||||
local -ri offset=${#sep_l}
|
local -ri offset=${#sep_l}
|
||||||
local -ri len=$((${#stat} - offset - ${#sep_r}))
|
local -ri len=$((${#stat} - offset - ${#sep_r}))
|
||||||
xsetroot -name "${pre}${stat:${offset}:${len}}${suf}"
|
xsetroot -name "${pre}${stat:${offset}:${len}}${suf}"
|
||||||
@ -107,8 +98,8 @@ 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
|
# For each file in the action directory, remove the file, and if a module
|
||||||
# the action is cached, call the module function and update the cache. If
|
# for the action is cached, call the module function and update the cache. If
|
||||||
# any cache entries were updated, redraw the status.
|
# any cache entries were updated, redraw the status.
|
||||||
process_signal () {
|
process_signal () {
|
||||||
local -a action_paths
|
local -a action_paths
|
||||||
@ -130,24 +121,20 @@ main() {
|
|||||||
trap process_signal SIGUSR1
|
trap process_signal SIGUSR1
|
||||||
|
|
||||||
# Wait for signals efficiently. In a loop begin a long-running sleep command
|
# Wait for signals efficiently. In a loop begin a long-running sleep command
|
||||||
# in the background, then wait on it. If we trap a signal before the wait
|
# in the background, then wait on it. If we trap a signal before the wait is
|
||||||
# is over and sleep is still running, trap will call process_signal, then
|
# over and sleep is still running, trap will call process_signal, then code
|
||||||
# code execution will resume at the line after the wait statement. So on
|
# execution will resume at the line after the wait statement. So on that line
|
||||||
# that line we kill the (probably) still running sleep command so they
|
# we kill the (probably) still running sleep command so they don't pile up,
|
||||||
# don't pile up, and loop to sleep and wait for the next signal. If we
|
# and loop to sleep and wait for the next signal. If we don't trap a signal
|
||||||
# don't trap a signal during the long running sleep, then the wait ends,
|
# during the long running sleep, then the wait ends, we try to kill the
|
||||||
# we try to kill the sleep command that has already exited, so it doesn't
|
# sleep command that has already exited, so it doesn't matter, and loop to
|
||||||
# matter, and loop to sleep and wait again. Note that we don't make the
|
# sleep and wait again. Note that we don't make the sleep too long because
|
||||||
# sleep too long because if the daemon is killed, the sleep will become
|
# if the daemon is killed, the sleep will become an orphaned process until
|
||||||
# an orphaned process until the sleep period elapses.
|
# the sleep period elapses.
|
||||||
local -i sleep_pid
|
|
||||||
while :; do
|
while :; do
|
||||||
sleep 30m &
|
sleep 30m &
|
||||||
sleep_pid="$!"
|
sleep_pid="$!"
|
||||||
wait "${sleep_pid}"
|
wait "${sleep_pid}"
|
||||||
kill "${sleep_pid}" 2>/dev/null
|
kill "${sleep_pid}" 2>/dev/null
|
||||||
done
|
done
|
||||||
}
|
|
||||||
|
|
||||||
main "$@"
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user