commit 128fb5d5ad24e2ad24b78d25c41d071ddfa546fe
parent 851758d4a04c4cb05f802ea3127841e94456ee44
Author: hhvn <dev@hhvn.uk>
Date: Wed, 15 Nov 2023 19:54:06 +0000
Use map instead of list for bars
Diffstat:
M | bar/bar.go | | | 56 | ++++++++++++++------------------------------------------ |
1 file changed, 14 insertions(+), 42 deletions(-)
diff --git a/bar/bar.go b/bar/bar.go
@@ -4,7 +4,6 @@ import (
"fmt"
"sync"
"errors"
- "container/list"
"hhvn.uk/hbspbar/common"
"hhvn.uk/hbspbar/bspc"
@@ -43,7 +42,7 @@ var conf = struct {
"/usr/share/fonts/TTF/DejaVuSansMono.ttf",
11.0, 1 }
-var bars *list.List
+var bars map[int]*bar
var font *truetype.Font
var stoploop chan bool
@@ -186,8 +185,6 @@ var Handle handle
func init() {
InitErr = nil
- bars = list.New()
-
x, err := xgb.NewConn()
if err != nil {
InitErr = err
@@ -262,23 +259,22 @@ func init() {
// default:
// }
case id := <- Handle.Destroy:
- destroy(id)
+ bars[id].destroy()
+ delete(bars, id)
case id := <- Handle.Create:
if state == nil {
Handle.Err <- errors.New("attempted to create a bar with uninitialized state")
return
}
- if getbar(id) != nil { break }
+ if _, ok := bars[id]; ok { break }
if err := create(x, xu, screen, state.GetMon(id)); err != nil {
Handle.Err <- fmt.Errorf("Couldn't create window: %s\n", err)
return
}
case state = <- Handle.NewState:
- fmt.Println(state)
- for e := bars.Front(); e != nil; e = e.Next() {
- v := e.Value.(*bar)
- v.Mon = state.GetMon(v.Mon.ID)
- go v.draw()
+ for _, b := range bars {
+ b.Mon = state.GetMon(b.Mon.ID)
+ go b.draw()
}
case <- stoploop:
return
@@ -287,16 +283,6 @@ func init() {
}()
}
-func getbar(monid int) (*bar) {
- for e := bars.Front(); e != nil; e = e.Next() {
- v := e.Value.(*bar)
-
- if v.Mon.ID == monid { return v }
- }
-
- return nil
-}
-
func create(x *xgb.Conn, xu *xgbutil.XUtil,
scr *xproto.ScreenInfo, m *bspc.Monitor) (error) {
var b bar
@@ -313,31 +299,17 @@ func create(x *xgb.Conn, xu *xgbutil.XUtil,
go b.draw()
- bars.PushBack(&b)
- return nil
-}
-
-func destroy(monid int) (bool) {
- var next *list.Element
-
- for e := bars.Front(); e != nil; e = next {
- next = e.Next()
- v := e.Value.(*bar)
-
- /* A monid of -1 means that we're doing
- the bar.Cleanup schermoodle today */
- if monid == -1 || monid > 0 && v.Mon.ID == monid {
- v.destroy()
- bars.Remove(e)
- if monid != -1 { return true }
- }
+ if bars == nil {
+ bars = make(map[int]*bar)
}
- if monid == -1 { return true }
- return false
+ bars[b.Mon.ID] = &b
+ return nil
}
func Cleanup() {
- destroy(-1)
+ for _, b := range bars {
+ b.destroy()
+ }
stoploop <- true
}