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 (
"io"
"net/http"
"os"
"regexp"
"strconv"
"time"
"barista.run"
"barista.run/bar"
"barista.run/colors"
"barista.run/modules/battery"
"barista.run/modules/clock"
"barista.run/modules/diskspace"
"barista.run/modules/funcs"
"barista.run/modules/netinfo"
"barista.run/modules/volume"
"barista.run/modules/volume/pulseaudio"
"barista.run/modules/wlan"
"barista.run/outputs"
"barista.run/pango"
"barista.run/pango/icons/mdi"
)
func main() {
// Constants
colors.LoadFromMap(map[string]string{
// Color palette of Cezanne's Vue de la Baie de Marseille
"good": "#C5D294",
"degraded": "#E9CC67",
"bad": "#FFBC88",
})
mdi.Load() // repo path will be inserted at build time
// Display space left on /
storageIcon := pango.Icon("mdi-database")
barista.Add(diskspace.New("/").Output(func(i diskspace.Info) bar.Output {
used := i.UsedPct()
var colorScheme string
if used >= 90 {
colorScheme = "bad"
} else if used >= 50 {
colorScheme = "degraded"
} else {
colorScheme = "good"
}
return outputs.Pango(storageIcon, pango.Textf(" %d%%", used)).Color(colors.Scheme(colorScheme))
}))
// Check connection to the Mullvad VPN
mullvadIsUpRe := regexp.MustCompile(`^You are connected to Mullvad`)
mullvadServerRe := regexp.MustCompile(`\(server (.*)\)`)
mullvadIpRe := regexp.MustCompile(`Your IP address is (.*)`)
client := &http.Client{Timeout: 3 * time.Second}
incognitoIcon := pango.Icon("mdi-incognito")
incognitoOffIcon := pango.Icon("mdi-incognito-off")
barista.Add(funcs.Every(5*time.Second, func(s bar.Sink) {
icon := incognitoOffIcon
message := pango.Text("")
colorScheme := "bad"
res, err := client.Get("https://am.i.mullvad.net/connected")
if !s.Error(err) {
status, err := io.ReadAll(res.Body)
res.Body.Close()
if !s.Error(err) {
var re *regexp.Regexp
if mullvadIsUpRe.Match(status) {
re = mullvadServerRe
colorScheme = "good"
icon = incognitoIcon
} else {
re = mullvadIpRe
colorScheme = "degraded"
}
result := re.FindSubmatch(status)
if len(result) >= 2 {
message = pango.Textf(" %s", result[1])
}
}
}
client.CloseIdleConnections()
s.Output(outputs.Pango(icon, message).Color(colors.Scheme(colorScheme)))
}))
// Display the wifi status
wifiOffIcon := pango.Icon("mdi-wifi-off")
wifiRefreshIcon := pango.Icon("mdi-wifi-refresh")
wifiOnIcon := pango.Icon("mdi-wifi")
barista.Add(wlan.Named("wlp2s0").Output(func(w wlan.Info) bar.Output {
switch {
case w.Connected():
return outputs.Pango(wifiOnIcon, pango.Textf(" %s", w.SSID)).Color(colors.Scheme("good"))
case w.Connecting():
return outputs.Pango(wifiRefreshIcon).Color(colors.Scheme("degraded"))
case w.Enabled():
return outputs.Pango(wifiOffIcon).Color(colors.Scheme("bad"))
default:
return nil
}
}))
// Display the ethernet status
ethernetCableOnIcon := pango.Icon("mdi-ethernet-cable")
ethernetCableOffIcon := pango.Icon("mdi-ethernet-cable-off")
barista.Add(netinfo.Prefix("e").Output(func(s netinfo.State) bar.Output {
switch {
case s.Connected():
ip := "<no ip>"
if len(s.IPs) > 0 {
ip = s.IPs[0].String()
}
return outputs.Pango(ethernetCableOnIcon, pango.Textf(" %s", ip)).Color(colors.Scheme("good"))
case s.Connecting():
return outputs.Pango(ethernetCableOnIcon).Color(colors.Scheme("degraded"))
case s.Enabled():
return outputs.Pango(ethernetCableOffIcon).Color(colors.Scheme("bad"))
default:
return nil
}
}))
// Display the battery status
batteryIcons := [11]*pango.Node{pango.Icon("mdi-battery-outline"),
pango.Icon("mdi-battery-10"),
pango.Icon("mdi-battery-20"),
pango.Icon("mdi-battery-30"),
pango.Icon("mdi-battery-40"),
pango.Icon("mdi-battery-50"),
pango.Icon("mdi-battery-60"),
pango.Icon("mdi-battery-70"),
pango.Icon("mdi-battery-80"),
pango.Icon("mdi-battery-90"),
pango.Icon("mdi-battery")}
batteryChargingIcons := [11]*pango.Node{pango.Icon("mdi-battery-charging-outline"),
pango.Icon("mdi-battery-charging-10"),
pango.Icon("mdi-battery-charging-20"),
pango.Icon("mdi-battery-charging-30"),
pango.Icon("mdi-battery-charging-40"),
pango.Icon("mdi-battery-charging-50"),
pango.Icon("mdi-battery-charging-60"),
pango.Icon("mdi-battery-charging-70"),
pango.Icon("mdi-battery-charging-80"),
pango.Icon("mdi-battery-charging-90"),
pango.Icon("mdi-battery-charging-100")}
barista.Add(battery.All().Output(func(b battery.Info) bar.Output {
switch b.Status {
case battery.Disconnected, battery.Unknown:
return nil
default:
var icons [11]*pango.Node
var colorScheme string
if b.Status == battery.Charging {
icons = batteryChargingIcons
colorScheme = "good"
} else {
icons = batteryIcons
if b.RemainingPct() <= 10 {
colorScheme = "bad"
} else if b.RemainingPct() <= 20 {
colorScheme = "degraded"
} else {
colorScheme = "good"
}
}
icon := icons[b.RemainingPct()/10]
return outputs.Pango(icon, pango.Textf(" %d%%", b.RemainingPct())).Color(colors.Scheme(colorScheme))
}
}))
// Display brightness
brightnessHighIcon := pango.Icon("mdi-lightbulb-on")
brightnessMidIcon := pango.Icon("mdi-lightbulb-on-outline")
brightnessLowIcon := pango.Icon("mdi-lightbulb-outline")
ReadBrightness := func(name string) (int, error) {
valueStr, err := os.ReadFile("/sys/class/backlight/intel_backlight/" + name)
if err != nil {
return 0, err
}
return strconv.Atoi(string(valueStr[:len(valueStr)-1]))
}
brightnessMax, _ := ReadBrightness("max_brightness") // always non-zero, unless there's an error
barista.Add(funcs.Every(time.Second, func(s bar.Sink) {
brightness, err := ReadBrightness("brightness")
if !s.Error(err) {
value := (brightness * 100) / brightnessMax
var icon *pango.Node
if value <= 30 {
icon = brightnessLowIcon
} else if value < 70 {
icon = brightnessMidIcon
} else {
icon = brightnessHighIcon
}
s.Output(outputs.Pango(icon, pango.Textf(" %d%%", value)))
}
}))
// Display output volume
volumeOffIcon := pango.Icon("mdi-volume-variant-off")
volumeLowIcon := pango.Icon("mdi-volume-low")
volumeMidIcon := pango.Icon("mdi-volume-medium")
volumeHighIcon := pango.Icon("mdi-volume-high")
barista.Add(volume.New(pulseaudio.DefaultSink()).Output(func(v volume.Volume) bar.Output {
volume := v.Pct()
var icon *pango.Node
if volume == 0 || v.Mute {
icon = volumeOffIcon
} else if volume <= 30 {
icon = volumeLowIcon
} else if volume <= 70 {
icon = volumeMidIcon
} else {
icon = volumeHighIcon
}
return outputs.Pango(icon, pango.Textf(" %d%%", volume))
}))
// Display microphone volume
microphoneOffIcon := pango.Icon("mdi-microphone-off")
microphoneIcon := pango.Icon("mdi-microphone")
barista.Add(volume.New(pulseaudio.DefaultSource()).Output(func(v volume.Volume) bar.Output {
volume := v.Pct() / 10 // TODO: correct Pct implementation for sources
var icon *pango.Node
if volume == 0 || v.Mute {
icon = microphoneOffIcon
} else {
icon = microphoneIcon
}
return outputs.Pango(icon, pango.Textf(" %d%%", volume))
}))
barista.Add(clock.Local().OutputFormat("2006-01-02 15:04:05"))
panic(barista.Run())
}
|