hbspbar

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

commit 0b3d4426cb585ceade7a27ed28cfe08f1710738d
parent 321045987904f487d533a6666265c506457e9b40
Author: hhvn <dev@hhvn.uk>
Date:   Fri,  2 Feb 2024 20:40:07 +0000

Pass slice of Blocks via NewBlocks

Diffstat:
Mbar/bar.go | 29++++++++++++++---------------
Mstatus/00-status.go | 62+++++++++++++++++++++++---------------------------------------
2 files changed, 37 insertions(+), 54 deletions(-)

diff --git a/bar/bar.go b/bar/bar.go @@ -76,10 +76,14 @@ func (b *bar) drawRect(x, y, w, h int, c color.Color, fill bool) { drw.DrawRect(b.i, x, y, w, h, c, fill) } -func (b bar) draw(state *bspc.State, blocks *status.Blocks) { +func (b bar) draw(state *bspc.State, blocks []*status.Block) { var bg color.Color var filled bool + if blocks == nil { + return + } + mon, err := b.getmon(state) if err != nil { return @@ -137,29 +141,24 @@ func (b bar) draw(state *bspc.State, blocks *status.Blocks) { cx += ax + 2 } - cx = int(mon.Rectangle.Width) - 6 - for i := blocks.Len() - 1; i >= 0; i -= 1 { - s, ok := blocks.Get(i) - if ok { + cx = int(mon.Rectangle.Width) + for _, s := range blocks { + if s != nil { cx -= s.W cx -= config.StatusPad } } - cx += config.StatusPad - cx -= 6 b.drawRect(cx, 0, int(mon.Rectangle.Width) - cx, int(config.H), config.Status, true) - cx = int(mon.Rectangle.Width) - 6 - for i := blocks.Len() - 1; i >= 0; i -= 1 { - s, ok := blocks.Get(i) - if ok { - cx -= s.W + cx += config.StatusPad / 2 + for _, s := range blocks { + if s != nil { drw.AddImg(b.i, cx, s.W, s.I) - cx -= config.StatusPad + cx += s.W + config.StatusPad } } @@ -176,8 +175,8 @@ var Err = make(chan error) var finalstate *bspc.State = nil func init() { - var state *bspc.State = nil - var blocks *status.Blocks = nil + var state *bspc.State = nil + var blocks []*status.Block = nil go func() { defer func() { diff --git 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)