dotfiles/bin/sbar

109 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
# MODULES
function update_crypto() {
local c; c="$(crypto)" && crypto="$c"
}
function update_cpu() {
cpu="$(awk '{print $1}' /proc/loadavg)"
}
function update_memory() {
memory="$(free -h | awk '$1 == "Mem:" {print $3}')"
}
function update_bat() {
local dir; dir="$(find /sys/class/power_supply/ -mindepth 1 -name 'BAT*' -print -quit)"
local cap; cap="$(cat "$dir/capacity")"
{ grep -qv Discharging "$dir/status" && bat="$cap%"; } ||
{ [ "$cap" -gt 80 ] && bat="$cap%"; } ||
{ [ "$cap" -gt 60 ] && bat="$cap%"; } ||
{ [ "$cap" -gt 40 ] && bat="$cap%"; } ||
{ [ "$cap" -gt 20 ] && bat="$cap%"; } ||
{ bat="$cap%"; }
}
function update_sink_vol() {
local sink; sink="$(wpctl get-volume '@DEFAULT_AUDIO_SINK@')"
local vol;
if grep -q 'MUTED' <<< "$sink"; then
sink_vol='🔇'
else
vol="$(awk '{print int($2 * 100)}' <<< "$sink")"
{ [ "$vol" -gt 67 ] && sink_vol="$vol"; } ||
{ [ "$vol" -gt 33 ] && sink_vol="$vol"; } ||
{ sink_vol="$vol"; }
fi
}
function update_source_vol() {
local source; source="$(wpctl get-volume '@DEFAULT_AUDIO_SOURCE@')"
local vol;
if grep -q 'MUTED' <<< "$source"; then
source_vol=""
else
vol="$(awk '{print int($2 * 100)}' <<< "$sink")"
source_vol="$(awk '{print int($2 * 100)}' <<< "$source")"
fi
}
function update_wlp() {
local sig; sig="$(awk '$1 ~ "wlp" {print int($3)}' /proc/net/wireless)"
local ssid; ssid="$(nmcli device wifi show-password | sed -n 's/SSID: //p')"
[ -z "$ssid" ] && wlp='⚠' && return
[ -z "$sig" ] && sig=0
# https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/blob/d9b06a95/src/libnmc-base/nm-client-utils.c#L628
{ [ "$sig" -gt 56 ] && wlp="▂▄▆█ $ssid"; } ||
{ [ "$sig" -gt 38 ] && wlp="▂▄▆_ $ssid"; } ||
{ [ "$sig" -gt 21 ] && wlp="▂▄__ $ssid"; } ||
{ [ "$sig" -gt 3 ] && wlp="▂___ $ssid"; } ||
{ wlp="____ $ssid"; }
}
function update_time() {
time="$(date "+%a %m/%d %R")"
}
function display() {
"${DISPLAYCMD[@]}" "$crypto | $cpu | $memory | $sink_vol $source_vol | $wlp | $bat | $time"
}
if [ "$XDG_SESSION_TYPE" = wayland ]; then
DISPLAYCMD=(echo)
else
DISPLAYCMD=(xsetroot -name)
fi
# SIGNALING
# trap '<function>;display' 'RTMIN+n'
trap 'update_sink_vol;display' 'RTMIN'
# to update it from external commands
## kill -m "$(cat ~/.cache/pidofbar)"
# where m = 34 + n
echo "$$" > "$HOME/.cache/pidofbar"
sec=0
while true; do
[ "$((sec % 300))" = 0 ] && update_crypto
[ "$((sec % 10))" = 0 ] && {
update_cpu
update_memory
update_sink_vol
update_source_vol
update_wlp
update_bat
update_time
display
}
sleep "$(awk "BEGIN {s=$(date '+%S.%N'); print 10 - s % 10}")" & wait
sec="$((sec + 10))"
done