improve eww

This commit is contained in:
2026-09-10 19:54:50 +02:00
parent e551d36e1c
commit 7bb27f9373
46 changed files with 1296 additions and 1092 deletions
@@ -1,61 +1,67 @@
#!/usr/bin/env zsh
#!/usr/bin/env bash
# Battery state from sysfs; refreshes on power_supply udev events and every
# 30 s. Emits a `state` (charging / discharging / low / full / idle) that the
# CSS maps to a colour, instead of hard-coding one here.
icons=("󰂎" "󰁺" "󰁻" "󰁼" "󰁽" "󰁾" "󰁿" "󰂀" "󰂁" "󰂂" "󰁹")
num_icons=$(awk -v n="${#icons[@]}" 'BEGIN{print 100 / n}')
BATTERY="/sys/class/power_supply/BAT0"
geticon() {
level=$(awk -v n="$CAPACITY" -v c="$num_icons" 'BEGIN{print int(n/c-1)}')
echo "${icons[$level]}"
}
status() {
if [ "$STATE" = "Charging" ]; then
echo -n "Charging"
if [ "$RATE" -gt 0 ]; then
echo ", $(gettime) left"
else
echo ""
fi
elif [ "$STATE" = "Discharging" ]; then
echo "$(gettime)h left"
else
echo "Fully Charged"
fi
}
color() {
[ "$CAPACITY" -le 20 ] && echo '#f38ba8' || echo '#a6e3a1'
}
wattage() {
awk -v rate="$RATE" -v uw="1000000" 'BEGIN{print sprintf("%.1f W", rate/uw)}'
}
gettime() {
FULL=$(cat $BATTERY/energy_full)
NOW=$(cat $BATTERY/energy_now)
if [ "$RATE" -gt 0 ]; then
if [ "$STATE" = "Discharging" ]; then
EX=$(( ($NOW*1.0) / $RATE))
else
EX=$(( (($FULL - $NOW)*1.0) / $RATE))
fi
date -u -d@"$(awk -v ex="$EX" 'BEGIN{print ex * 3600}')" +%H:%M
fi
}
if [ -d "$BATTERY" ]; then
while true; do
RATE=$(cat $BATTERY/power_now)
CAPACITY=$(cat $BATTERY/capacity)
STATE=$(cat $BATTERY/status)
echo '{ "visible": true, "icon": "'$(geticon)'", "percentage": '$CAPACITY', "wattage": "'$(wattage)'", "status": "'$(status)'", "color": "'$(color)'" }'
sleep 15
done
else
echo '{ "visible": false, "icon": "", "percentage": 0, "wattage": "", "status": "", "color": "#a6e3a1" }'
bat=""
for d in /sys/class/power_supply/*; do
[[ -r $d/type && $(<"$d/type") == Battery ]] && { bat=$d; break; }
done
if [[ -z $bat ]]; then
echo '{"visible":false,"percentage":0,"wattage":"","status":"","state":"none"}'
exit 0
fi
attr() { # first readable, non-empty attribute of the list, else 0
local a v
for a in "$@"; do
v=$(cat "$bat/$a" 2>/dev/null) && [[ -n $v ]] && { echo "$v"; return; }
done
echo 0
}
hours_to_clock() { awk -v h="$1" 'BEGIN { printf "%d:%02d", int(h), int((h - int(h)) * 60 + 0.5) }'; }
state() {
local cap status rate now full power time="" wattage state
cap=$(attr capacity)
status=$(attr status)
rate=$(attr power_now current_now) # µW or µA
now=$(attr energy_now charge_now) # µWh or µAh — same unit as rate, so now/rate = hours
full=$(attr energy_full charge_full)
if [[ -r $bat/power_now ]]; then power=$rate; else power=$((rate * $(attr voltage_now) / 1000000)); fi
wattage=$(awk -v p="$power" 'BEGIN { printf "%.1f W", p / 1000000 }')
if ((rate > 0)); then
case $status in
Discharging) time=$(hours_to_clock "$(awk -v n="$now" -v r="$rate" 'BEGIN { print n / r }')") ;;
Charging) time=$(hours_to_clock "$(awk -v n="$now" -v f="$full" -v r="$rate" 'BEGIN { print (f - n) / r }')") ;;
esac
fi
case $status in
Charging)
state=charging
status="Charging${time:+, $time left}"
;;
Discharging)
state=discharging
((cap <= 20)) && state=low
status=${time:+$time left}
;;
Full)
state=full
status="Fully charged"
;;
*) state=idle ;;
esac
printf '{"visible":true,"percentage":%s,"wattage":"%s","status":"%s","state":"%s"}' \
"$cap" "$wattage" "$(jstr "$status")" "$state"
}
refresh() { emit "$(state)"; }
_ps_events() { exec udevadm monitor --udev --subsystem-match=power_supply; }
refresh
mux_init
mux_add _ps_events
mux_tick 30
mux_loop refresh
@@ -1,49 +0,0 @@
#!/usr/bin/env bash
COLS=4
declare -A prev_idle prev_total
emit_grid() {
local rows=() row_items=()
while IFS= read -r line; do
[[ $line =~ ^cpu([0-9]+) ]] || continue
local core="${BASH_REMATCH[1]}"
read -ra f <<< "$line"
local idle=$(( f[4] + f[5] ))
local total=0
for x in "${f[@]:1}"; do (( total += x )); done
local usage="0.0"
if [[ -n "${prev_total[$core]+x}" ]]; then
local dt=$(( total - prev_total[$core] ))
local di=$(( idle - prev_idle[$core] ))
(( dt > 0 )) && usage=$(awk "BEGIN{printf \"%.1f\", 100*(1-$di/$dt)}")
fi
prev_idle[$core]=$idle
prev_total[$core]=$total
local freq=0
local fpath="/sys/devices/system/cpu/cpu${core}/cpufreq/scaling_cur_freq"
[[ -r $fpath ]] && freq=$(( $(< "$fpath") / 1000 ))
row_items+=("{\"core\":$core,\"usage\":$usage,\"freq\":$freq}")
if (( ${#row_items[@]} == COLS )); then
local row; printf -v row '%s,' "${row_items[@]}"; row="${row%,}"
rows+=("[$row]")
row_items=()
fi
done < /proc/stat
if (( ${#row_items[@]} > 0 )); then
local row; printf -v row '%s,' "${row_items[@]}"; row="${row%,}"
rows+=("[$row]")
fi
local out; printf -v out '%s,' "${rows[@]}"; out="${out%,}"
echo "[$out]"
}
while true; do
emit_grid
sleep 2
done
@@ -1,3 +1,10 @@
#!/usr/bin/env bash
dir=$(grep -rl k10temp /sys/class/hwmon/*/name 2>/dev/null | head -1 | xargs dirname)
awk '{printf "%.0f\n", $1/1000}' "$dir/temp1_input"
# CPU package temperature in °C from the first known hwmon driver.
for name in k10temp zenpower coretemp cpu_thermal acpitz; do
for d in /sys/class/hwmon/hwmon*; do
[[ -r $d/name && $(<"$d/name") == "$name" && -r $d/temp1_input ]] || continue
awk '{ printf "%d\n", $1 / 1000 }' "$d/temp1_input"
exit 0
done
done
echo 0
@@ -1,5 +1,7 @@
#!/usr/bin/env bash
amdgpu_top -J -s 5000 | jq -c --unbuffered '.devices[0] | {
# AMD GPU activity, clocks, temperature, power and VRAM via amdgpu_top's JSON
# stream, flattened to what the widgets need.
exec amdgpu_top -J -s 5000 | jq -c --unbuffered '.devices[0] | {
gfx_pct: (.gpu_activity.GFX.value // 0),
mem_pct: (.gpu_activity.Memory.value // 0),
media_pct: (.gpu_activity.MediaEngine.value // 0),
@@ -1,13 +1,18 @@
#!/usr/bin/env zsh
total="$(free --si | rg Mem | awk '{print $2}')"
human() {
awk -v mem="$1" 'BEGIN{print sprintf("%.1f%s", mem/1000/(mem > 1000000 ? 1000 : 1), mem > 1000000 ? "G" : "M")}'
}
free --si -s 3 | rg --line-buffered Mem | while read -r line; do
used=$(echo "$line" | awk '{print $3}')
cached=$(echo "$line" | awk '{print $6}')
echo '{"human": {"total": "'$(human "$total")'", "used": "'$(human "$used")'", "cached": "'$(human "$cached")'"}, "total": "'$total'", "used": "'$used'", "cached": "'$cached'"}'
#!/usr/bin/env bash
# RAM and swap from /proc/meminfo every 3 s. "used" follows `free`
# (total - available); "cached" is buffers + page cache + reclaimable slab.
# Human values use SI units like the previous `free --si`.
while :; do
awk '
/^MemTotal:/ { t = $2 } /^MemAvailable:/ { a = $2 }
/^Buffers:/ { b = $2 } /^Cached:/ { c = $2 }
/^SReclaimable:/ { s = $2 } /^SwapTotal:/ { st = $2 }
/^SwapFree:/ { sf = $2 }
function h(kb) { return kb >= 1000000 ? sprintf("%.1fG", kb / 1000000) : sprintf("%.0fM", kb / 1000) }
END {
used = t - a; cached = b + c + s; su = st - sf
printf "{\"total\":%d,\"used\":%d,\"cached\":%d,\"human\":{\"total\":\"%s\",\"used\":\"%s\",\"cached\":\"%s\"},", t, used, cached, h(t), h(used), h(cached)
printf "\"swap\":{\"total\":%d,\"used\":%d,\"human\":{\"total\":\"%s\",\"used\":\"%s\"}}}\n", st, su, h(st), h(su)
}' /proc/meminfo
sleep 3
done
@@ -1,7 +0,0 @@
#!/usr/bin/env bash
free --si | awk '/Swap/ {
used=$3; total=$2
u = sprintf("%.1fG", used/1000000)
t = sprintf("%.1fG", total/1000000)
printf "{\"used\":%d,\"total\":%d,\"human\":{\"used\":\"%s\",\"total\":\"%s\"}}\n", used, total, u, t
}'