1
0
Fork 0

sb-cpu: cleanup

This commit is contained in:
Luca Bilke 2024-09-23 14:47:21 +02:00
parent cdee5f6ce2
commit 61ac0b46fd

View file

@ -1,45 +1,55 @@
#!/bin/sh #!/bin/sh
. "colors" . "colors"
n_cpu=$(grep -c 'cpu[0-9]\+' /proc/stat)
cache=$XDG_RUNTIME_DIR/cpubarscache cache=$XDG_RUNTIME_DIR/cpubarscache
temp=$(sensors | awk '/CPU/ {printf ("%.f°C", $2)}' | tr -d "+") temp="$(sensors | awk '/CPU/ {printf ("%.f°C", $2)}' | tr -d "+") "
[ -n "$temp" ] && temp="$temp "
stats=$(awk '/cpu[0-9]+/ {printf "%d %d %d\n", substr($1,4), ($2 + $3 + $4 + $5), $5 }' /proc/stat 2>/dev/null)
[ ! -f "$cache" ] && echo "$stats" >"$cache" && exit new=$(awk '/cpu[0-9]+/ { printf "%d %d\n", $5, ($2 + $3 + $4 + $5) }' /proc/stat 2>/dev/null)
[ ! -f "$cache" ] && {
echo "$new" >"$cache"
exit
}
old=$(cat "$cache") old=$(cat "$cache")
printf "%b" " $temp"
printf "%b" " $temp<span ${bggray}>"
first=true first=true
echo "$stats" | while read -r row; do IFS='
id=${row%% *} '
rest=${row#* }
total=${rest%% *} for i in $(seq "$n_cpu"); do
idle=${rest##* } old_idle=$(echo "$old" | awk -v i="$i" 'NR==i { print $1 }')
case "$( old_total=$(echo "$old" | awk -v i="$i" 'NR==i { print $2 }')
echo "$old" | new_idle=$(echo "$new" | awk -v i="$i" 'NR==i { print $1 }')
awk '{if ($1 == id) printf "%d\n", (1 - (idle - $3) / (total - $2))*100 /12.5}' \ new_total=$(echo "$new" | awk -v i="$i" 'NR==i { print $2 }')
id="$id" total="$total" idle="$idle" 2>/dev/null
)" in delta_idle=$((new_idle - old_idle))
"0") col=$fggreen icon=▁ ;; delta_total=$((new_total - old_total))
"1") col=$fggreen icon=▂ ;; percent=$((((delta_total - delta_idle) * 100 / delta_total) / 10))
"2") col=$fggreen icon=▃ ;;
"3") col=$fgorange icon=▄ ;; case "$percent" in
"4") col=$fgorange icon=▅ ;; 0 | 1) color="$fggreen" bar="▁" ;;
"5") col=$fgorange icon=▆ ;; 2) color="$fggreen" bar="▂" ;;
"6" | "7" | "8") col=$fgred icon=▇ ;; 3 | 4) color="$fggreen" bar="▃" ;;
5) color="$fgorange" bar="▄" ;;
6 | 7) color="$fgorange" bar="▅" ;;
8) color="$fgred" bar="▆" ;;
*) color="$fgred" bar="▇" ;;
esac esac
if [ "$col" != "$prevcol" ]; then
[ "$first" != "true" ] && printf "%b" "</span>" if [ "$color" != "$prevcolor" ]; then
printf "%b" "<span $col>" $first || printf "%b" "</span>"
printf "%b" "<span $color>"
fi fi
printf "%b" "$icon" printf "%b" "$bar"
first=false first=false
prevcol=$col prevcolor=$color
index=$((index + 1))
done done
printf "%b" "</span>" printf "%s" "</span></span>"
echo "$new" >"$cache"
echo "$stats" >"$cache"