dotfiles/.local/bin/sbar

128 lines
3.2 KiB
Bash
Executable File

#!/bin/bash
function update_crypto() {
local c
c="$(crypto 2> /dev/null)" && echo "$c" > "$SBAR/crypto"
}
function update_cpu() {
echo "$(awk '{print $1}' /proc/loadavg)" > "$SBAR/cpu"
}
function update_memory() {
echo "$(free -h | awk '/^Mem:/{print $3}')" > "$SBAR/memory"
}
function update_bat() {
local cap; cap="$(cat /sys/class/power_supply/BAT0/capacity)"
if grep -qv Discharging /sys/class/power_supply/BAT0/status; then
echo "$cap%" > "$SBAR/bat"
else
echo "$cap%" > "$SBAR/bat"
fi
}
function update_sink_vol() {
local vol mute
read -r _ vol mute < <(wpctl get-volume '@DEFAULT_AUDIO_SINK@') || return
if [ -n "$mute" ]; then
echo '🔇' > "$SBAR/sink"
else
echo "$((10#${vol/./}))" > "$SBAR/sink"
fi
}
function update_source_vol() {
local vol mute
read -r _ vol mute < <(wpctl get-volume '@DEFAULT_AUDIO_SOURCE@') || return
if [ -n "$mute" ]; then
echo '' > "$SBAR/source"
else
echo "$((10#${vol/./}))" > "$SBAR/source"
fi
}
function update_net() {
local dev j ssid sig
dev="$(ip route | grep -oP 'default.*dev\s+\K[^\s]+')"
j="$(networkctl status "$dev" --json=short)"
case "$(jq -r .Type <<< "$j")" in
ether)
echo "🌐 $dev" > "$SBAR/net"
;;
wlan)
ssid="$(jq -r .SSID <<< "$j")"
sig="$(awk "/$dev/{printf \"%.0f\", \$3}" /proc/net/wireless)"
if [ "$sig" -gt 56 ]; then echo "▂▄▆█ $ssid" > "$SBAR/net"
elif [ "$sig" -gt 38 ]; then echo "▂▄▆_ $ssid" > "$SBAR/net"
elif [ "$sig" -gt 21 ]; then echo "▂▄__ $ssid" > "$SBAR/net"
elif [ "$sig" -gt 3 ]; then echo "▂___ $ssid" > "$SBAR/net"
else echo "____ $ssid" > "$SBAR/net"
fi
;;
*)
echo '⚠' > "$SBAR/net"
;;
esac
}
function update_vpn() {
local v; v="$(ip -br link show type wireguard | awk '{print $1}')"
if [ -n "$v" ]; then
echo " 🔒 $v" > "$SBAR/vpn"
else
: > "$SBAR/vpn"
fi
}
function update_time() {
echo "$(TZ=Asia/Makassar date '+%R %Z') - $(date -u '+%a %m-%d %R')" \
> "$SBAR/time"
}
function display() {
for f in "$SBAR"/*; do
local "$(basename "$f")"="$(cat "$f")"
done
# shellcheck disable=SC2154
xsetroot -name "$crypto | $cpu | $memory | $sink $source | $net$vpn | $bat | $time"
}
# SIGNALING
# trap '<function>;display;wait -n' 'RTMIN+n'
trap 'update_sink_vol;display;wait -n' 'RTMIN+0'
trap 'update_source_vol;display;wait -n' 'RTMIN+1'
trap 'update_net;display;wait -n' 'RTMIN+2'
trap 'update_vpn;display;wait -n' 'RTMIN+3'
# to update it from external commands
## kill -m "$(cat "$XDG_CACHE_HOME/pidofbar")"
# where m = 34 + n
SBAR="$XDG_CACHE_HOME/sbar"
mkdir -p "$SBAR"
echo "$$" > "$SBAR/pid"
(while :; do
update_crypto
sleep $((300 - 10#$(date '+%S') % 300)) & wait -n
done &)
while true; do
update_cpu
update_memory
update_sink_vol
update_source_vol
update_net
update_vpn
update_bat
update_time
display
sleep $((5 - 10#$(date '+%S') % 5)) & wait -n
done