[download]

local/bin/dwm-status

   1 #!/bin/bash
   2 
   3 source icon
   4 f=/tmp/$$.fifo && mkfifo $f
   5 
   6 trap 'kill $$; exec $0' USR1
   7 trap 'trap - TERM; kill $(jobs -p); rm $f' EXIT INT QUIT TERM
   8 
   9 music(){ # {{{
  10     MUSIC_FORMAT='$icon -$tl\n' command music | paste -sd ' '
  11 } # }}}
  12 battery(){ # {{{
  13     command battery | grep -i 'charging\|unknown' |
  14     xargs -I % icon battery-% % | awk '{ print $1, $2 }' | paste -sd ' '
  15 } # }}}
  16 
  17 ################################################################################
  18 
  19 xkbstate keyboard:En: keyboard:Ru: > $f &
  20 
  21 while true; do
  22     echo "time:$(date +%R):"
  23     echo "music:$(music):"
  24     echo "battery:$(battery):"
  25     echo "days:$(days > /dev/null && icon days):"
  26     echo "torrents:$(torrents > /dev/null && icon torrents):"
  27 
  28     sleep 10
  29 done > $f &
  30 
  31 inotifywait -me modify ~/Mail/Inbox 2>&1 | while read; do
  32     echo "inbox:$(icon inbox):"
  33 done > $f &
  34 
  35 inotifywait -me modify ~/.newsboat/cache.db 2>&1 | while read; do
  36     echo "news:$(news > /dev/null && icon news):"
  37 done > $f &
  38 
  39 tail -F ~/.cache/weather | while read i _; do
  40     echo "weather:$i:"
  41 done > $f &
  42 
  43 tail -F ~/.cache/volume{,.ico} | while read; do
  44     awk -v i="$(icon mixer)" '{ # {{{
  45         n = 20
  46         a = int($1 * n / 100)
  47         b = int(n - a)
  48 
  49         printf "volume:%s ", i
  50 
  51         for (i = 0; i < a; i++)
  52             printf "%s", "="
  53 
  54         printf "|"
  55 
  56         for (i = 0; i < b; i++)
  57             printf "%s", "-"
  58 
  59         printf ":\n"
  60     }' ~/.cache/volume # }}}
  61 done > $f &
  62 
  63 while true; do
  64     a=$(awk '!/lo/ { a+=$2; b+=$10 } END { print a, b }' /proc/net/dev) # {{{
  65     sleep 1
  66     b=$(awk '!/lo/ { a+=$2; b+=$10 } END { print a, b }' /proc/net/dev)
  67 
  68     echo "network:$(icon network) $(echo "$a $b" | awk '{
  69         a = ($3 - $1) / 1; b = ($4 - $2) / 1
  70         print (a < 1024 ? 0 : a), (b < 1024 ? 0 : b)
  71     }' | numfmt --field 1,2 --to iec --suffix B/s):" # }}}
  72 done > $f &
  73 
  74 upower -m | while read; do
  75     echo "battery:$(battery):"
  76 done > $f &
  77 
  78 tail -F ~/.cache/music | while read; do
  79     echo "music:$(music):"
  80 done > $f &
  81 
  82 ################################################################################
  83 
  84 declare -A mod
  85 status(){ # {{{
  86     local val=${mod[$1]}
  87     status+="$val${val:+$2 }"
  88 } # }}}
  89 update(){
  90     [[ $i == *:* ]] || continue &&
  91     i=${i:0:-1} mod[${i%%:*}]=${i#*:}
  92 
  93     status music
  94 
  95     status weather
  96     status battery
  97     status days
  98 
  99     status torrents
 100     status news
 101     status inbox
 102     status network
 103 
 104     status volume
 105     status+='   '
 106     status keyboard
 107     status+='  '
 108     status time
 109 }
 110 
 111 return 2>&-
 112 while read i; do
 113     status=' ' && update
 114     xsetroot -name "$status"
 115 done < $f