local/bin/status

   1 #!/bin/bash
   2 
   3 opts=$(getopt -o niI -l notify,icon,no-icon -- "$@")
   4 eval set -- "$opts"
   5 
   6 while true; do
   7     case $1 in
   8         -n|--notify) shift && notify=1;;
   9         -i|--icon) shift && icon=1;;
  10         -I|--no-icon) shift && no_icon=1;;
  11         --) shift && break;;
  12     esac
  13 done
  14 
  15 print(){
  16     local s=$(
  17         if [[ $no_icon || ! $1 && $2 ]]; then
  18             echo "$2"
  19         elif [[ $icon || $1 && $2 ]]; then
  20             echo "$@"
  21         fi | sed 's/^\s*//; s/\s*$//'
  22     )
  23 
  24     [[ $s ]] && echo "$s"
  25     exit
  26 }
  27 
  28 case $1${notify:+-notify} in
  29     apps) print ;;
  30     files) print ;;
  31     browser) print ;;
  32     kino) print ;;
  33     bloge) print ;;
  34     editor) print ;;
  35     mixer) print ;;
  36     windows) print ;;
  37     terminal) print ;;
  38     theme) print ;;
  39     develop) print ;;
  40     symbols) print ;;
  41 
  42     mail)
  43         n=$(frm -s new | wc -l)
  44         [[ $n == 0 ]] &&
  45         print  || print  $n
  46     ;;
  47 
  48     mail-notify)
  49         notify-send -i emblem-mail ${1^} "$(
  50             frm -s new | tac | awk '{ print NR ". " $0 }'
  51         )"
  52     ;;
  53 
  54     news)
  55         n=$(newsboat -x print-unread | cut -d ' ' -f 1)
  56         [[ $n == 0 ]] && n=
  57         print  $n
  58     ;;
  59 
  60     news-notify)
  61         notify-send -i application-rss+xml ${1^} "$(
  62             sqlite3 ~/.newsboat/cache.db '
  63                 select title from rss_item where unread = 1
  64                 order by pubDate desc limit 10
  65             ' | awk '{ print NR ". " $0 }'
  66         )"
  67     ;;
  68 
  69     network)
  70         d=($(nmcli -t -f type dev))
  71         i=$(nmcli -t -f in-use,bars dev wifi | awk -F : '$1 == "*" { print $2 }')
  72 
  73         [[ $d == wifi ]] &&
  74         i=${i:-} || i=
  75 
  76         print $i
  77     ;;
  78 
  79     network-notify)
  80         notify-send -i network-wireless ${1^} "$(
  81             nmcli -t -f bars,in-use,ssid dev wifi | tr : ' '
  82         )"
  83     ;;
  84 
  85     music) s=$(music); print  "$s";;
  86     music-notify) notify-send -i audio-x-generic ${1^} "$(music)";;
  87 
  88     torrents)
  89         s=$(
  90             transmission-remote -l | sed 's/\s\+\s/\t/g' |
  91             awk -F \\t '/^\s/ { print substr($(NF - 1), 0, 2) }' |
  92             sed 's/Do\|Up/1 /; s/Se/3 /; s/Qu/4 /; s/Id\|St/5 /' |
  93             sort | uniq -c | awk '{ printf "%s %s ", $3, $1 }'
  94         )
  95         print "${s:-}"
  96     ;;
  97 
  98     torrents-notify)
  99         notify-send -i transmission ${1^} "$(
 100             transmission-remote -l | sed '1d; $d; s/\s\+\s/\t/g' | cut -f 3,9,10 | column -t
 101         )"
 102     ;;
 103 
 104     weather) print  "$(< ~/.cache/status-weather)";;
 105     weather-notify)
 106         f=~/.cache/status-weather
 107 
 108         read _ _ weather icon desc < <(
 109             lynx -dump -display_charset utf-8 pogoda.ya.ru/saint-petersburg |
 110             grep -A 3 Сейчас | tr \\n ' ' | tr -d '[]'
 111         )
 112 
 113         echo "$weather" > $f
 114         curl -s https://yastatic.net/weather/i/icons/funky/light/$icon > $f.svg
 115         notify-send -i $f.svg ${1^} "$weather ${desc,,}"
 116     ;;
 117 
 118     days)
 119         s=$(grep "$(date +'%d %b')" ~/Documents/Заметки.md)
 120         n=$(wc -l <<< "$s")
 121 
 122         print  ${s:+$n}
 123     ;;
 124 
 125     days-notify)
 126         notify-send -i x-office-calendar "🎂 ${1^}" "$(
 127             cat  ~/Documents/Заметки.md | tr \| \\t |
 128             awk 'int($1) >= int(strftime("%d")) && index($2, strftime("%b")) {
 129                 print
 130 
 131                 if (int($1) == int(strftime("%d"))) {
 132                     gsub(/\S/, "-")
 133                     print
 134                 }
 135             }' | column -te
 136         )"
 137     ;;
 138 
 139     redshift) [[ $(pgrep -x redshift) ]] && print  || print ;;
 140     redshift-notify) notify-send -i redshift ${1^} "$(redshift -p)";;
 141 
 142     lang)
 143         a=([00000]=EN [00001]=RU)
 144         n=$(xset -q | grep -o '0000[0-9]')
 145         print גּ ${a[$n]}
 146     ;;
 147 
 148     bandwidth)
 149         a=$(< ~/.cache/status-bandwidth)
 150         b=$(awk '!/lo/ { a+=$2; b+=$10 } END { print a, b }' /proc/net/dev)
 151         echo "$b" > ~/.cache/status-bandwidth
 152 
 153         print  "$(
 154             echo $a $b ${2:-1} |
 155             awk '{ print ($3 - $1) / $5, ($4 - $2) / $5 }' |
 156             numfmt --field 1,2 --to iec --suffix B/s
 157         )"
 158     ;;
 159 
 160     volume)
 161         s=$(pacmd list-sinks | awk '/^\s+volume:/ { print $5 }')
 162         n=${s:0:-1}
 163         i=
 164 
 165         (( $n > 30 )) && i=
 166         (( $n > 65 )) && i=
 167 
 168         print $i "$s"
 169     ;;
 170 
 171     volumebar)
 172         s=$($0 volume)
 173         n=${s:2:-1}
 174         i=${s:0:1}
 175 
 176         print $i "$(
 177             echo "${2:-10} $n" | awk '{
 178                 a = int($1 * $2 / 100)
 179                 b = int($1 - a)
 180 
 181                 for (i = 0; i < a; i++)
 182                     printf "%s", "="
 183 
 184                 printf "|"
 185 
 186                 for (i = 0; i < b; i++)
 187                     printf "%s", "-"
 188 
 189                 printf "\n"
 190             }'
 191         )"
 192     ;;
 193 
 194     volume-notify)
 195         notify-send -i audio-card ${1^} "$(
 196             pacmd list-sinks | awk '
 197                 /device.description/; /index:/; /^\s+volume:/ { printf "\t%s %s", $1, $5 } /muted:/
 198             ' | tr -s \\t | sed 's/.*description = /\t/' | sed 's/^  //; s/\t/    /'
 199         )"
 200     ;;
 201 
 202     battery)
 203         declare -A a=(
 204             [Unknown]=
 205            [Critical]=
 206                 [Low]=
 207              [Normal]=
 208                [High]=
 209                [Full]=
 210         )
 211 
 212         for i in /sys/class/power_supply/*; do
 213             print "${a[$(< $i/capacity_level)]}" "$(< $i/model_name)"
 214         done
 215     ;;
 216 
 217     battery-notify)
 218         notify-send -i battery ${1^} "$(
 219             paste -d ' ' /sys/class/power_supply/*/{manufacturer,model_name,capacity*}
 220         )"
 221     ;;
 222 
 223     calendar) print  "$(calcurse -n | sed '1d; s/^\s*//')";;
 224     date) print  "$(date +'%a, %d %B')";;
 225     time)
 226         a=(           )
 227         n=$(date +%I)
 228         i=${a[${n:1}]}
 229 
 230         print "$i" "$(date +'%H:%M')"
 231     ;;
 232 
 233     '') grep -Po '^\s+[\w^)]+\)' $0 | tr -d ') ' | sort;;
 234 esac 2>&-