tmkbar - scripts - various script and utils
HTML git clone git://z3bra.org/scripts
DIR Log
DIR Files
DIR Refs
---
tmkbar (5283B)
---
1 #!/bin/bash
2 #
3 # z3bra - (c) wtfpl 2014
4 # Fetch infos on your computer, and print them to stdout every second.
5 #
6 # require :
7 # amixer - volume()
8 # bc - cpuload() memused()
9 # curl - netip()
10 # fcount - mails()
11 # iwconfig - netint() (to spot wifi interface)
12 # mpc - nowplaying()
13 # xprop - groups() gstate()
14 # xwininfo - gstate()
15
16 # configuration variables
17 refresh_rate=0.75 # how often will the bar update
18 datefmt="%d %b %H:%M" # date time format
19 maildir=~/var/mail/inbox/new # where do new mails arrive ?
20 alsactl=Master # which alsa channel to display
21 #battery=BAT0 # battery index
22 # find battery name in a smart way
23 battery=$(ls /sys/class/power_supply | grep BAT)
24
25 #barch=''
26 barch=''
27 barfg='%{F#ff888888}'
28 barmg='%{F#ffffffff}'
29 barbg='%{F#00888888}'
30
31 grpfg='%{F#ffffff00} '
32 grpmg='%{F#ff2288cc} '
33 grpbg='%{F#ffffffff} '
34
35 # no need to modify anything else here
36
37 clock() {
38 date "+${datefmt}"
39 }
40
41 mails() {
42 fcount ${maildir}
43 }
44
45 battery() {
46 BATC=/sys/class/power_supply/${battery}/capacity
47 BATS=/sys/class/power_supply/${battery}/status
48
49 # prepend percentage with a '-' if discharging, '+' otherwise
50 #test "`cat $BATS`" = "Discharging" && echo -n '-' || echo -n '+'
51
52 cat $BATC
53 }
54
55 muted() {
56 amixer get $alsactl | grep -o '\[off\]' >/dev/null && false || true
57 }
58
59 volume() {
60 amixer get $alsactl | sed -n 's/^.*\[\([0-9]\+\)%.*$/\1/p' | uniq
61 }
62
63 cpuload() {
64 LINE=`ps -eo pcpu |grep -vE '^\s*(0.0|%CPU)' |sed -n '1h;$!H;$g;s/\n/ +/gp'`
65 echo `bc <<< $LINE`
66 }
67
68 memused() {
69 read t f <<< `grep -E 'Mem(Total|Free)' /proc/meminfo |awk '{print $2}'`
70 read b c <<< `grep -E '^(Buffers|Cached)' /proc/meminfo |awk '{print $2}'`
71 bc <<< "100 * ($t - $f - $c - $b) / $t"
72 }
73
74 netstate() {
75 test -n "`ip route`" && echo "connected" || echo "disconnected"
76 }
77
78 netip() {
79 case $1 in
80 local)
81 # local ip
82 ip addr show $(netint) | grep 'inet ' | awk '{print $2}'
83 ;;
84 *)
85 # extern ip
86 curl http://canihazip.com/s
87 ;;
88 esac
89 }
90
91 nettrafic() {
92 case $1 in
93 up) col=10;;
94 down) col=2;;
95 *)
96 esac
97
98 trafic=$(awk "/$(netint)/ {print \$$col}" /proc/net/dev)
99 echo $(SCALE=2 human $trafic)
100 }
101
102 netint() {
103 read lo int1 int2 <<< `ip link | sed -n 's/^[0-9]: \(.*\):.*$/\1/p'`
104 if iwconfig $int1 >/dev/null 2>&1; then
105 wifi=$int1
106 eth0=$int2
107 else
108 wifi=$int2
109 eth0=$int1
110 fi
111
112 if ip link show $eth0 | grep 'state UP' >/dev/null; then
113 int=$eth0
114 elif ip link show $wifi | grep 'state UP' >/dev/null; then
115 int=$wifi
116 else
117 int=lo
118 fi
119
120 echo $int
121 }
122
123 groups() {
124 cur=`xprop -root _NET_CURRENT_DESKTOP | awk '{print $3}'`
125 tot=`xprop -root _NET_NUMBER_OF_DESKTOPS | awk '{print $3}'`
126
127 for w in `seq 0 $((cur - 1))`; do line="${line}${grpbg}"; done
128 line="${line}${grpfg}"
129 for w in `seq $((cur + 2)) $tot`; do line="${line}${grpbg}"; done
130 line="${line}${barfg}"
131
132 echo $line
133 }
134
135 # print current group and shown groups
136 gstate() {
137 cur=`xprop -root _NET_CURRENT_DESKTOP | awk '{print $3}'`
138 tot=`xprop -root _NET_NUMBER_OF_DESKTOPS | awk '{print $3}'`
139
140 for wid in `xprop -root | sed '/_LIST(WINDOW)/!d;s/.*# //;s/,//g'`; do
141 if grep -q 'IsViewable' <<< $(xwininfo -id $wid); then
142 grp=`xprop -id $wid _NET_WM_DESKTOP | awk '{print $3}'`
143 shown="$shown $grp"
144 fi
145 done
146
147 for g in `seq 0 $((tot - 1))`; do
148 if test $g -eq $cur; then
149 line="${line}${grpfg}"
150 elif grep -q $g <<< "$shown"; then
151 line="${line}${grpmg}"
152 else
153 line="${line}${grpbg}"
154 fi
155 done
156
157 echo $line
158 }
159
160 nowplaying() {
161 cur=`mpc current`
162 # this line allow to choose whether the output will scroll or not
163 test "$1" = "scroll" && PARSER='skroll -n20 -d0.5 -r' || PARSER='cat'
164 test -n "$cur" && $PARSER <<< $cur || echo "- stopped -"
165 }
166
167 makebar() {
168 max=10
169 cur=$(($1 / 10))
170
171 bar="$barfg"
172
173 for v in $(seq 0 $((max - 1))); do
174 if [ "$v" -lt "$cur" ]; then
175 bar="${bar}${barmg}${barch}"
176 else
177 bar="${bar}${barfg}${barch}"
178 fi
179 done
180
181 echo "${bar}${barfg}"
182 }
183
184 # This loop will fill a buffer with our infos, and output it to stdout.
185 buf="%{l} "
186
187 # Set icon depending on the interface that is up
188 buf="${buf}${barmg} "
189 buf="${buf}${barfg}"
190 buf="$buf $(netip local) "
191
192 buf="${buf}${barmg} ${barfg} $(nettrafic down) "
193 buf="${buf}${barmg} ${barfg} $(nettrafic up) "
194 buf="${buf}%{c} $(gstate)"
195 buf="${buf}%{r}"
196 buf="${buf}${barmg} ${barfg} $(mails) "
197
198 # Change icon if volume is muted
199 if amixer get $alsactl | grep '\[off\]' >/dev/null; then
200 buf="${buf}${barmg} ${barfg} "
201 else
202 buf="${buf}${barmg} ${barfg} "
203 fi
204 buf="${buf}$(makebar $(volume)) "
205
206 # Show battery status if there is a battery (U DONT SAY)
207 test -n "${battery}" &&
208 buf="${buf}${barmg} ${barfg} $(makebar $(battery)) "
209
210 buf="${buf}${barmg} ${barfg} $(clock) "
211 echo $buf
212 sleep ${refresh_rate}