hbspbar

bspwm status bar
git clone https://hhvn.uk/hbspbar
git clone git://hhvn.uk/hbspbar
Log | Files | Refs

03-mem.go (1030B)


      1 package status // import "hhvn.uk/hbspbar/status"
      2 
      3 import (
      4 	"fmt"
      5 	"strings"
      6 
      7 	"hhvn.uk/hbspbar/common"
      8 )
      9 
     10 func checkMemField(m map[string]int, k string) error {
     11 	_, ok := m[k]
     12 	if !ok {
     13 		return fmt.Errorf("missing %s", k)
     14 	}
     15 	return nil
     16 }
     17 
     18 func mem(u *Block) error {
     19 	mem := make(map[string]int)
     20 
     21 	lines, err := common.FileAsLines("/proc/meminfo")
     22 	if err != nil { return err }
     23 
     24 	for _, l := range lines {
     25 		name, rest, _ := strings.Cut(l, ":")
     26 		val := strings.TrimLeft(strings.TrimSuffix(rest, " kB"), " ")
     27 
     28 		n, err := common.Intify(val)
     29 		if err != nil { return err }
     30 
     31 		mem[name] = n
     32 	}
     33 
     34 	if err := checkMemField(mem, "MemTotal"); err != nil { return err }
     35 	if err := checkMemField(mem, "MemAvailable"); err != nil { return err }
     36 
     37 	total  := mem["MemTotal"]
     38 	used   := total - mem["MemAvailable"]
     39 	p      := int(float64(used) / float64(total) * 100.0)
     40 	cx     := 0
     41 	
     42 	cx     += u.drawPercentBar(cx, p)
     43 	_, err  = u.drawText(cx, blendGYR(p), fmt.Sprintf("%d%%", p))
     44 
     45 	return nil
     46 }
     47 
     48 func init() {
     49 	register("mem", mem, 1)
     50 }