aboutsummaryrefslogtreecommitdiffstats
path: root/config/herbstluftwm/panel.sh
blob: 763c668b44fcbf773f8bed928d4df7f5dc266781 (plain)
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
#!/bin/bash

hc() {
	"${herbstclient_command[@]:-herbstclient}" "$@" ;
}

dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source "$dir/theme.sh"
source "$dir/panel_indicators.sh"

monitor=${1:-0}

geometry=( $(herbstclient monitor_rect "$monitor") )
if [ -z "$geometry" ] ;then
    echo "Invalid monitor $monitor"
    exit 1
fi
# geometry has the format W H X Y
x=$(echo "${geometry[0]} + $window_p" | bc)
y=$(echo "${geometry[1]} + $window_p" | bc)
panel_width=$(echo "${geometry[2]} - (2 * $window_p)" | bc)
bar_opts="-g ${panel_width}x${panel_h}+${x}+${y} -f $font,$font_sec -u 2 -B $acolor_bg -F $acolor_fg"

hc pad $monitor $(echo "$panel_h + $window_p" | bc)

if awk -Wv 2>/dev/null | head -1 | grep -q '^mawk'; then
    # mawk needs "-W interactive" to line-buffer stdout correctly
    # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=593504
    uniq_linebuffered() {
      awk -W interactive '$0 != l { print ; l=$0 ; fflush(); }' "$@"
    }
else
    # other awk versions (e.g. gawk) issue a warning with "-W interactive", so
    # we don't want to use it there.
    uniq_linebuffered() {
      awk '$0 != l { print ; l=$0 ; fflush(); }' "$@"
    }
fi

{
    ### Event generator ###
    #   <eventname>\t<data> [...]
    # e.g.
    #   date    ^fg(#efefef)18:33^fg(#909090), 2013-10-^fg(#efefef)29
    while true ; do
		music
		volume
		network
		battery
		clock
        sleep 1 || break
    done > >(uniq_linebuffered) &
    childpid=$!
    hc --idle
    kill $childpid
} 2> /dev/null | {

    IFS=$'\t' read -ra tags <<< "$(hc tag_status $monitor)"
    visible=true
    date=""
	volume=""
	battery=""
	net=""
    windowtitle=""
    while true ; do
        separator="%{F$acolor_accent}|%{F-}"
        # draw tags
        for i in "${tags[@]}" ; do
            case ${i:0:1} in
                '#')
                    echo -n "%{U$acolor_accent+u}%{F$acolor_fg}"
                    ;;
                '+')
                    echo -n "%{U$acolor_fg+u}%{F$acolor_fg}"
                    ;;
                ':')
                    echo -n "%{F$acolor_fg}"
                    ;;
                '!')
                    echo -n "%{B$acolor_accent}%{U$acolor_accent+u}%{F$acolor_bg}"
                    ;;
                *)
                    echo -n "%{F$acolor_fg}"
                    ;;
            esac
            echo -n "%{A:tag,${i:1}:} ${i:1} %{A}%{F-}%{U-u}%{B-}"
        done
        echo -n "$separator%{F-}%{B-} "
        echo -n "${windowtitle//^/^^}"

		#Right part of panel
        right="$music$volume$net$battery$date "
        echo -n "%{r}$right"

		#DO NOT REMOVE THIS ECHO
		echo

        # wait for next event
        IFS=$'\t' read -ra cmd || break
        case "${cmd[0]}" in
            tag*)
                #echo "resetting tags" >&2
                IFS=$'\t' read -ra tags <<< "$(hc tag_status $monitor)"
                ;;
			music)
				music="${cmd[@]:1}"
				if [ $music == "off" ] ; then
					music=""
				else
					music="$music $separator%{B-} "
				fi
				;;
			volume)
				volume="${cmd[@]:1}"
				if [ $volume == "off" ] ; then
					volume=""
				else
					volume="$volume $separator%{B-} "
				fi
				;;
			net)
				net="${cmd[@]:1}"
				if [ $net = "off" ] ; then
					net=""
				else
					net="$net $separator%{B-} "
				fi
				;;
			battery)
				battery="${cmd[@]:1}"
				if [ $battery == "off" ] ; then
					battery=""
				else
					battery="$battery $separator%{B-} "
				fi
				;;
            date)
                #echo "resetting date" >&2
                date="${cmd[@]:1}"
                ;;
            focus_changed|window_title_changed)
                windowtitle="${cmd[@]:2}"
                ;;
        esac
    done
} 2> /dev/null | bar $bar_opts | {
	#Handle clickable areas
	while read line; do
		IFS=',' read -a c <<< $(echo $line)
		case "${c[0]}" in
			tag)
				herbstclient use "${c[1]}"
				echo "herbstclient use \"${c[1]}\""
				;;
			*)
				echo "${c[0]}: not valid command"
				;;
		esac
	done
}