commit d5ffccc1b4499e01ebb8d727c4651856894e154f
parent 3776ecd92be2886b2b9d41c2b78cc2adf46e14e6
Author: hhvn <dev@hhvn.uk>
Date: Sun, 19 Nov 2023 14:51:31 +0000
Memory status
Diffstat:
A | status/03-mem.go | | | 63 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 63 insertions(+), 0 deletions(-)
diff --git a/status/03-mem.go b/status/03-mem.go
@@ -0,0 +1,63 @@
+package status // import "hhvn.uk/hbspbar/status"
+
+import (
+ "fmt"
+ "strings"
+
+ "hhvn.uk/hbspbar/common"
+)
+
+func checkMemField(m map[string]int, k string) error {
+ _, ok := m[k]
+ if !ok {
+ return fmt.Errorf("missing %s", k)
+ }
+ return nil
+}
+
+func mem(name string) error {
+ u := newUpdate(name)
+
+ mem := make(map[string]int)
+
+ lines, err := common.FileAsLines("/proc/meminfo")
+ if err != nil { return err }
+
+ for _, l := range lines {
+ name, rest, _ := strings.Cut(l, ":")
+ val := strings.TrimLeft(strings.TrimSuffix(rest, " kB"), " ")
+
+ n, err := common.Intify(val)
+ if err != nil { return err }
+
+ mem[name] = n
+ }
+
+ if err := checkMemField(mem, "MemTotal"); err != nil { return err }
+ if err := checkMemField(mem, "MemAvailable"); err != nil { return err }
+
+ total := mem["MemTotal"]
+ avail := mem["MemAvailable"]
+ p := int(float64(avail) / float64(total) * 100.0)
+ cx := 0
+
+ cx += u.drawPercentBar(cx, p)
+ _, err = u.drawText(cx, blendGYR(p), fmt.Sprintf("%d%%", p))
+
+ updates <- u
+
+ sleep(1)
+
+ return nil
+}
+
+func init() {
+ name := "mem"
+ register(name)
+
+ go func() {
+ for {
+ checkError(name, mem(name))
+ }
+ }()
+}