From 0b3d4426cb585ceade7a27ed28cfe08f1710738d Mon Sep 17 00:00:00 2001 From: hhvn Date: Fri, 2 Feb 2024 20:40:07 +0000 Subject: Pass slice of Blocks via NewBlocks --- status/00-status.go | 62 ++++++++++++++++++++--------------------------------- 1 file changed, 23 insertions(+), 39 deletions(-) (limited to 'status') diff --git a/status/00-status.go b/status/00-status.go index 109c49c..dadc668 100644 --- a/status/00-status.go +++ b/status/00-status.go @@ -3,7 +3,6 @@ package status // import "hhvn.uk/hbspbar/status" import ( "fmt" "time" - "sync" "image" "image/color" @@ -12,46 +11,30 @@ import ( "hhvn.uk/hbspbar/common" ) -var NewBlocks = make(chan *Blocks) -var updates = make(chan *status) -var statusid = make(map[string]int) +var NewBlocks = make(chan []*Block) +var updates = make(chan *Block) +var blockid = make(map[string]int) +var blockids = 0 -type Blocks struct { - l *sync.RWMutex - m map[int]*status -} - -func (b *Blocks) Len() int { - b.l.RLock() - defer b.l.RUnlock() - return len(b.m) -} - -func (b *Blocks) Get(i int) (s *status, ok bool) { - b.l.RLock() - defer b.l.RUnlock() - s, ok = b.m[i] - return -} - -type status struct { +type Block struct { Name string I *image.RGBA W int } func init() { - var mutex sync.RWMutex - status := make(map[int]*status) - b := &Blocks{&mutex, status} + blocks := make(map[int]*Block) go func() { for s := range updates { - id := statusid[s.Name] + id := blockid[s.Name] - mutex.Lock() - status[id] = s - mutex.Unlock() + blocks[id] = s + + b := make([]*Block, blockids) + for i := 0; i < blockids; i++ { + b[i] = blocks[i] + } NewBlocks <- b } @@ -60,19 +43,20 @@ func init() { func register(name string, fn func(string) error) { nid := 0 - for _, id := range statusid { + for _, id := range blockid { if id >= nid { nid = id + 1 + blockids++ } } - statusid[name] = nid + blockid[name] = nid go func(){ for { err := fn(name) if err != nil { - common.Error("status \"%s\": %s\n", name, err) + common.Error("block \"%s\": %s\n", name, err) u := newUpdate(name) u.drawText(0, config.Red, fmt.Sprintf("[err: %s]", name)) @@ -84,8 +68,8 @@ func register(name string, fn func(string) error) { }() } -func newUpdate(name string) (*status) { - var s status +func newUpdate(name string) (*Block) { + var s Block s.Name = name s.W = 0 @@ -109,24 +93,24 @@ func blendBg(c color.Color) color.Color { return drw.Blend(75, c, config.Status) } -func (s *status) furthest(x int) { +func (s *Block) furthest(x int) { if x > s.W { s.W = x } } -func (s *status) drawText(x int, col color.Color, text string) (int, error) { +func (s *Block) drawText(x int, col color.Color, text string) (int, error) { w, err := drw.DrawText(s.I, x, col, text) s.furthest(x + w) return w, err } -func (s *status) drawRect(x, y, w, h int, c color.Color, fill bool) { +func (s *Block) drawRect(x, y, w, h int, c color.Color, fill bool) { drw.DrawRect(s.I, x, y, w, h, c, fill) s.furthest(x + w) } -func (s *status) drawPercentBar(x, percent int) int { +func (s *Block) drawPercentBar(x, percent int) int { tbpad := 3 th := int(config.H) - tbpad * 2 + 1 h := int(th * percent / 100) -- cgit v1.2.3