-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdzen.go
402 lines (332 loc) · 10.3 KB
/
dzen.go
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2017 Sergio Correia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"time"
)
type colorInfo struct {
SidebarsBg string
SidebarsFg string
Urgent string
Key string
Value string
Bg string
}
type dzenInfo struct {
name string
cmd *exec.Cmd
args []string
stdin io.WriteCloser
hidden bool
}
type popupConfig struct {
Info string
Clock string
Weather string
User string
}
type barConfig struct {
Height int
LeftBarWidth int
Contiguous string
Position string
}
type info struct {
icon string
key string
value string
format *string
formatted string
length int
}
var (
formatDefault string
formatUrgent string
mainbarWidth = 500
leftBarWidth = 0
barHeight = 15
contiguousBar = true
isTopBar = true
dzenMainbar []dzenInfo
dzenLeftbar []dzenInfo
err error
username string
)
const (
// Yeah, I am wondering about this as well.
barWidthMagic = 7.5
)
func initDzenBars() {
dzenLeftbar = make([]dzenInfo, len(monitors))
dzenMainbar = make([]dzenInfo, len(monitors))
}
func loadDzenColorFormats() {
formatDefault = fmt.Sprintf("^fg(%s)%%s ^fg(%s)%%s", config.Colors.Key, config.Colors.Value)
formatUrgent = fmt.Sprintf("^fg(%s)%%s %%s", config.Colors.Urgent)
}
func barWidthFromKey(key string) int {
w := 0
started := false
for i := range keys {
if keys[i] == key {
started = true
}
if started {
w += data[keys[i]].length
}
}
w += len(username) + 5
ret := int(float32(w) * barWidthMagic)
return ret
}
func leftBarContent(screen int) string {
if len(config.Popups.Info) > 0 {
return fmt.Sprintf("^ca(1,%s %d %d %d)^fg(%s)^bg(%s) info^fg(%s)^bg(%s) ^ca()\n", config.Popups.Info, (screen + 1), monitors[screen].width, monitors[screen].height, config.Colors.SidebarsFg, config.Colors.SidebarsBg, config.Colors.SidebarsBg, config.Colors.Bg)
}
return fmt.Sprintf("^fg(%s)^bg(%s) info^fg(%s)^bg(%s) \n", config.Colors.SidebarsFg, config.Colors.SidebarsBg, config.Colors.SidebarsBg, config.Colors.Bg)
}
func statusBarLen() int {
bar := ""
var key string
// Regular bar without any color formatting.
for i := range keys {
key = keys[i]
if collected, ok := data[key]; ok {
bar = fmt.Sprintf("%s %s %s", bar, collected.icon, collected.value)
}
}
return len(bar)
}
func statusBar(screen int) string {
bar := ""
var key string
for i := range keys {
key = keys[i]
if collected, ok := data[key]; ok {
switch {
case key == "clock" && len(config.Popups.Clock) > 0:
barWidth := barWidthFromKey(key)
bar = fmt.Sprintf("%s ^ca(1,%s %d %d %d %d)%s^ca()", bar, config.Popups.Clock, (screen + 1), monitors[screen].width, monitors[screen].height, barWidth, collected.formatted)
case key == "weather" && len(config.Popups.Weather) > 0:
barWidth := barWidthFromKey(key)
bar = fmt.Sprintf("%s ^ca(1,%s %d %d %d %d)%s^ca()", bar, config.Popups.Weather, (screen + 1), monitors[screen].width, monitors[screen].height, barWidth, collected.formatted)
default:
bar = fmt.Sprintf("%s %s", bar, collected.formatted)
}
}
}
if len(config.Popups.User) > 0 {
bar = fmt.Sprintf("%s ^ca(1,%s %d %d %d)^fg(%s)^fg(%s)^bg(%s) %s ^ca()", bar, config.Popups.User, (screen + 1), monitors[screen].width, monitors[screen].height, config.Colors.SidebarsBg, config.Colors.SidebarsFg, config.Colors.SidebarsBg, username)
} else {
bar = fmt.Sprintf("%s ^fg(%s)^fg(%s)^bg(%s) %s ", bar, config.Colors.SidebarsBg, config.Colors.SidebarsFg, config.Colors.SidebarsBg, username)
}
resizeDzenMainBar()
return bar
}
func updateStatusBar() {
collectStats()
var err error
status := ""
for i := 0; i < len(dzenMainbar); i++ {
status = fmt.Sprintf("%s\n", statusBar(i))
if _, err = io.WriteString(dzenMainbar[i].stdin, status); err != nil {
log.Printf("updateStatusBar: WriteString (bar #%d, status: %s) failed: %v", i, strings.Trim(status, "\n"), err)
}
}
}
func resizeDzenMainBar() {
if contiguousBar {
return
}
if newStatusbarLen := int(float32(statusBarLen()) * barWidthMagic); newStatusbarLen != mainbarWidth {
mainbarWidth = newStatusbarLen
drawDzenMainBar()
}
}
func reloadStatusBar() {
collectVolume("volume")
collectBrightness("brightness")
var err error
status := ""
for i := 0; i < len(monitors); i++ {
status = fmt.Sprintf("%s\n", statusBar(i))
if _, err = io.WriteString(dzenMainbar[i].stdin, status); err != nil {
log.Printf("reloadStatusBar: WriteString (bar #%d, status: %s) failed: %v", i, strings.Trim(status, "\n"), err)
}
}
}
func execDzen(args []string) (dzen *exec.Cmd, stdin io.WriteCloser, err error) {
dzen = exec.Command("dzen2", args...)
stdin, err = dzen.StdinPipe()
if err != nil {
return
}
dzen.Stdout = os.Stdout
dzen.Stderr = os.Stderr
err = dzen.Start()
return
}
func closeDzenByMonitor(monitor int) {
var err error
if _, err = closeDzenBar(&dzenLeftbar[monitor]); err != nil {
log.Printf("closeDzenByMonitor: closeDzenBar with left bar (%+v) failed for monitor %d: %v", dzenLeftbar[monitor], monitor, err)
}
if _, err = closeDzenBar(&dzenMainbar[monitor]); err != nil {
log.Printf("closeDzenByMonitor: closeDzenBar with main bar (%+v) failed for monitor %d: %v", dzenMainbar[monitor], monitor, err)
}
}
func closeDzenBar(dzen *dzenInfo) (bool, error) {
if dzen != nil && dzen.stdin != nil && dzen.cmd != nil {
var err error
if err = dzen.stdin.Close(); err != nil {
log.Printf("closeDzenBar: (dzenInfo: %+v) close failed: %v", dzen, err)
return false, err
}
dzen.hidden = true
if err = dzen.cmd.Wait(); err != nil {
log.Printf("closeDzenBar: (dzenInfo: %+v) wait failed: %v", dzen, err)
return true, err
}
return true, nil
}
return false, nil
}
func closeDzen(dzen []dzenInfo, delay bool) {
if delay {
// Delay for 1 second the closing of the old stdin.
time.Sleep(1 * time.Second)
}
var err error
for i := 0; i < len(dzen); i++ {
if _, err = closeDzenBar(&dzen[i]); err != nil {
log.Printf("closeDzen: error closing %+v: %v", dzen[i], err)
}
}
}
func drawDzenMainBarByMonitor(monitor int) (dzenInfo, error) {
if monitor < 0 || monitor >= len(monitors) {
return dzenInfo{}, errors.New("Monitor index incorrect")
}
width := monitors[monitor].width - leftBarWidth
x := leftBarWidth
if !contiguousBar {
x = monitors[monitor].width - mainbarWidth - 1
width = mainbarWidth
}
y := 0
if !isTopBar {
y = monitors[monitor].height - barHeight
}
dzenArgs := []string{"-xs", fmt.Sprintf("%d", (monitor + 1)), "-ta", "r", "-fn", config.Font, "-x", fmt.Sprintf("%d", x), "-y", fmt.Sprintf("%d", y), "-w", fmt.Sprintf("%d", width), "-h", fmt.Sprintf("%d", barHeight), "-bg", config.Colors.Bg, "-fg", config.Colors.Key, "-e", "button2=;"}
cmd, dzenStdin, err := execDzen(dzenArgs)
if err != nil {
return dzenInfo{}, err
}
status := fmt.Sprintf("%s\n", statusBar(monitor))
if _, err := io.WriteString(dzenStdin, status); err != nil {
log.Printf("drawDzenMainBarByMonitor: (monitor #%d, status: %s) failed: %v", monitor, strings.Trim(status, "\n"), err)
}
dzenBar := dzenInfo{name: fmt.Sprintf("main bar, monitor %d", monitor), cmd: cmd, args: dzenArgs, stdin: dzenStdin, hidden: false}
dzenMainbar[monitor] = dzenBar
return dzenBar, nil
}
func drawDzenMainBar() {
nscreens := len(monitors)
var oldBar dzenInfo
for i := 0; i < nscreens; i++ {
oldBar = dzenMainbar[i]
if _, err := drawDzenMainBarByMonitor(i); err == nil {
go closeDzenBar(&oldBar)
}
}
}
func drawDzenLeftBarByMonitor(monitor int) (dzenInfo, error) {
if monitor < 0 || monitor >= len(monitors) {
return dzenInfo{}, errors.New("Monitor index incorrect")
}
y := 0
if !isTopBar {
y = monitors[monitor].height - barHeight
}
dzenArgs := []string{"-xs", fmt.Sprintf("%d", (monitor + 1)), "-ta", "l", "-fn", config.Font, "-w", fmt.Sprintf("%d", leftBarWidth), "-h", fmt.Sprintf("%d", barHeight), "-x", "0", "-y", fmt.Sprintf("%d", y), "-bg", config.Colors.Bg, "-fg", config.Colors.Key, "-e", "button2=;"}
cmd, dzenStdin, err := execDzen(dzenArgs)
if err != nil {
return dzenInfo{}, err
}
status := fmt.Sprintf("%s", leftBarContent(monitor))
if _, err := io.WriteString(dzenStdin, status); err != nil {
log.Printf("drawDzenLeftBarByMonitor: (monitor #%d, status: %s) failed: %v", monitor, status, err)
}
dzenBar := dzenInfo{name: fmt.Sprintf("left bar, monitor %d", monitor), cmd: cmd, args: dzenArgs, stdin: dzenStdin, hidden: false}
dzenLeftbar[monitor] = dzenBar
return dzenBar, nil
}
func drawDzenLeftBar() {
nscreens := len(monitors)
var oldBar dzenInfo
for i := 0; i < nscreens; i++ {
oldBar = dzenLeftbar[i]
if _, err := drawDzenLeftBarByMonitor(i); err == nil {
go closeDzenBar(&oldBar)
}
}
}
func drawDzenBars() {
drawDzenLeftBar()
drawDzenMainBar()
resizeDzenMainBar()
}
func drawDzenByMonitor(monitor int) {
var err error
if _, err = drawDzenLeftBarByMonitor(monitor); err != nil {
log.Printf("drawDzenByMonitor: drawDzenLeftBarByMonitor failed with monitor %d: %v", monitor, err)
}
if _, err = drawDzenMainBarByMonitor(monitor); err != nil {
log.Printf("drawDzenByMonitor: drawDzenMainBarByMonitor failed with monitor %d: %v", monitor, err)
}
}
func updateDzenConfig() {
contiguousBar = true
if len(config.Bar.Contiguous) > 0 && config.Bar.Contiguous != "yes" {
contiguousBar = false
}
barHeight = 15
if config.Bar.Height > 0 {
barHeight = config.Bar.Height
}
leftBarWidth = 0
if config.Bar.LeftBarWidth > 0 {
leftBarWidth = config.Bar.LeftBarWidth
}
isTopBar = true
if config.Bar.Position == "bottom" {
isTopBar = false
}
}
func toggleBars(monitor int) {
hidden := dzenLeftbar[monitor].hidden
// close/drawDzenByMonitor take care of their hidden status.
if !hidden {
closeDzenByMonitor(monitor)
} else {
drawDzenByMonitor(monitor)
}
}