package status // import "hhvn.uk/hbspbar/status" import ( "fmt" "time" "image" "image/color" "hhvn.uk/hbspbar/drw" "hhvn.uk/hbspbar/config" "hhvn.uk/hbspbar/common" ) var NewBlocks = make(chan *Blocks) var updates = make(chan *status) var statusid = make(map[string]int) type Blocks map[int]*status func (b *Blocks) Len() int { return len(map[int]*status(*b)) } func (b *Blocks) Get(i int) (s *status, ok bool) { s, ok = (map[int]*status(*b))[i] return } type status struct { Name string I *image.RGBA W int } func init() { status := make(map[int]*status) go func() { for s := range updates { id := statusid[s.Name] status[id] = s b := Blocks(status) NewBlocks <- &b } }() } func register(name string, fn func(string) error) { nid := 0 for _, id := range statusid { if id >= nid { nid = id + 1 } } statusid[name] = nid go func(){ for { err := fn(name) if err != nil { common.Error("status \"%s\": %s\n", name, err) u := newUpdate(name) u.drawText(0, config.Red, fmt.Sprintf("[err: %s]", name)) updates <- u sleep(5) } } }() } func newUpdate(name string) (*status) { var s status s.Name = name s.W = 0 s.I = image.NewRGBA(image.Rect(0, 0, 1000, int(config.H))) // Use drw's in order not to modify s.W drw.DrawRect(s.I, 0, 0, 1000, int(config.H), config.Status, true) return &s } func sleep(s int) { time.Sleep(time.Duration(s) * time.Second) } func blendGYR(percent int) color.Color { return drw.Blend(percent, config.Green, config.Yellow, config.Red) } func blendBg(c color.Color) color.Color { return drw.Blend(75, c, config.Status) } func (s *status) furthest(x int) { if x > s.W { s.W = x } } func (s *status) 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) { drw.DrawRect(s.I, x, y, w, h, c, fill) s.furthest(x + w) } func (s *status) drawPercentBar(x, percent int) int { tbpad := 3 th := int(config.H) - tbpad * 2 + 1 h := int(th * percent / 100) fg := blendGYR(percent) bg := blendBg(fg) s.drawRect(x, tbpad - 1, 3, th, bg, true) s.drawRect(x, tbpad + th - h - 1, 3, h, fg, true) return 5 }