Panel.

Commit
eb810761711ff50c91ce028429a791e569b6fe45
Author
Marius Peter <marius.peter@tutanota.com>
Author date
Committer
Marius Peter <marius.peter@tutanota.com>
Committer date
panel.sh
index 00000000..88f56ef0 000000..100755
@@ -0,0 +1,191 @@
1 Added: #!/usr/bin/env bash
2 Added:
3 Added: quote() {
4 Added: local q="$(printf '%q ' "$@")"
5 Added: printf '%s' "${q% }"
6 Added: }
7 Added:
8 Added:
9 Added: hc_quoted="$(quote "${herbstclient_command[@]:-herbstclient}")"
10 Added: hc() { "${herbstclient_command[@]:-herbstclient}" "$@" ;}
11 Added: monitor=${1:-0}
12 Added: geometry=( $(hc monitor_rect "$monitor") )
13 Added: if [ -z "$geometry" ] ;then
14 Added: echo "Invalid monitor $monitor"
15 Added: exit 1
16 Added: fi
17 Added: # geometry has the format W H X Y
18 Added: x=${geometry[0]}
19 Added: y=${geometry[1]}
20 Added: panel_width=${geometry[2]}
21 Added: panel_height=32
22 Added: font="-*-hack-*-*-*-*-12-*-*-*-*-*-*-*"
23 Added: # extract colors from hlwm and omit alpha-value
24 Added: bgcolor=$(hc get frame_border_normal_color|sed 's,^\(\#[0-9a-f]\{6\}\)[0-9a-f]\{2\}$,\1,')
25 Added: selbg=$(hc get window_border_active_color|sed 's,^\(\#[0-9a-f]\{6\}\)[0-9a-f]\{2\}$,\1,')
26 Added: selfg='#101010'
27 Added:
28 Added: ####
29 Added: # Try to find textwidth binary.
30 Added: # In e.g. Ubuntu, this is named dzen2-textwidth.
31 Added: if which textwidth &> /dev/null ; then
32 Added: textwidth="textwidth";
33 Added: elif which dzen2-textwidth &> /dev/null ; then
34 Added: textwidth="dzen2-textwidth";
35 Added: elif which xftwidth &> /dev/null ; then # For guix
36 Added: textwidth="xftwidth";
37 Added: else
38 Added: echo "This script requires the textwidth tool of the dzen2 project."
39 Added: exit 1
40 Added: fi
41 Added: ####
42 Added: # true if we are using the svn version of dzen2
43 Added: # depending on version/distribution, this seems to have version strings like
44 Added: # "dzen-" or "dzen-x.x.x-svn"
45 Added: if dzen2 -v 2>&1 | head -n 1 | grep -q '^dzen-\([^,]*-svn\|\),'; then
46 Added: dzen2_svn="true"
47 Added: else
48 Added: dzen2_svn=""
49 Added: fi
50 Added:
51 Added: if awk -Wv 2>/dev/null | head -1 | grep -q '^mawk'; then
52 Added: # mawk needs "-W interactive" to line-buffer stdout correctly
53 Added: # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=593504
54 Added: uniq_linebuffered() {
55 Added: awk -W interactive '$0 != l { print ; l=$0 ; fflush(); }' "$@"
56 Added: }
57 Added: else
58 Added: # other awk versions (e.g. gawk) issue a warning with "-W interactive", so
59 Added: # we don't want to use it there.
60 Added: uniq_linebuffered() {
61 Added: awk '$0 != l { print ; l=$0 ; fflush(); }' "$@"
62 Added: }
63 Added: fi
64 Added:
65 Added: hc pad $monitor $panel_height
66 Added:
67 Added: {
68 Added: ### Event generator ###
69 Added: # based on different input data (mpc, date, hlwm hooks, ...) this generates events, formed like this:
70 Added: # <eventname>\t<data> [...]
71 Added: # e.g.
72 Added: # date ^fg(#efefef)18:33^fg(#909090), 2013-10-^fg(#efefef)29
73 Added:
74 Added: #mpc idleloop player &
75 Added: while true ; do
76 Added: # "date" output is checked once a second, but an event is only
77 Added: # generated if the output changed compared to the previous run.
78 Added: date +$'date\t^fg(#efefef)%H:%M^fg(#909090), %Y-%m-^fg(#efefef)%d'
79 Added: sleep 1 || break
80 Added: done > >(uniq_linebuffered) &
81 Added: childpid=$!
82 Added: hc --idle
83 Added: kill $childpid
84 Added: } 2> /dev/null | {
85 Added: IFS=$'\t' read -ra tags <<< "$(hc tag_status $monitor)"
86 Added: visible=true
87 Added: date=""
88 Added: windowtitle=""
89 Added: while true ; do
90 Added:
91 Added: ### Output ###
92 Added: # This part prints dzen data based on the _previous_ data handling run,
93 Added: # and then waits for the next event to happen.
94 Added:
95 Added: separator="^bg()^fg($selbg)|"
96 Added: # draw tags
97 Added: for i in "${tags[@]}" ; do
98 Added: case ${i:0:1} in
99 Added: '#')
100 Added: echo -n "^bg($selbg)^fg($selfg)"
101 Added: ;;
102 Added: '+')
103 Added: echo -n "^bg(#9CA668)^fg(#141414)"
104 Added: ;;
105 Added: ':')
106 Added: echo -n "^bg()^fg(#ffffff)"
107 Added: ;;
108 Added: '!')
109 Added: echo -n "^bg(#FF0675)^fg(#141414)"
110 Added: ;;
111 Added: *)
112 Added: echo -n "^bg()^fg(#ababab)"
113 Added: ;;
114 Added: esac
115 Added: if [ ! -z "$dzen2_svn" ] ; then
116 Added: # clickable tags if using SVN dzen
117 Added: echo -n "^ca(1,$hc_quoted focus_monitor \"$monitor\" && "
118 Added: echo -n "$hc_quoted use \"${i:1}\") ${i:1} ^ca()"
119 Added: else
120 Added: # non-clickable tags if using older dzen
121 Added: echo -n " ${i:1} "
122 Added: fi
123 Added: done
124 Added: echo -n "$separator"
125 Added: echo -n "^bg()^fg() ${windowtitle//^/^^}"
126 Added: # small adjustments
127 Added: right="$separator^bg() $date $separator"
128 Added: right_text_only=$(echo -n "$right" | sed 's.\^[^(]*([^)]*)..g')
129 Added: # get width of right aligned text.. and add some space..
130 Added: width=$($textwidth "$font" "$right_text_only ")
131 Added: echo -n "^pa($(($panel_width - $width)))$right"
132 Added: echo
133 Added:
134 Added: ### Data handling ###
135 Added: # This part handles the events generated in the event loop, and sets
136 Added: # internal variables based on them. The event and its arguments are
137 Added: # read into the array cmd, then action is taken depending on the event
138 Added: # name.
139 Added: # "Special" events (quit_panel/togglehidepanel/reload) are also handled
140 Added: # here.
141 Added:
142 Added: # wait for next event
143 Added: IFS=$'\t' read -ra cmd || break
144 Added: # find out event origin
145 Added: case "${cmd[0]}" in
146 Added: tag*)
147 Added: #echo "resetting tags" >&2
148 Added: IFS=$'\t' read -ra tags <<< "$(hc tag_status $monitor)"
149 Added: ;;
150 Added: date)
151 Added: #echo "resetting date" >&2
152 Added: date="${cmd[@]:1}"
153 Added: ;;
154 Added: quit_panel)
155 Added: exit
156 Added: ;;
157 Added: togglehidepanel)
158 Added: currentmonidx=$(hc list_monitors | sed -n '/\[FOCUS\]$/s/:.*//p')
159 Added: if [ "${cmd[1]}" -ne "$monitor" ] ; then
160 Added: continue
161 Added: fi
162 Added: if [ "${cmd[1]}" = "current" ] && [ "$currentmonidx" -ne "$monitor" ] ; then
163 Added: continue
164 Added: fi
165 Added: echo "^togglehide()"
166 Added: if $visible ; then
167 Added: visible=false
168 Added: hc pad $monitor 0
169 Added: else
170 Added: visible=true
171 Added: hc pad $monitor $panel_height
172 Added: fi
173 Added: ;;
174 Added: reload)
175 Added: exit
176 Added: ;;
177 Added: focus_changed|window_title_changed)
178 Added: windowtitle="${cmd[@]:2}"
179 Added: ;;
180 Added: #player)
181 Added: # ;;
182 Added: esac
183 Added: done
184 Added:
185 Added: ### dzen2 ###
186 Added: # After the data is gathered and processed, the output of the previous block
187 Added: # gets piped to dzen2.
188 Added:
189 Added: } 2> /dev/null | dzen2 -w $panel_width -x $x -y $y -fn "$font" -h $panel_height \
190 Added: -e "button3=;button4=exec:$hc_quoted use_index -1;button5=exec:$hc_quoted use_index +1" \
191 Added: -ta l -bg "$bgcolor" -fg '#efefef'