-
Notifications
You must be signed in to change notification settings - Fork 2
/
herbstluftwm_panel
executable file
·327 lines (281 loc) · 9.76 KB
/
herbstluftwm_panel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/bin/bash
# this dzen panel script is based on the one provided with herbstluftwm
# ... but this one has been chomping on steroid candy :-)
# disable path name expansion so globs not expanded in "cmd=( $line )"
set -f
monitor=${1:-0}
geometry=( $(herbstclient monitor_rect "$monitor") )
if [[ -z "$geometry" ]] ;then
echo "ERROR: Invalid monitor $monitor"
exit 1
fi
# geometry has the format: WxH+X+Y
x=${geometry[0]}
y=${geometry[1]}
pw=${geometry[2]} # panel width
ph=22 # panel height
bw=60 # dzen gdbar width
bh=$((ph / 2)) # dzen gdbar height
font="-*-terminus-bold-r-*-*-20-*-*-*-*-*-iso10646-*"
bgcolor="#000"
selbg="#0F0"
selfg="#000"
ICONS=/usr/share/icons/stlarch_icons
pomodoro_block=5 # minutes
window_title_len=60 # chars
NET_SLEEP=1
NETI=eth0
[[ $HOSTNAME = "eadmaka" ]] && NETI=kvmbr0
[[ $HOSTNAME = "tiburon" ]] && NETI=wlan0
IFDATA=(${NETI} 0 0 0 0) # array stores previous byte counts and rates
function uniq_linebuffered()
{
awk '$0 != l { print ; l=$0 ; fflush(); }' "$@"
}
function linebuffered()
{
awk '{ printf "%s\n",$0; fflush(); }' "$@"
}
function linebuffered_with_cpu_prefix()
{
awk '{ printf "cpu %s\n",$0; fflush(); }' "$@"
}
function network_bandwidth()
{
# get new rx/tx counts
RXB=`cat /sys/class/net/${1}/statistics/rx_bytes`
TXB=`cat /sys/class/net/${1}/statistics/tx_bytes`
# calculate the rates, format the values to four digit fields
RXR=$(((RXB - $2) / 1024 / $6))
TXR=$(((TXB - $3) / 1024 / $6))
echo "$1 $RXB $TXB $RXR $TXR"
}
function memory_usage()
{
awk '/MemTotal:/ {mtotal=$2}; # total usable ram
/MemFree:/ {mfree=$2}; # total ram that is free
/Active:/ {mactive=$2}; # total ram that is active/used
/Inactive:/ {minactive=$2}; # total ram that is inactive/unused
/^Cached:/ {mcached=$2}; # total ram used for caching
/SwapTotal:/ {swtotal=$2}; # total usable swap
/SwapFree:/ {swfree=$2}; # total swap that is free
END {
printf("mem %d %d %d %d %d\n",
(100 - ((mfree / mtotal) * 100)), # mem used
( (mactive / mtotal) * 100), # mem active
( (minactive / mtotal) * 100), # mem inactive
( (mcached / mtotal) * 100), # mem cached
(100 - ((swfree / swtotal) * 100))); # swap used
}' /proc/meminfo
}
function disk_usage()
{
echo "disk `df / | tail -1 | awk '{ print $5; }' | cut -d% -f1`"
}
herbstclient pad $monitor $ph
{
# Gatherers that spew events...
# -> each event line formatted as "<tag> <data>\n"
# pomodoro
POMODORO_CNT=0
while true ; do
if [[ -f $HOME/.pomodoro ]]; then
$((POMODORO_CNT += 1))
if [[ $POMODORO_CNT -eq $((pomodoro_block * 60 * 5)) ]]; then
DISPLAY=:0.0 notify-send -u critical -a pomodoro "Take a break!"
fi
if [[ $POMODORO_CNT -eq $((pomodoro_block * 60 * 6)) ]]; then
DISPLAY=:0.0 notify-send -u critical -a pomodoro "Back to work!"
POMODORO_CNT=1
fi
else
POMODORO_CNT=0
fi
echo "pomodoro $POMODORO_CNT"
sleep 1 || break
done > >(linebuffered) &
pomodoro_pid=$!
# date
while true ; do
date +'date ^fg(#efefef)%H:%M ^fg(#909090)%m-^fg(#efefef)%d'
sleep 1 || break
done > >(uniq_linebuffered) &
date_pid=$!
# cpu
gcpubar -s o \
-h $bh \
-w $bw \
-fg darkorange \
-bg darkgreen > >(linebuffered_with_cpu_prefix) &
cpu_pid=$!
# network
while true ; do
IFDATA=( `network_bandwidth ${IFDATA[@]} $NET_SLEEP` )
echo "net ${IFDATA[0]} ${IFDATA[3]} ${IFDATA[4]}"
sleep $NET_SLEEP || break
done > >(uniq_linebuffered) &
network_pid=$!
# memory
while true ; do
memory_usage
sleep 5 || break
done > >(uniq_linebuffered) &
memory_pid=$!
# disk
while true ; do
disk_usage
sleep 60 || break
done > >(uniq_linebuffered) &
disk_pid=$!
herbstclient --idle
kill $pomodoro_pid
kill $date_pid
kill $cpu_pid
kill $network_pid
kill $memory_pid
kill $disk_pid
} \
2> /dev/null | \
{
TAGS=( $(herbstclient tag_status $monitor) )
visible=true
date=""
cpu=""
net=""
mem=""
disk=""
windowtitle=""
pomodoro=0
separator="^bg()^fg(magenta) .:. "
while true ; do
echo -n "^fg(cyan)^i(${ICONS}/arch1.xbm) "
echo -n "^fg(orange)["
if [[ $pomodoro -ne 0 ]]; then
if [[ $pomodoro -ge $((pomodoro_block * 60 * 1)) ]]; then
echo -n "^fg(green)^i(${ICONS}/dice1.xbm) "
else
echo -n "^fg(red)^i(${ICONS}/dice1.xbm) "
fi
if [[ $pomodoro -ge $((pomodoro_block * 60 * 2)) ]]; then
echo -n "^fg(green)^i(${ICONS}/dice2.xbm) "
else
echo -n "^fg(red)^i(${ICONS}/dice2.xbm) "
fi
if [[ $pomodoro -ge $((pomodoro_block * 60 * 3)) ]]; then
echo -n "^fg(green)^i(${ICONS}/dice3.xbm) "
else
echo -n "^fg(red)^i(${ICONS}/dice3.xbm) "
fi
if [[ $pomodoro -ge $((pomodoro_block * 60 * 4)) ]]; then
echo -n "^fg(green)^i(${ICONS}/dice4.xbm) "
else
echo -n "^fg(red)^i(${ICONS}/dice4.xbm) "
fi
if [[ $pomodoro -ge $((pomodoro_block * 60 * 5)) ]]; then
echo -n "^fg(green)^i(${ICONS}/dice5.xbm) "
echo -n "^fg(yellow)^i(${ICONS}/monocle.xbm)"
else
echo -n "^fg(red)^i(${ICONS}/dice5.xbm) "
echo -n "^fg(red)^i(${ICONS}/monocle.xbm)"
fi
else
echo -n "^fg(yellow)^i(${ICONS}/monocle.xbm)"
fi
echo -n "^fg(orange)] "
#echo -n "^fg(magenta)$pomodoro "
echo -n "^bg()^fg(green)^i(${ICONS}/cpu13.xbm) ${cpu} "
echo -n "^fg(yellow)^i(${ICONS}/mem1.xbm) "
echo -n "`echo ${mem[0]} | gdbar -s o -h $bh -w $bw -fg darkcyan -bg darkgreen` "
echo -n "`echo ${mem[1]} | gdbar -s o -h $bh -w $bw -fg darkred -bg darkgreen` "
echo -n "`echo ${mem[2]} | gdbar -s o -h $bh -w $bw -fg darkgreen -bg darkgreen` "
echo -n "`echo ${mem[3]} | gdbar -s o -h $bh -w $bw -fg darkmagenta -bg darkgreen` "
echo -n "`echo ${mem[4]} | gdbar -s o -h $bh -w $bw -fg red -bg darkgreen` "
echo -n "^fg(orange)^i(${ICONS}/file1.xbm) "
echo -n "`echo $disk | gdbar -s o -h $bh -w $bw -fg brown -bg darkgreen` "
echo -n "^fg(magenta)^i(${ICONS}/downarrow1.xbm) "
echo -n "^fg(darkmagenta)${net[1]} kB/s "
echo -n "^fg(magenta)^i(${ICONS}/uparrow1.xbm) "
echo -n "^fg(darkmagenta)${net[2]} kB/s "
# get herbstluftwm tag status
tagtext="${separator}"
for i in "${TAGS[@]}" ; do
tagit=true
case ${i:0:1} in
':') tagtext+="^bg()^fg(#ffffff)" ;;
'#'|'%') tagtext+="^bg($selbg)^fg($selfg)" ;;
'+'|'-') tagtext+="^bg($selfg)^fg($selbg)" ;;
'!') tagtext+="^bg(#FF0675)^fg(#141414)" ;;
*) tagit=false ;;
esac
if $tagit; then
tagtext+="^ca(1, herbstclient focus_monitor $monitor && herbstclient use ${i:1}) ${i:1} ^ca()"
fi
done
tagtext+="${separator}"
# compute width of right aligned text for absolute positioning
# escape '^' in window title for dzen2
# nasty sed regex extracts text (removes '^^' and '^xx(xx)')
windowtitle=$(echo ${windowtitle} | iconv -f utf8 -t ascii//TRANSLIT | cut -c-${window_title_len})
right="^bg()^fg()${windowtitle//^/^^}${tagtext}^bg()${date}${separator}"
right_text=$(echo -n "$right" | sed -r 's.\^(\^|[^(^]*\([^)^]*\))..g')
width=$(textwidth "$font" "$right_text")
echo -n "^pa($((pw - width)))${right}"
echo # eol
# wait for and process the next event
read line || break
#echo "-> $line" >> /tmp/foo # debug
cmd=( $line )
case "${cmd[0]}" in
tag*)
TAGS=( $(herbstclient tag_status $monitor) )
;;
pomodoro)
pomodoro="${cmd[@]:1}"
;;
date)
date="${cmd[@]:1}"
;;
cpu)
cpu="${cmd[@]:1}"
;;
net)
net=( ${cmd[@]:1} )
;;
mem)
mem=( ${cmd[@]:1} )
;;
disk)
disk="${cmd[@]:1}"
;;
quit_panel)
exit
;;
togglehidepanel)
echo "^togglehide()"
if $visible; then
visible=false
herbstclient pad $monitor 0
else
visible=true
herbstclient pad $monitor $ph
fi
;;
reload)
exit
;;
focus_changed|window_title_changed)
windowtitle="${cmd[@]:2}"
;;
esac
done
} \
2> /dev/null | \
dzen2 -x $x \
-y $y \
-w $pw \
-h $ph \
-fn "$font" \
-ta l \
-bg "$bgcolor" \
-fg "#efefef" \
-e "Button3"