Compare commits

..

No commits in common. "01b247d7fab82cdc9d1507b9c9c37d2843371a28" and "1516ff1550095acf4a24485adb2e4d041cbf1678" have entirely different histories.

11 changed files with 46 additions and 182 deletions

View File

@ -67,8 +67,8 @@ Then schedule the CPU and memory usage to be updated every 5 seconds.
avds 'cpu mem' 5000 true & avds 'cpu mem' 5000 true &
``` ```
You can add these commmands to your volume and brightness keybindings to Finally, you can add these commmands to your volume and brightness keybindings
update the status when those keys are pressed. Note that these jobs don't to update the status when those keys are pressed. Note that these jobs don't
need to be backgrounded since they run immediately and exit. need to be backgrounded since they run immediately and exit.
```Shell ```Shell
@ -76,15 +76,6 @@ avds vol-amixer
avds bl avds bl
``` ```
In case you have multiple daemons running, e.g., in different login sessions,
you can update them all simultaneously. This would have all running daemons
update the volume and backlight sections of their statuses at once.
```Shell
avds '>>* vol-amixer'
avds '>>* bl'
```
You can also bind keys to commands like those in the `cmd` directory to You can also bind keys to commands like those in the `cmd` directory to
perform actions and also update the status bar. For instance, you can bind perform actions and also update the status bar. For instance, you can bind
keys to these commands to control the backlight and volume while also updating keys to these commands to control the backlight and volume while also updating

12
avdd
View File

