-
Notifications
You must be signed in to change notification settings - Fork 0
/
panel.sh
executable file
·303 lines (260 loc) · 9.48 KB
/
panel.sh
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
#!/bin/bash
# function wrapping the herbstclient command
hc() { "${herbstclient_command[@]:-herbstclient}" "$@" ;}
#==============================================================================
# theme
#==============================================================================
# load theme file
source $HOME/.config/herbstluftwm/panel_theme.sh
#==============================================================================
# variables
#==============================================================================
#-geometry---------------------------------------------------------------------
# get monitor dimensions
monitor=${1:-0}
geometry=( $(hc monitor_rect $monitor) )
# set panel dimensions
x=${geometry[0]}
y=${geometry[1]}
w=${geometry[2]}
h=$((${geometry[3]}/65))
#-settings---------------------------------------------------------------------
wireless_client="wicd-client"
dzen="dzen2 -x $x -y $y -w $w -h $h -fn $font -ta l -bg $panel_bg -fg $panel_fg"
#==============================================================================
# functions
#==============================================================================
icon() {
echo "^bg(${1})^fg(${2}) ^i($icon_path/${3}.xbm) ^bg()^fg()"
}
bar() {
bar_height=$(echo "$h*$bar_h" | bc)
echo $3 | gdbar -w $bar_w -h $bar_height $bar_style -bg ${1} -fg ${2}
}
function uniq_linebuffered() {
awk '$0 != l { print ; l=$0 ; fflush(); }' "$@"
}
#-widgets----------------------------------------------------------------------
battery_icon() {
if [ "$battery_status" == "Full" ]; then
echo $(icon $battery_icon_style $battery_full_icon)
elif [ "$battery_status" == "Charging" ]; then
echo $(icon $battery_icon_style $battery_charging_icon)
elif [ "$battery_status" == "Discharging" ]; then
echo $(icon $battery_icon_style $battery_discharging_icon)
else
echo $(icon $battery_icon_style $battery_missing_icon)
fi
}
battery_percentage() {
# TODO support more than one battery
if [ -z "${1}" ]; then
echo "ac"
elif [ ${1} -le $battery_critical_percentage ] &&
[ $battery_status == "Discharging" ]; then
$(bar $battery_critical_fg_color $battery_critical_bg_color ${1})
else
echo -n "$(bar $bar_bg $bar_fg ${1})"
fi
}
battery() {
# TODO support more than one battery
battery_status=$(acpi -b | egrep -o "Battery 0.*" |\
egrep -o "(Full|Charging|Discharging|Unknown)")
echo $(battery_icon)
}
playing() {
mpc -h passwd@localhost -f "$now_playing_format" current
}
temp() {
echo -n "$(bar $temp_bar_bg $temp_bar_fg ${1})"
}
volume() {
volume=$(amixer get Master | egrep -o "[0-9]+%" | tr -d "%")
echo -n "^ca(1, amixer -q set Master 5%-)"
echo -n "^ca(3, amixer -q set Master 5%+)"
echo -n "^ca(2, amixer -q set Master toggle)"
if [ -z "$(amixer get Master | grep "\[on\]")" ]; then
echo -n "$(bar $bar_bg $bar_fg $volume)"
else
echo -n "$(bar $bar_bg $bar_fg $volume)"
fi
echo "^ca()^ca()^ca()"
}
#==============================================================================
# execution
#==============================================================================
# leave room for the panel
hc pad $monitor $h
#-event-generating-------------------------------------------------------------
{
# conky
conky -c $HOME/.conky/statusbar | while read -r conky_reply; do
echo -e "conky $conky_reply";
done > >(uniq_linebuffered) &
childpid=$!
# herbstluftwm event
hc --idle
kill $childpid
} | tee /dev/stderr | {
# get tags from herbstluft
tags=( $(hc tag_status $monitor) )
date=""
conky=""
windowtitle=""
#-draw-tags--------------------------------------------------------------------
while true ; do
for i in "${tags[@]}" ; do
case ${i:0:1} in
'.') # tag is empty
echo -n "^bg($unused_bg)^fg($unused_fg)"
;;
':') # tag is not empty
echo -n "^bg($used_bg)^fg($used_fg)"
;;
'#') # currently focused on active monitor
echo -n "^bg($active_bg)^fg($active_fg)"
;;
'+') # not focused but on active monitor
echo -n "^bg($inactive_bg)^fg($inactive_fg)"
;;
# '-', '%' are still missing
'!') # urgent tag
echo -n "^bg($urgent_bg)^fg($urgent_fg)"
;;
*)
echo -n "^bg()^fg()"
;;
esac
# clickable tags
echo -n "^ca(1,\"herbstclient\" "
echo -n "focus_monitor \"$monitor\" && "
echo -n "\"${herbstclient_command[@]:-herbstclient}\" "
echo -n "use \"${i:1}\") ${i:1} ^ca()"
done
echo -n "$sep"
# draw window title
echo -n "^bg($title_bg)^fg($title_fg) ${windowtitle//^/^^}"
#==============================================================================
# draw right part of bar
#==============================================================================
#-parse-conky-stats------------------------------------------------------------
# parse conky stats
IFS='|' read -ra conky_stats <<< "$conky"
for i in "${conky_stats[@]}"; do
read -ra stat <<< "$i"
case "${stat[0]}" in
UPTIME)
uptime="${stat[@]:1}"
;;
CPU)
cpu="${stat[@]:1}"
;;
TIME)
time="${stat[@]:1}"
;;
DATE)
date="${stat[@]:1}"
;;
BAT)
bat="${stat[@]:1}"
;;
TEMPCPU)
temp="${stat[@]:1}"
;;
esac
done
right="$sep_style"
text="$sep"
width=0
#-draw-volume------------------------------------------------------------------
right="$right$(icon $volume_icon_style $volume_icon)"
right="$right$padding"
right="$right$(volume)"
right="$right$sep_style"
text="$text$padding$sep"
width=$(($width+$icon_width+$bar_w))
#-draw-cpu---------------------------------------------------------------------
if [ ${#cpu} == 2 ]; then
# pad cpu string
cpu=" $cpu"
fi
right="$right$(icon $cpu_icon_style $cpu_icon)"
right="$right$padding"
right="$right$cpu_style$cpu"
right="$right$sep_style"
text="$text$padding$cpu$sep"
width=$(($width+$icon_width))
#-draw-cpu-temp----------------------------------------------------------------
right="$right$(icon $temp_icon_style $temp_icon)"
right="$right$padding"
right="$right$(temp $temp)"
right="$right$sep_style"
text="$text$padding$sep"
width=$(($width+$icon_width+$bar_w))
#-draw-mpd---------------------------------------------------------------------
playing=$(now_playing)
if [ ! -z "$playing" ]; then
right="$right$(icon $playing_icon_style $now_playing_icon)"
right="$right$padding"
right="$right$playing_style$playing"
right="$right$sep_syle"
text="$text$padding$playing$sep"
width=$(($width+$icon_width))
fi
#-draw-battery-----------------------------------------------------------------
right="$right$(battery $bat)"
right="$right$padding"
right="$right$(battery_percentage $bat)"
right="$right$sep_style"
text="$text$padding$sep"
width=$(($width+$icon_width+$bar_w))
#-draw-clock-------------------------------------------------------------------
right="$right$(icon $clock_icon_style $clock_icon)"
right="$right$padding"
right="$right$date_style$date"
right="$right$padding"
right="$right$clock_style$time"
right="$right$sep_style"
text="$text$padding$time$padding$date$sep"
width=$(($width+$icon_width))
#-finish-output----------------------------------------------------------------
width=$(($width+$(textwidth "$font" "$text")))
echo -n "^pa($(($w - $width)))$right"
echo
#==============================================================================
# handle events
#==============================================================================
# wait for next event
read line || break
cmd=( $line )
# find out event origin
echo "Command: ${cmd[0]}" >&2
case "${cmd[0]}" in
tag*)
# reset tags
tags=( $(hc tag_status $monitor) )
;;
date)
# reset date
time=$(date +"$clock_format")
date=$(date +"$date_format")
;;
conky*)
conky="${cmd[@]:1}"
;;
quit_panel)
exit
;;
reload)
exit
;;
focus_changed|window_title_changed)
windowtitle="${cmd[@]:2}"
;;
esac
done
# pipe result to dzen
#} 2> /dev/null | $dzen
# use this for debug messages
} | $dzen