1
0
Fork 0
dotfiles/.local/libexec/statusbar/sb-cpu

31 lines
1 KiB
Text
Raw Normal View History

2022-07-04 21:36:33 +02:00
#!/bin/sh
2024-02-03 17:52:33 +01:00
. "$HOME/.local/libexec/statusbar/colors"
cache=/tmp/cpubarscache
2023-06-15 13:27:35 +02:00
temp=$(sensors | awk '/CPU/ {printf ("%.f°C", $2)}' | tr -d "+")
2024-01-17 12:37:27 +01:00
[ -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"
old=$(cat "$cache")
2024-02-03 17:52:33 +01:00
printf "%b" "<span $big> </span>$temp"
echo "$stats" | while read -r row; do
id=${row%% *}
rest=${row#* }
total=${rest%% *}
idle=${rest##* }
2024-02-03 17:52:33 +01:00
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") printf "%b" "<span $fggreen>▁</span>" ;;
"1") printf "%b" "<span $fggreen>▂</span>" ;;
"2") printf "%b" "<span $fggreen>▃</span>" ;;
"3") printf "%b" "<span $fgorange>▄</span>" ;;
"4") printf "%b" "<span $fgorange>▅</span>" ;;
"5") printf "%b" "<span $fgorange>▆</span>" ;;
"6" | "7" | "8") printf "%b" "<span $fgred>▇</span>" ;;
esac
done
echo "$stats" >"$cache"