@ -1,4 +1,4 @@
#!/usr/bin/env bash #!/bin/bash
USAGE=" USAGE="
USAGE: avdd [<mod_list>='cpu mem bl vol-amixer bat dt'|-h|-[-]help] USAGE: avdd [<mod_list>='cpu mem bl vol-amixer bat dt'|-h|-[-]help]
@ -38,15 +38,13 @@ EXAMPLES:
avdd 'vol-amixer dt' '[<' '<' '>' '>]' & avdd 'vol-amixer dt' '[<' '<' '>' '>]' &
" "
DEFAULT_MOD_LIST='cpu mem vol-amixer bat dt' DEFAULT_MOD_LIST='cpu mem bl vol-amixer bat dt'
DEFAULT_PRE=' ' DEFAULT_PRE=' '
DEFAULT_SEP_L='| ' DEFAULT_SEP_L='| '
DEFAULT_SEP_R=' ' DEFAULT_SEP_R=' '
DEFAULT_SUF=' ' DEFAULT_SUF=' '
DIR="$(dirname "$0")" MOD_DIR="$(dirname "$0")"/mod
MOD_DIR="${DIR}/mod" FIFO=/tmp/avdd-fifo-"$("$(dirname "$0")"/rpid $$)"
DAEMON="$(basename "$0")"
FIFO="/tmp/${DAEMON}-fifo-$("${DIR}/rpid" $$)"
mod_list="${1-${DEFAULT_MOD_LIST}}" mod_list="${1-${DEFAULT_MOD_LIST}}"
@ -102,7 +100,7 @@ draw_status
# function to get the new value for that section of the status bar. If any # 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 # 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. # and redraw the status bar once after all the mods are processed.
process_mods() { process_mods () {
local mod new_val is_changed local mod new_val is_changed
for mod in "$@"; do for mod in "$@"; do
if [[ -v stat_cache[${mod}] ]]; then if [[ -v stat_cache[${mod}] ]]; then

42
avds
View File

@ -1,12 +1,11 @@
#!/usr/bin/env bash #!/bin/bash
USAGE=" USAGE="
USAGE: avds <mod_list|-h|-[-]help> [<when>] [<repeat>] USAGE: avds <mod_list|-h|-[-]help> [<when>] [<repeat>]
mod_list mod_list
A comma or space separated list of modules to request that A comma or space separated list of modules to request that
the daemon execute. If mod_list begins this '>>* ', then the daemon execute.
requests are sent to all running daemons.
when The integer number of milliseconds to wait before sending the when The integer number of milliseconds to wait before sending the
request, or one of the following values: request, or one of the following values:
@ -32,10 +31,6 @@ EXAMPLES:
avds 'vol,bl' avds 'vol,bl'
Send the previous requests to all running daemons.
avds '>>* vol,bl'
If there are cpu and memory usage modules named 'cpu' and 'mem' If there are cpu and memory usage modules named 'cpu' and 'mem'
that update the cpu and memory usage statuses, send a requst to that update the cpu and memory usage statuses, send a requst to
update both of those statuses every 5 seconds. update both of those statuses every 5 seconds.
@ -49,9 +44,10 @@ EXAMPLES:
avds 'bat,dt' m true avds 'bat,dt' m true
" "
DAEMON=avdd DAEMON=avdd
FIFO=/tmp/"${DAEMON}"-fifo-"$("$(dirname "$0")"/rpid $$)"
# Convert integer milliseconds to floating point seconds # Convert integer milliseconds to floating point seconds
ms_to_s() { ms_to_s () {
printf '%.3f' "${1}e-3" printf '%.3f' "${1}e-3"
} }
@ -69,15 +65,6 @@ if [[ "${mod_list}" =~ ^(-h|-(-)?help)$ ]]; then
exit 0 exit 0
fi fi
# Check if we will be sending requests to all daemons, or set the named pipe
# for this login session
if [[ "${mod_list}" =~ ^\>\>\*\ ]]; then
mod_list="${mod_list:4}"
is_request_all=1
else
fifo="/tmp/${DAEMON}-fifo-$("$(dirname "$0")/rpid" $$)"
fi
when="${2:-0}" when="${2:-0}"
# Validate when # Validate when
@ -100,7 +87,7 @@ while [[ "${first_run}" -eq 1 || -n "${repeat}" ]]; do
else else
case "${when}" in case "${when}" in
m) m)
sleep $((60 - 10#$(date +%S))) sleep $((60 - $(date +%S)))
;; ;;
h) h)
readarray -t ms < <(date +'%M%n%S') readarray -t ms < <(date +'%M%n%S')
@ -116,24 +103,11 @@ while [[ "${first_run}" -eq 1 || -n "${repeat}" ]]; do
fi fi
fi fi
# Write each command to the named pipe for this login session # Write each command to the pipe
if [[ ! -v is_request_all ]]; then if [[ ! -p "${FIFO}" ]]; then
if [[ ! -p "${fifo}" ]]; then
printf 'The daemon %s is not running\n' "${DAEMON}" 1>&2 printf 'The daemon %s is not running\n' "${DAEMON}" 1>&2
exit 1 exit 1
fi fi
printf '%s\n' "${mod_list}" >> "${fifo}" printf '%s\n' "${mod_list}" >> "${FIFO}"
# Write each command to all the daemon named pipes
else
readarray -d '' fifos \
< <(find /tmp -maxdepth 1 -type p -name "${DAEMON}-fifo-*" -print0)
if [[ "${#fifos[@]}" -eq 0 ]]; then
printf 'There are no daemons running\n' 1>&2
exit 1
fi
for fifo in "${fifos[@]}"; do
printf '%s\n' "${mod_list}" >> "${fifo}"
done
fi
done done

View File

