dotfiles

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

dwmblock-cpu (980B)


      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) pgrep -f /usr/bin/top || setsid -f "$TERMINAL" -e top ;;
      8   6) "$TERMINAL" -e "$EDITOR" "$0" ;;
      9 esac
     10 
     11 # id total idle
     12 stats=$(awk '/cpu[0-9]+/ {printf "%d %d %d\n", substr($1,4), ($2 + $3 + $4 + $5), $5 }' /proc/stat)
     13 [ ! -f $cache ] && echo "$stats" > "$cache" && exit
     14 
     15 old=$(cat "$cache")
     16 printf "CPU "
     17 
     18 echo "$stats" | while read -r row; do
     19   id=${row%% *}
     20   rest=${row#* }
     21   total=${rest%% *}
     22   idle=${rest##* }
     23 
     24   case "$(echo "$old" | awk '{if ($1 == id)
     25     printf "%d\n", (1 - (idle - $3)  / (total - $2))*100 /12.5}' \
     26       id="$id" total="$total" idle="$idle")" in
     27 
     28     "0") printf "▁";;
     29     "1") printf "▂";;
     30     "2") printf "▃";;
     31     "3") printf "▄";;
     32     "4") printf "▅";;
     33     "5") printf "▆";;
     34     "6") printf "▇";;
     35     "7") printf "█";;
     36     "8") printf "█";;
     37   esac
     38 done
     39 echo "$stats" > "$cache"