dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

dwmblock-cpu (1055B)


      1 #!/bin/sh
      2 # dwmblocks script for CPU information
      3 # Cache in tmpfs to improve speed and reduce SSD load
      4 cache=/tmp/cpubarscache
      5 
      6 case "$BUTTON" in
      7   1)
      8     if pgrep -f /usr/bin/top >/dev/null 2>&1; then
      9       wmctrl -Fa 'top'
     10     else
     11       setsid -f "$TERMINAL" -e top
     12     fi
     13     ;;
     14   6) "$TERMINAL" -e "$EDITOR" "$0" ;;
     15 esac
     16 
     17 # id total idle
     18 stats=$(awk '/cpu[0-9]+/ {printf "%d %d %d\n", substr($1,4), ($2 + $3 + $4 + $5), $5 }' /proc/stat)
     19 [ ! -f $cache ] && echo "$stats" > "$cache" && exit
     20 
     21 old=$(cat "$cache")
     22 printf "CPU "
     23 
     24 echo "$stats" | while read -r row; do
     25   id=${row%% *}
     26   rest=${row#* }
     27   total=${rest%% *}
     28   idle=${rest##* }
     29 
     30   case "$(echo "$old" | awk '{if ($1 == id)
     31     printf "%d\n", (1 - (idle - $3)  / (total - $2))*100 /12.5}' \
     32       id="$id" total="$total" idle="$idle")" in
     33 
     34     "0") printf "▁";;
     35     "1") printf "▂";;
     36     "2") printf "▃";;
     37     "3") printf "▄";;
     38     "4") printf "▅";;
     39     "5") printf "▆";;
     40     "6") printf "▇";;
     41     "7") printf "█";;
     42     "8") printf "█";;
     43   esac
     44 done
     45 echo "$stats" > "$cache"