commit 612e4fbeb14001d5eef0bb0e3e19ea65eb47cef2
parent 75a34eb4b035963970c8415316c2b2bd6a81eea6
Author: hhvn <dev@hhvn.uk>
Date: Sun, 14 Apr 2024 13:49:18 +0100
Tidy up the status block api a bit
Handle (nearly) all the plumbing (sleeps, goroutines and comms) in register()
Diffstat:
4 files changed, 14 insertions(+), 25 deletions(-)
diff --git a/status/00-status.go b/status/00-status.go
@@ -41,7 +41,7 @@ func init() {
}()
}
-func register(name string, fn func(string) error) {
+func register(name string, fn func(*Block) error, seconds int) {
nid := 0
for _, id := range blockid {
if id >= nid {
@@ -53,22 +53,24 @@ func register(name string, fn func(string) error) {
go func(){
for {
- err := fn(name)
+ u := newBlock(name)
+ err := fn(u)
if err != nil {
common.Error("block \"%s\": %s\n", name, err)
- u := newUpdate(name)
u.drawText(0, config.Red, fmt.Sprintf("[err: %s]", name))
updates <- u
-
sleep(5)
+ } else {
+ updates <- u
+ sleep(seconds)
}
}
}()
}
-func newUpdate(name string) (*Block) {
+func newBlock(name string) (*Block) {
var s Block
s.Name = name
diff --git a/status/01-bat.go b/status/01-bat.go
@@ -12,7 +12,7 @@ import (
"hhvn.uk/hbspbar/common"
)
-func bat(name string) error {
+func bat(u *Block) error {
dir := "/sys/class/power_supply"
const (
@@ -21,8 +21,6 @@ func bat(name string) error {
discharging
)
- u := newUpdate(name)
-
dirs, err := os.ReadDir(dir)
if dirs == nil { return err }
@@ -87,12 +85,9 @@ func bat(name string) error {
// Fill
u.drawRect(4 + iw - w, 4, w + 1, ih, c, true)
- updates <- u
- sleep(1)
-
return nil
}
func init() {
- register("bat", bat)
+ register("bat", bat, 1)
}
diff --git a/status/02-cpu.go b/status/02-cpu.go
@@ -42,9 +42,7 @@ func getcpus(c chan [][]string, e chan error) {
}
}
-func cpu(name string) error {
- u := newUpdate(name)
-
+func cpu(u *Block) error {
cpus := [2]chan [][]string{
make(chan [][]string),
make(chan [][]string) }
@@ -127,10 +125,10 @@ func cpu(name string) error {
u.drawText(cx, blendGYR(int(cp)), fmt.Sprintf("%d°C", temps / ncores))
- updates <- u
return nil
}
func init() {
- register("cpu", cpu)
+ // 0 second interval, as there is already a 1 second sleep in cpu()
+ register("cpu", cpu, 0)
}
diff --git a/status/03-mem.go b/status/03-mem.go
@@ -15,9 +15,7 @@ func checkMemField(m map[string]int, k string) error {
return nil
}
-func mem(name string) error {
- u := newUpdate(name)
-
+func mem(u *Block) error {
mem := make(map[string]int)
lines, err := common.FileAsLines("/proc/meminfo")
@@ -44,13 +42,9 @@ func mem(name string) error {
cx += u.drawPercentBar(cx, p)
_, err = u.drawText(cx, blendGYR(p), fmt.Sprintf("%d%%", p))
- updates <- u
-
- sleep(1)
-
return nil
}
func init() {
- register("mem", mem)
+ register("mem", mem, 1)
}