commit 6b9838158348341ea4aae87913fe265ed9edb1c7
parent 72aa164df163dc9e72abdf2e617c4cc85ebe4f35
Author: Raymond Cole <rc@wolog.xyz>
Date: Wed, 13 Nov 2024 12:56:15 +0000
Change variable naming scheme
Diffstat:
M | infobar | | | 150 | +++++++++++++++++++++++++++++++++++++++---------------------------------------- |
1 file changed, 74 insertions(+), 76 deletions(-)
diff --git a/infobar b/infobar
@@ -3,8 +3,6 @@
# Copyright (C) Raymond Cole <rc@wolog.xyz>
# License: GPL-3.0-or-later
-#!/bin/sh
-
cleanup() {
set -- $(cat "$tmpdir"/pid/* 2>/dev/null)
[ $# -eq 0 ] || kill "$@"
@@ -12,22 +10,22 @@ cleanup() {
}
humanize() {
- _n="$1"
- _b="$2"
+ n="$1"
+ b="$2"
shift 2
- for _u in "$@"; do
- [ "$_n" -ge "$_b" ] || break
- _n=$(( (_n + _b / 2) / _b ))
+ for u in "$@"; do
+ [ "$n" -ge "$b" ] || break
+ n=$(( (n + b / 2) / b ))
done
- echo "$_n$_u"
+ echo "$n$u"
}
runinformer() {
- _f="$1"; shift
- [ -p "$tmpdir/fifo/$_f" ] || mkfifo "$tmpdir/fifo/$_f" || exit
- [ -e "$tmpdir/pid/$_f" ] && kill "$(cat "$tmpdir/pid/$_f")"
- "$@" >"$tmpdir/fifo/$_f" &
- echo "$!" >"$tmpdir/pid/$_f"
+ f="$1"; shift
+ [ -p "$tmpdir/fifo/$f" ] || mkfifo "$tmpdir/fifo/$f" || exit
+ [ -e "$tmpdir/pid/$f" ] && kill "$(cat "$tmpdir/pid/$f")"
+ "$@" >"$tmpdir/fifo/$f" &
+ echo "$!" >"$tmpdir/pid/$f"
}
updatebat() {
@@ -51,43 +49,43 @@ updateclk() {
}
updatecpu() {
- _o="$cpu"
+ o="$cpu"
set -- $(cat /proc/loadavg)
cpu=" $1"
case "$bar" in
*%P*)
set -- $(head -n 1 /proc/stat)
shift
- _u=$(( $1 + $2 + $3 + $6 + $7 ))
- _s=$(( _u + $4 + $5 ))
+ u=$(( $1 + $2 + $3 + $6 + $7 ))
+ s=$(( u + $4 + $5 ))
if [ -n "$cpuuse$cpusum" ]; then
- cpu="$cpu $(( (_u - cpuuse) * 100 / (_s - cpusum) ))%"
+ cpu="$cpu $(( (u - cpuuse) * 100 / (s - cpusum) ))%"
fi
- cpuuse="$_u"
- cpusum="$_s"
- if _f="$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq)"; then
- _t="$(humanize "$(( _f * 10 ))" 1000 '' K M G)"
- _f="${_t%[[:digit:]]*}"
- _f="${_f:-0}.${_t#$_f}"
- cpu="$cpu ${_f}Hz"
+ cpuuse="$u"
+ cpusum="$s"
+ if f="$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq)"; then
+ t="$(humanize "$(( f * 10 ))" 1000 '' K M G)"
+ f="${t%[[:digit:]]*}"
+ f="${f:-0}.${t#$f}"
+ cpu="$cpu ${f}Hz"
fi
;;
esac
- [ "$cpu" != "$_o" ]; return
+ [ "$cpu" != "$o" ]; return
}
updatemem() {
set -- $(free | sed -n '2{p;q}')
shift
- _o="$mem"
- _x="$(humanize "$(( $1 * 10 ))" 1024 Ki Mi Gi)"
- _t="${_x%[[:digit:]]*}"
- _t="${_t:-0}.${_x#$_t}B"
- _x="$(humanize "$(( $2 * 10 ))" 1024 Ki Mi Gi)"
- _u="${_x%[[:digit:]]*}"
- _u="${_u:-0}.${_x#$_u}B"
- mem=" ${_u}/${_t}"
- [ "$mem" != "$_o" ]; return
+ o="$mem"
+ x="$(humanize "$(( $1 * 10 ))" 1024 Ki Mi Gi)"
+ t="${x%[[:digit:]]*}"
+ t="${t:-0}.${x#$t}B"
+ x="$(humanize "$(( $2 * 10 ))" 1024 Ki Mi Gi)"
+ u="${x%[[:digit:]]*}"
+ u="${u:-0}.${x#$u}B"
+ mem=" $u/$t"
+ [ "$mem" != "$o" ]; return
}
updatenet() {
@@ -104,16 +102,16 @@ updatenet() {
*%N*)
case "$2" in
wl*)
- _x="$(sed -n "s/^$2:"' *[^ ]* *[^ ]* *\([^.]*\).*/\1/p' /proc/net/wireless)"
- net="$net ${_x}dBm"
+ x="$(sed -n "s/^$2:"' *[^ ]* *[^ ]* *\([^.]*\).*/\1/p' /proc/net/wireless)"
+ net="$net ${x}dBm"
;;
esac
- _x="$(cat "/sys/class/net/$2/statistics/rx_bytes")"
- [ -z "$netrx" ] || net="$net $(humanize $(( _x - netrx )) 1024 B KiB MiB)"
- netrx="$_x"
- _x="$(cat "/sys/class/net/$2/statistics/tx_bytes")"
- [ -z "$nettx" ] || net="$net $(humanize $(( _x - nettx )) 1024 B KiB MiB)"
- nettx="$_x"
+ x="$(cat "/sys/class/net/$2/statistics/rx_bytes")"
+ [ -z "$netrx" ] || net="$net $(humanize $(( x - netrx )) 1024 B KiB MiB)"
+ netrx="$x"
+ x="$(cat "/sys/class/net/$2/statistics/tx_bytes")"
+ [ -z "$nettx" ] || net="$net $(humanize $(( x - nettx )) 1024 B KiB MiB)"
+ nettx="$x"
;;
esac
else
@@ -138,20 +136,20 @@ updatevol() {
setbar() {
bar="$1"
modules=
- _o=
- _s="$IFS"
+ o=
+ s="$IFS"
IFS=' %'
- for _x in $bar; do
- modules="$_x $modules"
- case "$_x" in
- [[:upper:]]) _o="s${_o#s}" ;;
- [bc]) _o="${_o%m}m" ;;
+ for x in $bar; do
+ modules="$x $modules"
+ case "$x" in
+ [[:upper:]]) o="s${o#s}" ;;
+ [bc]) o="${o%m}m" ;;
esac
done
- IFS="$_s"
- if [ "$ttopts" != "$_o" ]; then
- runinformer t ticktack ${_o:+-$_o}
- ttopts="$_o"
+ IFS="$s"
+ if [ "$ttopts" != "$o" ]; then
+ runinformer t ticktack ${o:+-$o}
+ ttopts="$o"
fi
}
@@ -160,21 +158,21 @@ changebar() {
}
putbar() {
- _s="$IFS"
+ s="$IFS"
IFS=%
- for _x in $bar; do
- _t="${_x%% *}"
- case "${_t#%}" in
- b) _t="$bat" ;;
- [Cc]) _t="$clk" ;;
- M) _t="$mem" ;;
- [Nn]) _t="$net" ;;
- [Pp]) _t="$cpu" ;;
- v) _t="$vol" ;;
+ for x in $bar; do
+ t="${x%% *}"
+ case "${t#%}" in
+ b) t="$bat" ;;
+ [Cc]) t="$clk" ;;
+ M) t="$mem" ;;
+ [Nn]) t="$net" ;;
+ [Pp]) t="$cpu" ;;
+ v) t="$vol" ;;
esac
- printf '%s' "$_t${_x##*[! ]}"
+ printf '%s' "$t${x##*[! ]}"
done
- IFS="$_s"
+ IFS="$s"
echo
}
@@ -225,8 +223,8 @@ setbar ' %n %v %p %b %c '
# remove the %b module if there is no BAT0 (e.g. on desktop computer)
[ -r /sys/class/power_supply/BAT0/capacity ] || changebar ' %b ' ''
-for x in $modules; do
- case "$x" in
+for mod in $modules; do
+ case "$mod" in
b)
runinformer a acpi_listen
updatebat
@@ -256,15 +254,15 @@ putbar
fifolog - "$tmpdir"/fifo/* | while read -r src tok toks; do
case "$src" in
-)
- for x in $modules; do
+ for mod in $modules; do
[ "$tok" -gt 1 ] || break
tok=$(( tok - 1 ))
done
button="${toks%% *}"
- case "$x" in
+ case "$mod" in
[Cc])
if [ "$button" -eq 1 ]; then
- if [ "$x" = c ]; then
+ if [ "$mod" = c ]; then
changebar %c %C
else
changebar %C %c
@@ -334,20 +332,20 @@ fifolog - "$tmpdir"/fifo/* | while read -r src tok toks; do
esac
;;
t)
- x="$tok$bar"
- case "$x" in
+ tokbar="$tok$bar"
+ case "$tokbar" in
[sm]*%b*) updatebat && putbar ;
esac
- case "$x" in
+ case "$tokbar" in
m*%c*|[sm]*%C*) updateclk && putbar ;;
esac
- case "$x" in
+ case "$tokbar" in
[sm]*%M*) updatemem && putbar ;;
esac
- case "$x" in
+ case "$tokbar" in
[sm]*%N*) updatenet && putbar ;;
esac
- case "$x" in
+ case "$tokbar" in
[sm]*%[Pp]*) updatecpu && putbar ;;
esac
;;