1 #!/bin/sh 2 3 # Module showing CPU load as a changing bars. 4 # Just like in polybar. 5 # Each bar represents amount of load on one core since 6 # last run. 7 8 # Cache in tmpfs to improve speed and reduce SSD load 9 cache=/tmp/cpubarscache 10 11 case $BLOCK_BUTTON in 12 2) setsid -f "$TERMINAL" -e htop ;; 13 3) notify-send "🪨 CPU load module" "Each bar represents 14 one CPU core";; 15 6) "$TERMINAL" -e "$EDITOR" "$0" ;; 16 esac 17 18 # id total idle 19 stats=$(awk '/cpu[0-9]+/ {printf "%d %d %d\n", substr($1,4), ($2 + $3 + $4 + $5), $5 }' /proc/stat) 20 [ ! -f $cache ] && echo "$stats" > "$cache" 21 old=$(cat "$cache") 22 printf "🪨" 23 echo "$stats" | while read -r row; do 24 id=${row%% *} 25 rest=${row#* } 26 total=${rest%% *} 27 idle=${rest##* } 28 29 case "$(echo "$old" | awk '{if ($1 == id) 30 printf "%d\n", (1 - (idle - $3) / (total - $2))*100 /12.5}' \ 31 id="$id" total="$total" idle="$idle")" in 32 33 "0") printf "▁";; 34 "1") printf "▂";; 35 "2") printf "▃";; 36 "3") printf "▄";; 37 "4") printf "▅";; 38 "5") printf "▆";; 39 "6") printf "▇";; 40 "7") printf "█";; 41 "8") printf "█";; 42 esac 43 done; printf "\\n" 44 echo "$stats" > "$cache"