From 61ac0b46fd51939ae13dac508a59d8087c6db2ef Mon Sep 17 00:00:00 2001
From: Luca Bilke <luca@bil.ke>
Date: Mon, 23 Sep 2024 14:47:21 +0200
Subject: [PATCH] sb-cpu: cleanup

---
 .local/libexec/statusbar/sb-cpu | 70 +++++++++++++++++++--------------
 1 file changed, 40 insertions(+), 30 deletions(-)

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