hbspbar

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

00-status.go (2274B)


      1 package status // import "hhvn.uk/hbspbar/status"
      2 
      3 import (
      4 	"fmt"
      5 	"time"
      6 	"image"
      7 	"image/color"
      8 
      9 	"hhvn.uk/hbspbar/drw"
     10 	"hhvn.uk/hbspbar/config"
     11 	"hhvn.uk/hbspbar/common"
     12 )
     13 
     14 var NewBlocks = make(chan []*Block)
     15 var updates   = make(chan *Block)
     16 var blockid   = make(map[string]int)
     17 var blockids  = 0
     18 
     19 type Block struct {
     20 	Name string
     21 	I    *image.RGBA
     22 	W    int
     23 }
     24 
     25 func init() {
     26 	blocks := make(map[int]*Block)
     27 
     28 	go func() {
     29 		for s := range updates {
     30 			id := blockid[s.Name]
     31 
     32 			blocks[id] = s
     33 
     34 			b := make([]*Block, blockids)
     35 			for i := 0; i < blockids; i++ {
     36 				b[i] = blocks[i]
     37 			}
     38 
     39 			NewBlocks <- b
     40 		}
     41 	}()
     42 }
     43 
     44 func register(name string, fn func(*Block) error, seconds int) {
     45 	nid := 0
     46 	for _, id := range blockid {
     47 		if id >= nid {
     48 			nid = id + 1
     49 			blockids++
     50 		}
     51 	}
     52 	blockid[name] = nid
     53 
     54 	go func(){
     55 		for {
     56 			u   := newBlock(name)
     57 			err := fn(u)
     58 
     59 			if err != nil {
     60 				common.Error("block \"%s\": %s\n", name, err)
     61 
     62 				u.drawText(0, config.Red, fmt.Sprintf("[err: %s]", name))
     63 				updates <- u
     64 				sleep(5)
     65 			} else {
     66 				updates <- u
     67 				sleep(seconds)
     68 			}
     69 		}
     70 	}()
     71 }
     72 
     73 func newBlock(name string) (*Block) {
     74 	var s Block
     75 
     76 	s.Name = name
     77 	s.W    = 0
     78 	s.I    = image.NewRGBA(image.Rect(0, 0, 1000, int(config.H)))
     79 
     80 	// Use drw's in order not to modify s.W
     81 	drw.DrawRect(s.I, 0, 0, 1000, int(config.H), config.Status, true)
     82 
     83 	return &s
     84 }
     85 
     86 func sleep(s int) {
     87 	time.Sleep(time.Duration(s) * time.Second)
     88 }
     89 
     90 func blendGYR(percent int) color.Color {
     91 	return drw.Blend(percent, config.Green, config.Yellow, config.Red)
     92 }
     93 
     94 func blendBg(c color.Color) color.Color {
     95 	return drw.Blend(75, c, config.Status)
     96 }
     97 
     98 func (s *Block) furthest(x int) {
     99 	if x > s.W {
    100 		s.W = x
    101 	}
    102 }
    103 
    104 func (s *Block) drawText(x int, col color.Color, text string) (int, error) {
    105 	w, err := drw.DrawText(s.I, x, col, text)
    106 	s.furthest(x + w)
    107 	return w, err
    108 }
    109 
    110 func (s *Block) drawRect(x, y, w, h int, c color.Color, fill bool) {
    111 	drw.DrawRect(s.I, x, y, w, h, c, fill)
    112 	s.furthest(x + w)
    113 }
    114 
    115 func (s *Block) drawPercentBar(x, percent int) int {
    116 	tbpad := 3
    117 	th    := int(config.H) - tbpad * 2 + 1
    118 	h     := int(th * percent / 100)
    119 	fg    := blendGYR(percent)
    120 	bg    := blendBg(fg)
    121 
    122 	s.drawRect(x, tbpad - 1, 3, th, bg, true)
    123 	s.drawRect(x, tbpad + th - h - 1, 3, h, fg, true)
    124 
    125 	return 5
    126 }