-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
statusbar.go
76 lines (65 loc) · 1.62 KB
/
statusbar.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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"
)
type element interface {
value() string
}
type statusbar struct {
elements []element
// configuration properties
Dzen2 []string
Gmail []gmailAccount
}
func run(conf string) error {
var bar statusbar
file, err := os.ReadFile(conf)
if err != nil {
return fmt.Errorf("failed to read config file: %s - %s", conf, err)
}
if err := json.Unmarshal(file, &bar); err != nil {
return fmt.Errorf("failed to unmarshal config file: %s - %s", conf, err)
}
bar.elements = append(bar.elements, keyboard())
if len(bar.Gmail) > 0 {
bar.elements = append(bar.elements, emails(bar.Gmail))
}
bar.elements = append(bar.elements, network())
bar.elements = append(bar.elements, cpu_temp())
bar.elements = append(bar.elements, power())
bar.elements = append(bar.elements, cpu_load())
bar.elements = append(bar.elements, memory_usage())
bar.elements = append(bar.elements, date())
cmd := exec.Command("dzen2", bar.Dzen2...)
stdin, err := cmd.StdinPipe()
if err != nil {
return fmt.Errorf("failed to create stdin pipe: %s", err)
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start dzen2 command: %s", err)
}
// run the iteration loop
go func() {
for {
if _, e := stdin.Write([]byte(strings.Join(bar.iterate(), " ") + "\n")); e != nil {
log.Printf("probably the pipe closed: %s", e)
break
}
time.Sleep(time.Second * INTERVAL_SECS)
}
}()
return cmd.Wait()
}
func (bar *statusbar) iterate() []string {
var res []string
for _, el := range bar.elements {
res = append(res, el.value())
}
return res
}