-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
237 lines (202 loc) · 7.67 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os/exec"
"strconv"
"strings"
"time"
)
var COLORS = map[string]string {
"{{ColorFg}}" :"#FFdddddd", "{{ColorBg}}" :"#FF101010",
"{{ColorFocusedOccupiedFg}}" :"#FFdddddd", "{{ColorFocusedOccupiedBg}}" :"#FF404040",
"{{ColorFocusedFreeFg}}" :"#FFdddddd", "{{ColorFocusedFreeBg}}" :"#FF101010",
"{{ColorFocusedUrgentFg}}" :"#FFdddddd", "{{ColorFocusedUrgentBg}}" :"#FFe84f4f",
"{{ColorOccupiedFg}}" :"#FFdddddd", "{{ColorOccupiedBg}}" :"#FF404040",
"{{ColorFreeFg}}" :"#FF555555", "{{ColorFreeBg}}" :"#FF101010",
"{{ColorUrgentFg}}" :"#FFdddddd", "{{ColorUrgentBg}}" :"#FFd23d3d",
"{{ColorLayoutFg}}" :"#FFdddddd", "{{ColorLayoutBg}}" :"#FF101010",
"{{ColorTitleFg}}" :"#FF555555", "{{ColorTitleBg}}" :"#FF101010",
"{{ColorStatusFg}}" :"#FFdddddd", "{{ColorStatusBg}}" :"#FF101010",
}
type Element struct {
name string
result string
format string
}
/*
* Xtitle is used to pass the stream of data from xtitle -s into the ch string.
* It passes all of its errors through the errCh
* To invoke this function: go Xtitle(2000, ch, errCh)
*/
func Xtitle(interval int64, ch chan Element, errCh chan error) {
for {
out, err := exec.Command("xtitle", "-f", "'%s'").Output()
if err != nil {errCh <- err;return}
outStr := strings.TrimSpace(string(out))
result := "%{F{{ColorTitleFg}}B{{ColorTitleBg}}}" + outStr
ch <-Element{name:"xtitle",result:string(result)}
time.Sleep(time.Duration(interval) * time.Millisecond)
}
}
/*
* Load is used to pass the results of cat /proc/loadavg into the ch string every 'interval' milliseconds.
* It passes all of its errors through the errCh
* To invoke this function: go Load(2000, ch, errCh)
*/
func Load(interval int, ch chan Element, errCh chan error) {
for {
out, err := ioutil.ReadFile("/proc/loadavg")
if err != nil {errCh <- err;return}
outStr := strings.Fields(string(out))
result := "%{F{{ColorStatusFg}}B{{ColorStatusBg}}}" + fmt.Sprintf("Load: %s %s %s", outStr[0], outStr[1], outStr[2])
ch <-Element{name:"load",result:string(result)}
time.Sleep(time.Duration(interval) * time.Millisecond)
}
}
/*
* Memory is used to pass the results of free -m into the ch string every 'interval' milliseconds.
* It passes all of its errors through the errCh
* To invoke this function: go Memory(2000, ch, errCh)
*/
func Memory(interval int64, ch chan Element, errCh chan error) {
for {
out, err := exec.Command("free", "-m").Output()
if err != nil {errCh <- err;return}
outStr := strings.Fields(strings.Split(string(out), "\n")[1])
total, err := strconv.ParseFloat(outStr[1], 32)
if err != nil {errCh <- err;return}
used, err := strconv.ParseFloat(outStr[3], 32)
if err != nil {errCh <- err;return}
result := "%{F{{ColorStatusFg}}B{{ColorStatusBg}}}" + fmt.Sprintf("Mem: %.2f/%.2fG", used/1024, total/1024)
ch <-Element{name:"memory",result:string(result)}
time.Sleep(time.Duration(interval) * time.Millisecond)
}
}
/*
* Volume is used to pass the processed results of pacmd dump into the ch string every 'interval' milliseconds.
* It passes all of its errors through the errCh
* To invoke this function: go Volume(2000, ch, errCh)
*/
func Volume(interval int64, ch chan Element, errCh chan error) {
for {
out, err := exec.Command("pacmd", "dump").Output()
if err != nil {errCh <- err;return}
outStr := strings.Split(string(out), "\n")
var desiredInt uint64
for _, value := range outStr {
if strings.HasPrefix(value, "set-sink-volume") {
strs := strings.Fields(value)
desiredInt, err = strconv.ParseUint(strs[len(strs)-1][2:], 16, 32)
if err != nil {errCh <- err;return}
}
}
result :="%{F{{ColorStatusFg}}B{{ColorStatusBg}}}" + fmt.Sprintf("Vol: %d%%", int(float64(desiredInt)/655.36))
ch <-Element{name:"volume",result:result}
time.Sleep(time.Duration(interval) * time.Millisecond)
}
}
/*
* Time is used to pass the formatted results of time.Now() into the ch channel string every 'interval' milliseconds.
* It passes all of its errors through the errCh
* To invoke this function: go Time(2000, ch, errCh)
*/
func Time(interval int, ch chan Element, errCh chan error) {
for {
result :="%{F{{ColorStatusFg}}B{{ColorStatusBg}}}" + time.Now().Format("Mon, Jan 2, 2006 15:04:05")
ch <-Element{name:"time",result:string(result)}
time.Sleep(time.Duration(interval) * time.Millisecond)
}
}
/*
* Temp is used to pass the contents of the path specified into the ch channel string every 'interval' milliseconds.
* It passes all of its errors through the errCh
* To invoke this function: go Temp(2000, ch, errCh)
*/
func Temp(interval int, ch chan Element, errCh chan error) {
for {
out, err := ioutil.ReadFile("/sys/bus/platform/devices/coretemp.0/temp1_input")
if err != nil {errCh <- err;return}
temp, err := strconv.ParseInt(string(out[:len(out)-4]), 10, 16)
if err != nil {errCh <- err;return}
result := "%{F{{ColorStatusFg}}B{{ColorStatusBg}}}" + fmt.Sprintf("Temp: %dC", temp)
ch <-Element{name:"temp",result:result}
time.Sleep(time.Duration(interval) * time.Millisecond)
}
}
/*
* StatusWm takes the stream of information from bspc control --subscribe and pass it to ch channel string every 'interval' milliseconds.
* It passes all of its errors through the errCh
* To invoke this function: go StatusWm(2000, ch, errCh)
*/
func StatusWm(interval int64, ch chan Element, errCh chan error) {
for {
byt, err := exec.Command("bspc", "control", "--get-status").Output()
if err != nil {errCh <- err;return}
var out string
temp := strings.TrimSpace(string(byt))
tempSlice := strings.Split(temp, ":")
for _, str := range tempSlice {
switch str[0] {
case 'O':
out += "%{F{{ColorFocusedOccupiedFg}} U{{ColorFocusedOccupiedFg}} B{{ColorFocusedOccupiedBg}} +u} " + str[1:] + " %{-uB{{ColorBg}}}"
case 'F':
out += "%{F{{ColorFocusedFreeFg}} U{{ColorFocusedFreeFg}} B{{ColorFocusedFreeBg}} +u} " + str[1:] + " %{-uB{{ColorBg}}}"
case 'U':
out += "%{F{{ColorFocusedUrgentFg}} U{{ColorFocusedUrgentFg}} B{{ColorFocusedUrgentBg}} +u} " + str[1:] + " %{-uB{{ColorBg}}}"
case 'o':
out += "%{F{{ColorOccupiedFg}} B{{ColorOccupiedBg}}} " + str[1:] + " %{B{{ColorBg}}}"
case 'f':
out += "%{F{{ColorFreeFg}} B{{ColorFreeBg}}} " + str[1:] + " %{B{{ColorBg}}}"
case 'u':
out += "%{F{{ColorUrgentFg}} B{{ColorUrgentBg}}} " + str[1:] + " %{B{{ColorBg}}}"
case 'L':
out += "%{F{{ColorLayoutFg}} B{{ColorLayoutBg}}} " + strings.ToUpper(string(str[1])) + " %{B{{ColorBg}}}"
}
}
ch <- Element{name:"wm",result:out}
time.Sleep(time.Duration(interval) * time.Millisecond)
}
}
func DisplayInfo(format string) func(Element) {
var outString string
elements := make(map[string] Element)
return func(item Element) {
elements[item.name] = item
outString = format
for key, value := range elements {
outString = strings.Replace(outString, "{{"+key+"}}", value.result, -1)
}
for key, value := range COLORS {
outString = strings.Replace(outString, key, value, -1)
}
fmt.Println(outString)
}
}
func main() {
ch := make(chan Element)
errCh := make(chan error)
go Load(10000, ch, errCh)
go Memory(10000, ch, errCh)
go Volume(5000, ch, errCh)
go Time(1000, ch, errCh)
go Temp(1000, ch, errCh)
go Xtitle(200, ch, errCh)
go StatusWm(200, ch, errCh)
format := fmt.Sprint("%{l}{{wm}} %{c}{{xtitle}} %{r}{{volume}} | {{memory}} | {{load}} | {{temp}} | {{time}}")
//format = fmt.Sprint("%{S0}", format, "%{S1}", format, "%{S2}", format)
format = fmt.Sprint("%{S0}", format)
DisplayClosure := DisplayInfo(format)
for {
select {
case err := <-errCh:
log.Fatal(err)
default:
select {
case item := <-ch:
go DisplayClosure(item)
}
}
}
}