@ -1,11 +1,11 @@
#!/usr/bin/env bash #!/bin/bash
mod_bat () { mod_bat () {
# Customizable configuration constants # Customizable configuration constants
local -r DEFAULT_CNT=1 local -r DEFAULT_CNT=1
local -r DEFAULT_PRE_PLG=$'\u26A1 ' local -r DEFAULT_PRE_PLG=' '
local -r DEFAULT_PRE_UPLG=$'\u2755 ' local -r DEFAULT_PRE_UPLG=' '
local -r DEFAULT_PRE_UNK='?? ' local -r DEFAULT_PRE_UNK=' '
local -r DEFAULT_SUF='%' local -r DEFAULT_SUF='%'
local -r cnt="${1:-${DEFAULT_CNT}}" local -r cnt="${1:-${DEFAULT_CNT}}"

2
mod/bl
View File

@ -1,4 +1,4 @@
#!/usr/bin/env bash #!/bin/bash
mod_bl () { mod_bl () {
# Customizable configuration constants # Customizable configuration constants

View File

@ -1,8 +1,8 @@
#!/usr/bin/env bash #!/bin/bash
mod_cpu () { mod_cpu () {
# Customizable configuration constants # Customizable configuration constants
local -r DEFAULT_PRE=$'\U0001F7E9 ' local -r DEFAULT_PRE=' '
local -r DEFAULT_SUF='' local -r DEFAULT_SUF=''
local -r pre="${1-${DEFAULT_PRE}}" local -r pre="${1-${DEFAULT_PRE}}"

2
mod/dt
View File

@ -1,4 +1,4 @@
#!/usr/bin/env bash #!/bin/bash
mod_dt () { mod_dt () {
# Customizable configuration constants # Customizable configuration constants

View File

@ -1,8 +1,8 @@
#!/usr/bin/env bash #!/bin/bash
mod_mem () { mod_mem () {
# Customizable configuration constants # Customizable configuration constants
local -r DEFAULT_PRE=$'\U0001F9E0 ' local -r DEFAULT_PRE=' '
local -r DEFAULT_SUF='%' local -r DEFAULT_SUF='%'
local -r pre="${1-${DEFAULT_PRE}}" local -r pre="${1-${DEFAULT_PRE}}"

View File

@ -1,12 +1,12 @@
#!/usr/bin/env bash #!/bin/bash
# Requires amixer from the alsa-utils package # Requires amixer from the alsa-utils package
mod_vol_amixer () { mod_vol_amixer () {
# Customizable configuration constants # Customizable configuration constants
local -r DEFAULT_PRE_LOW=$'\U0001F508 ' local -r DEFAULT_PRE_LOW=' '
local -r DEFAULT_PRE_HI=$'\U0001F50A ' local -r DEFAULT_PRE_HI=' '
local -r DEFAULT_PRE_MUTE=$'\U0001F507 ' local -r DEFAULT_PRE_MUTE=' '
local -r DEFAULT_SUF='%' local -r DEFAULT_SUF='%'
local -r pre_low="${1-${DEFAULT_PRE_LOW}}" local -r pre_low="${1-${DEFAULT_PRE_LOW}}"

32
rpid
View File

@ -1,4 +1,4 @@
#!/usr/bin/env bash #!/bin/bash
USAGE=' USAGE='
USAGE: rpid <pid|-h|-[-]help> [<root_name>=login] USAGE: rpid <pid|-h|-[-]help> [<root_name>=login]
@ -22,16 +22,6 @@ EXAMPLES:
process. process.
rpid rpid
Get the PID of the first ancestor of the current process, if there
is no ancestor named "not_a_process."
rpid $$ not_a_process
Get the PID of the current process itself, if it is named
"current_process."
rpid $$ current_process
' '
# Validate the arguments # Validate the arguments
@ -50,19 +40,25 @@ fi
root_name="${2:-login}" root_name="${2:-login}"
prev_pid="${pid}" get_info() {
current_pid="${pid}" local -ri current_pid="$1"
while [[ "${name}" != "${root_name}" && -r "/proc/${current_pid}/status" ]]; do if [[ ! -r "/proc/${current_pid}/status" ]]; then exit 1; fi
mapfile info < \ mapfile info < \
<(grep --null -E -m 2 '^(Name|PPid):' "/proc/${current_pid}/status" \ <(grep --null -E -m 2 '^(Name|PPid):' "/proc/${current_pid}/status" \
| sort | cut -f 2) | sort | cut -f 2)
name="${info[0]##[[:space:]]}" name="${info[0]##[[:space:]]}"
name="${name%%[[:space:]]}" name="${name%%[[:space:]]}"
prev_pid="${current_pid}" ppid="${info[1]##[[:space:]]}"
current_pid="${info[1]##[[:space:]]}" ppid="${ppid%%[[:space:]]}"
current_pid="${current_pid%%[[:space:]]}" }
next_pid="${pid}"
while [[ "${name}" != "${root_name}" && "${ppid}" -ne 1 ]]; do
get_info "${next_pid}";
name_pid="${next_pid}"
next_pid="${ppid}"
done done
printf '%s\n' "${prev_pid}"
if [[ "${name}" != "${root_name}" ]]; then exit 1; fi if [[ "${name}" != "${root_name}" ]]; then exit 1; fi
printf '%s\n' "${name_pid}"

95
util
View File

@ -1,95 +0,0 @@
#!/usr/bin/env bash
if [[ -v IS_SOURCED_UTIL ]]; then return 0; fi
IS_SOURCED_UTIL=1
# Show the usage if requested and exit
maybe_show_usage_and_exit() {
local -r arg="${1}"
if [[ "${arg}" =~ ^(-h|-(-)?help)$ ]]; then
printf %s "${USAGE:-No help}" 1>&2
exit 0
fi
}
# Get the ms since the epoch until now
tick_now() {
local -r out_name="${1}"
printf -v "${out_name}" %.0f "$(date +%s.%N)"e3
}
# Get the ms since the epoch until the given at spec
tick_at() {
local -r out_name="${1}" at_spec="${2}"
local -i now_ms now_s h m s s_to_d
tick_now now_ms
if [[ "${at_spec}" =~ ^[0-9]+$ ]]; then
printf -v "${out_name}" %d $((now_ms + at_spec))
else
now_s=$((now_ms / 1000))
case "${at_spec}" in
m)
printf -v "${out_name}" %d $(((now_s + 60 - now_s % 60) * 1000))
;;
h)
printf -v "${out_name}" %d $(((now_s + 3600 - now_s % 3600) * 1000))
;;
d)
# Work off the local time to account for the timezone since the
# epoch is aligned to a UTC day boundary
read -r h m s < <(date +'%H %M %S')
s_to_d=$(((24 - h) * 3600 - m * 60 - s))
printf -v "${out_name}" %d $(((now_s + s_to_d) * 1000))
;;
*)
return 1
;;
esac
fi
}
# Get the ms from now until the given ms since the epoch
ms_until() {
local -r out_name="${1}" until_ms="${2}"
local -i now_ms
tick_now now_ms
printf -v "${out_name}" %d $((until_ms - now_ms))
}
# Convert integer milliseconds to floating point seconds
ms_to_s() {
local -r out_name="${1}"
local -ri ms="${2}"
printf -v "${out_name}" %.3f "${ms}e-3"
}
# Map the module file name to the module function
mod_to_fn() {
local -r out_name="${1}" file_name="${2}"
printf mod_%s "${file_name//-/_}"
}
# Get the pid of the ancestor process with the given name
root_pid() {
local -r out_name="${1}"
local -ri pid="${2:-$$}"
local -r root_name="${3:-login}"
local -i current_pid="${pid}"
local name
while [[ "${current_pid}" -ne 1 ]]; do
if [[ ! -r "/proc/${current_pid}/status" ]]; then return 1; fi
mapfile info < \
<('grep' --null -E -m 2 '^(Name|PPid):' "/proc/${current_pid}/status" \
| 'sort' | 'cut' -f 2)
name="${info[0]##[[:space:]]}"
name="${name%%[[:space:]]}"
if [[ "${name}" = "${root_name}" ]]; then
printf -v "${out_name}" %s "${current_pid}"
return 0
fi
current_pid="${info[1]##[[:space:]]}"
current_pid="${current_pid%%[[:space:]]}"
done
return 1
}