Feat: Daemon
This commit is contained in:
parent
08dc3a58d3
commit
8471ea66c0
146
status-bar
146
status-bar
@ -1,146 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Configurable variables
|
||||
SEP=' | '
|
||||
|
||||
CPU_ON=1
|
||||
CPU_SYM=
|
||||
|
||||
MEM_ON=1
|
||||
MEM_SYM=
|
||||
|
||||
BL_ON=1
|
||||
BL_SYM=
|
||||
|
||||
VOL_ON=1
|
||||
VOL_SYM_LOW=
|
||||
VOL_SYM_HI=
|
||||
VOL_SYM_MUTE=
|
||||
|
||||
BAT_ON=1
|
||||
BAT_SYM=
|
||||
BAT_SYM_PLG=
|
||||
BAT_SYM_UNK=
|
||||
BAT_CNT=1
|
||||
|
||||
DT_ON=1
|
||||
DT_SYM=''
|
||||
DT_FMT='%a %d %I:%M%p'
|
||||
|
||||
status_cpu() {
|
||||
ps --no-headers -eo %cpu | awk '{cpu = cpu + $1} END {print cpu}'
|
||||
}
|
||||
|
||||
status_mem() {
|
||||
printf '%.0f%%' "$(free | awk 'NR == 2 {print $3 * 100 / $2}')"
|
||||
}
|
||||
|
||||
status_bl () {
|
||||
local bl
|
||||
local max_bl
|
||||
|
||||
printf -v bl '%s' \
|
||||
"$(find /sys/class/backlight/*/brightness | head -n 1 | xargs cat)"
|
||||
printf -v max_bl '%s' \
|
||||
"$(find /sys/class/backlight/*/max_brightness | head -n 1 | xargs cat)"
|
||||
# Multiply by 10^3 inside the arithemetic expansion so the integer arithmetic
|
||||
# will preserve 3 digits after the decimal point. Applying e-3 would provide
|
||||
# 3 decimal points for printf, but since we want the result scaled by 100, we
|
||||
# only apply e-1. printf will round the result to the specified presion.
|
||||
printf '%.0f%%' "$((bl * 1000 / max_bl))e-1"
|
||||
}
|
||||
|
||||
status_vol () {
|
||||
printf '%s%%' \
|
||||
"$(amixer get Master | tail -n 1 | sed -E 's/.*\[(.*)%\].*/\1/')"
|
||||
}
|
||||
|
||||
status_vol_state() {
|
||||
local -r vol="${1:-0}"
|
||||
local state
|
||||
|
||||
printf -v state '%s' \
|
||||
"$(amixer get Master | tail -n 1 | sed -E 's/.*\[(.*)\]/\1/')"
|
||||
case "${state}" in
|
||||
on)
|
||||
if [[ "${vol}" -lt 50 ]]; then printf '%s' "${VOL_SYM_LOW}";
|
||||
else printf '%s' "${VOL_SYM_HI}"; fi
|
||||
;;
|
||||
*)
|
||||
printf '%s' "${VOL_SYM_MUTE}"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
status_bat_cap () {
|
||||
printf '%s' "$(find /sys/class/power_supply/BAT?/capacity \
|
||||
| head -n "${BAT_CNT}" | xargs cat | tr '\n' '% ' | sed 's/ *$//')"
|
||||
}
|
||||
|
||||
status_bat_state() {
|
||||
local state
|
||||
|
||||
printf -v state '%s' \
|
||||
"$(find /sys/class/power_supply/BAT?/status | head -n 1 | xargs cat)"
|
||||
case "${state}" in
|
||||
Discharging)
|
||||
printf '%s' "${BAT_SYM}"
|
||||
;;
|
||||
Charging|Full)
|
||||
printf '%s' "${BAT_SYM_PLG}"
|
||||
;;
|
||||
*)
|
||||
printf '%s' "${BAT_SYM_UNK}"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
status_dt () {
|
||||
printf '%s' "$(date +"${DT_FMT}")"
|
||||
}
|
||||
|
||||
add_stat () {
|
||||
local -r val="$1"
|
||||
local -r sym="$2"
|
||||
local sep
|
||||
|
||||
# Skip the separator for the first status section
|
||||
if [[ -n "${status}" ]]; then sep="${SEP}"; fi
|
||||
|
||||
# Only output the status section if it has a value
|
||||
if [[ -n "${val}" ]]; then
|
||||
if [[ -n "${sym}" ]]; then status="${status}${sep}${sym} ${val}";
|
||||
else status="${status}${sep}${val}"; fi
|
||||
fi
|
||||
}
|
||||
|
||||
while :; do
|
||||
status=''
|
||||
|
||||
if [[ "${CPU_ON}" -ne 0 ]]; then
|
||||
add_stat "$(status_cpu)" "${CPU_SYM}"
|
||||
fi
|
||||
if [[ "${MEM_ON}" -ne 0 ]]; then
|
||||
add_stat "$(status_mem)" "${MEM_SYM}"
|
||||
fi
|
||||
if [[ "${BL_ON}" -ne 0 ]]; then
|
||||
add_stat "$(status_bl)" "${BL_SYM}"
|
||||
fi
|
||||
if [[ "${VOL_ON}" -ne 0 ]]; then
|
||||
vol="$(status_vol)"
|
||||
add_stat "${vol}" "$(status_vol_state "${vol:0:-1}")"
|
||||
fi
|
||||
if [[ "${BAT_ON}" -ne 0 ]]; then
|
||||
add_stat "$(status_bat_cap)" "$(status_bat_state)"
|
||||
fi
|
||||
if [[ "${DT_ON}" -ne 0 ]]; then
|
||||
add_stat "$(status_dt)" "${DT_SYM}"
|
||||
fi
|
||||
xsetroot -name " ${status} "
|
||||
|
||||
# Sleep until the beginning of the next minute, this way when we are showing
|
||||
# the time, the status updates on the minute
|
||||
## sleep $((60 - $(date +%S)))
|
||||
sleep 2
|
||||
done
|
||||
|
96
xrsbd
Executable file
96
xrsbd
Executable file
@ -0,0 +1,96 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Global readonly variables can't be shadowed by local variables so wrap
|
||||
# our code in a function so we can declare all variables local
|
||||
main() {
|
||||
# Customizable configuration constants
|
||||
local -r DEFAULT_RES=.25
|
||||
local -r DEFAULT_MOD_LIST='cpu mem bl vol-amixer bat dt'
|
||||
local -r DEFAULT_PRE=' '
|
||||
local -r DEFAULT_SEP_L='| '
|
||||
local -r DEFAULT_SEP_R=' '
|
||||
local -r DEFAULT_SUF=' '
|
||||
|
||||
local -r res="${1-${DEFAULT_RES}}"
|
||||
local -r mod_list="${2-${DEFAULT_MOD_LIST}}"
|
||||
local -r pre="${3-${DEFAULT_PRE}}"
|
||||
local -r sep_l="${4-${DEFAULT_SEP_L}}"
|
||||
local -r sep_r="${5-${DEFAULT_SEP_R}}"
|
||||
local -r suf="${6-${DEFAULT_SUF}}"
|
||||
|
||||
local -r MOD_DIR="$(dirname "$0")"/module
|
||||
local -r ACTION_DIR=/tmp/xrsb-action
|
||||
local -i 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 of cache keys
|
||||
# so we can loop over the cache in display order to generate the full status
|
||||
local -a stat_ordered_keys
|
||||
local mod mod_file key
|
||||
|
||||
# Process the module list
|
||||
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
|
||||
source "${mod_file}"
|
||||
key="${mod//-/_}"
|
||||
stat_ordered_keys+=("${key}")
|
||||
stat_cache["${key}"]="$("mod_${key}")"
|
||||
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
|
||||
printf -v stat '%b%b%b%b' \
|
||||
"${stat}" "${sep_l}" "${stat_cache[${key}]}" "${sep_r}"
|
||||
done
|
||||
|
||||
# Trim the leading left separator and trailing right separator, and
|
||||
# display the status
|
||||
local -ri offset=${#sep_l}
|
||||
local -ri len=$((${#stat} - offset - ${#sep_r}))
|
||||
xsetroot -name "${pre}${stat:${offset}:${len}}${suf}"
|
||||
}
|
||||
|
||||
# Draw the initial status
|
||||
draw_status
|
||||
|
||||
process_signal () {
|
||||
local -a actions
|
||||
local path do_redraw
|
||||
|
||||
# Get the actions and purge the action directory
|
||||
readarray -d '' actions < \
|
||||
<(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 cache entry for the module is defined
|
||||
if [[ -v stat_cache[${key}] ]]; then
|
||||
stat_cache["${key}"]="$("mod_${key}")"
|
||||
do_redraw=1
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ -v do_redraw ]]; then draw_status; fi
|
||||
}
|
||||
|
||||
# Wait for signals
|
||||
mkdir -p "${ACTION_DIR}"
|
||||
trap process_signal SIGUSR1
|
||||
|
||||
while :; do
|
||||
sleep "${res}"
|
||||
done
|
||||
}
|
||||
|
||||
main "$@"
|
||||
|
Loading…
Reference in New Issue
Block a user