dotfiles

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

dwmblock-cpu (1132B)


      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 # Available memory
     18 free -h | awk '/^Mem:/ { printf "MEM +%s ", $NF }'
     19 
     20 # CPU
     21 # id total idle
     22 stats=$(awk '/cpu[0-9]+/ {printf "%d %d %d\n", substr($1,4), ($2 + $3 + $4 + $5), $5 }' /proc/stat)
     23 [ ! -f $cache ] && echo "$stats" > "$cache" && exit
     24 
     25 old=$(cat "$cache")
     26 printf "CPU "
     27 
     28 echo "$stats" | while read -r row; do
     29   id=${row%% *}
     30   rest=${row#* }
     31   total=${rest%% *}
     32   idle=${rest##* }
     33 
     34   case "$(echo "$old" | awk '{if ($1 == id)
     35     printf "%d\n", (1 - (idle - $3)  / (total - $2))*100 /12.5}' \
     36       id="$id" total="$total" idle="$idle")" in
     37 
     38     "0") printf "▁";;
     39     "1") printf "▂";;
     40     "2") printf "▃";;
     41     "3") printf "▄";;
     42     "4") printf "▅";;
     43     "5") printf "▆";;
     44     "6") printf "▇";;
     45     "7") printf "█";;
     46     "8") printf "█";;
     47   esac
     48 done
     49 echo "$stats" > "$cache"