2020-07-02 15:01:45 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# Module showing CPU load as a changing bars.
|
|
|
|
# Just like in polybar.
|
|
|
|
# Each bar represents amount of load on one core since
|
|
|
|
# last run.
|
|
|
|
|
|
|
|
# Cache in tmpfs to improve speed and reduce SSD load
|
|
|
|
cache=/tmp/cpubarscache
|
|
|
|
|
|
|
|
case $BLOCK_BUTTON in
|
|
|
|
2) setsid -f "$TERMINAL" -e htop ;;
|
2020-07-02 19:32:36 +00:00
|
|
|
3) notify-send "🪨 CPU load module" "Each bar represents
|
2020-07-02 15:01:45 +00:00
|
|
|
one CPU core";;
|
|
|
|
6) "$TERMINAL" -e "$EDITOR" "$0" ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# id total idle
|
|
|
|
stats=$(awk '/cpu[0-9]+/ {printf "%d %d %d\n", substr($1,4), ($2 + $3 + $4 + $5), $5 }' /proc/stat)
|
|
|
|
[ ! -f $cache ] && echo "$stats" > "$cache"
|
|
|
|
old=$(cat "$cache")
|
2020-07-02 19:32:36 +00:00
|
|
|
printf "🪨"
|
|
|
|
echo "$stats" | while read -r row; do
|
2020-07-02 15:01:45 +00:00
|
|
|
id=${row%% *}
|
|
|
|
rest=${row#* }
|
|
|
|
total=${rest%% *}
|
|
|
|
idle=${rest##* }
|
|
|
|
|
2020-07-02 19:32:36 +00:00
|
|
|
case "$(echo "$old" | awk '{if ($1 == id)
|
2020-07-02 15:01:45 +00:00
|
|
|
printf "%d\n", (1 - (idle - $3) / (total - $2))*100 /12.5}' \
|
2020-07-02 19:32:36 +00:00
|
|
|
id="$id" total="$total" idle="$idle")" in
|
2020-07-02 15:01:45 +00:00
|
|
|
|
2020-07-02 19:32:36 +00:00
|
|
|
"0") printf "▁";;
|
|
|
|
"1") printf "▂";;
|
|
|
|
"2") printf "▃";;
|
|
|
|
"3") printf "▄";;
|
|
|
|
"4") printf "▅";;
|
|
|
|
"5") printf "▆";;
|
|
|
|
"6") printf "▇";;
|
|
|
|
"7") printf "█";;
|
|
|
|
"8") printf "█";;
|
2020-07-02 15:01:45 +00:00
|
|
|
esac
|
2020-07-02 19:32:36 +00:00
|
|
|
done; printf "\\n"
|
2020-07-02 15:01:45 +00:00
|
|
|
echo "$stats" > "$cache"
|