hbspbar

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

commit 4722a069233b35efd0b600c973e7a7e9bca01d16
parent 1c46de9342a93cf8284d2bd4ed58f524cda60b29
Author: hhvn <dev@hhvn.uk>
Date:   Sun, 19 Nov 2023 13:49:28 +0000

Do int2rgb conversions once. In config.go.

Diffstat:
Mbar/bar.go | 7++++---
Mconfig/config.go | 55+++++++++++++++++++++++++++++++++++++++++++++++--------
Mdrw/drw.go | 21++++++++++-----------
Mdrw/x.go | 29+----------------------------
Mstatus/00-status.go | 18++++++------------
Mstatus/01-bat.go | 11++++++-----
6 files changed, 74 insertions(+), 67 deletions(-)

diff --git a/bar/bar.go b/bar/bar.go @@ -5,6 +5,7 @@ import ( "sync" "image" "errors" + "image/color" "hhvn.uk/hbspbar/config" "hhvn.uk/hbspbar/common" @@ -42,16 +43,16 @@ func (b *bar) rect() image.Rectangle { return image.Rect(0, 0, int(b.Mon.Rectangle.Width), int(config.H)) } -func (b *bar) drawText(x int, col uint32, text string) (int, error) { +func (b *bar) drawText(x int, col color.Color, text string) (int, error) { return drw.DrawText(b.i, x, col, text) } -func (b *bar) drawRect(x, y, w, h int, c uint32, fill bool) { +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() { - var bg uint32 + var bg color.Color var filled bool b.drawing.Lock() diff --git a/config/config.go b/config/config.go @@ -1,16 +1,55 @@ package config // import "hhvn.uk/hbspbar/config" +import "image/color" + // Bar height var H uint = 17 -// Colours -var Bg uint32 = 0xff050a10 -var UnselMon uint32 = 0xff0c1726 -var Status uint32 = 0xff0a2126 -var Fg uint32 = 0xffeeeeee -var FgDark uint32 = 0xff444444 -var Sel uint32 = 0xff1b364b -var Urg uint32 = 0xff90222b +const ( + // Bar colours + xBg = 0xff050a10 + xUnselMon = 0xff0c1726 + xStatus = 0xff0a2126 + xFg = 0xffeeeeee + xFgDark = 0xff444444 + xSel = 0xff1b364b + xUrg = 0xff90222b + + // Status colours + xRed = 0xffaa2222 + xOrange = 0xffaa7700 + xGreen = 0xff00aa00 + xBlack = 0xff000000 + xGrey = 0xff888888 + xYellow = 0xffaaaa00 + xWhite = 0xffcccccc +) + +func int2rgb(argb uint32) (color.Color) { + return color.RGBA{ + B: uint8( argb & 0x000000ff), + G: uint8((argb & 0x0000ff00) >> 8), + R: uint8((argb & 0x00ff0000) >> 16), + A: uint8((argb & 0xff000000) >> 24) } +} + +var ( + Bg = int2rgb(xBg) + UnselMon = int2rgb(xUnselMon) + Status = int2rgb(xStatus) + Fg = int2rgb(xFg) + FgDark = int2rgb(xFgDark) + Sel = int2rgb(xSel) + Urg = int2rgb(xUrg) + + Red = int2rgb(xRed) + Orange = int2rgb(xOrange) + Green = int2rgb(xGreen) + Black = int2rgb(xBlack) + Grey = int2rgb(xGrey) + Yellow = int2rgb(xYellow) + White = int2rgb(xWhite) +) var Font string = "/usr/share/fonts/TTF/DejaVuSansMono.ttf" var FontSize float64 = 11.0 diff --git a/drw/drw.go b/drw/drw.go @@ -3,6 +3,7 @@ package drw // import "hhvn.uk/hbspbar/drw" import ( "image" "image/draw" + "image/color" "hhvn.uk/hbspbar/config" @@ -11,7 +12,7 @@ import ( "github.com/BurntSushi/freetype-go/freetype" ) -func DrawText(i *image.RGBA, x int, col uint32, text string) (int, error) { +func DrawText(i *image.RGBA, x int, col color.Color, text string) (int, error) { ft := freetype.NewContext() ft.SetDPI(72) ft.SetFont(font) @@ -19,7 +20,7 @@ func DrawText(i *image.RGBA, x int, col uint32, text string) (int, error) { ft.SetClip(i.Bounds()) ft.SetDst(i) - src := image.NewUniform(int2rgb(col)) + src := image.NewUniform(col) ft.SetSrc(src) pt := freetype.Pt(x, config.FontYPad+ @@ -34,16 +35,14 @@ func TextWidth(text string) (int) { return w } -func DrawRect(i *image.RGBA, x, y, w, h int, c uint32, fill bool) { - col := int2rgb(c) - +func DrawRect(i *image.RGBA, x, y, w, h int, c color.Color, fill bool) { var ix, iy int for ix = x; ix < x + w; ix++ { for iy = y; iy < y + h; iy++ { if fill || ix == x || ix == x + w - 1 || iy == y || iy == y + h - 1 { - i.Set(ix, iy, col) + i.Set(ix, iy, c) } } } @@ -54,7 +53,7 @@ func AddImg(dst *image.RGBA, x, w int, src *image.RGBA) { draw.Draw(dst, r, src, image.Pt(0,0), draw.Src) } -func Blend3(a, b, c uint32, percent int) uint32 { +func Blend3(a, b, c color.Color, percent int) color.Color { if percent <= 50 { return Blend(a, b, percent * 2) } else { @@ -63,10 +62,10 @@ func Blend3(a, b, c uint32, percent int) uint32 { } // Conversion hell, I know -func Blend(a, b uint32, percent int) uint32 { - ac, _ := col.MakeColor(int2rgb(a)) - bc, _ := col.MakeColor(int2rgb(b)) +func Blend(a, b color.Color, percent int) color.Color { + ac, _ := col.MakeColor(a) + bc, _ := col.MakeColor(b) - return rgb2int(ac.BlendHcl(bc, float64(percent) / 100).Clamped()) + return color.Color(ac.BlendHcl(bc, float64(percent) / 100).Clamped()) } diff --git a/drw/x.go b/drw/x.go @@ -3,7 +3,6 @@ package drw // import "hhvn.uk/hbspbar/drw" import ( "os" "image" - "image/color" "hhvn.uk/hbspbar/config" "hhvn.uk/hbspbar/common" @@ -69,32 +68,6 @@ func init() { }}() } -func int2rgb(argb uint32) (color.RGBA) { - return color.RGBA{ - B: uint8( argb & 0x000000ff), - G: uint8((argb & 0x0000ff00) >> 8), - R: uint8((argb & 0x00ff0000) >> 16), - A: uint8((argb & 0xff000000) >> 24) } -} - -func rgb2int(c color.Color) uint32 { - r, g, b, a := c.RGBA() - - r /= 256 - g /= 256 - b /= 256 - a /= 256 - - var ret uint32 = 0 - - ret |= (a << 24) - ret |= (r << 16) - ret |= (g << 8) - ret |= b - - return ret -} - type Window struct { id xproto.Window } @@ -112,7 +85,7 @@ func WindowCreate(x, y, w, h uint) (*Window, error) { int16(x), int16(y), uint16(w), uint16(h), 0, xproto.WindowClassInputOutput, screen.RootVisual, xproto.CwBackPixel|xproto.CwEventMask, []uint32{ - config.Bg, + 0x00000000, xproto.EventMaskStructureNotify }) class := icccm.WmClass{"hbspbar", "hbspbar"} diff --git a/status/00-status.go b/status/00-status.go @@ -5,6 +5,7 @@ import ( "time" "sync" "image" + "image/color" "hhvn.uk/hbspbar/drw" "hhvn.uk/hbspbar/config" @@ -18,13 +19,6 @@ var Updated chan bool var updates chan *status const ( - Red = 0xffaa2222 - Orange = 0xffaa7700 - Green = 0xff00aa00 - Black = 0xff000000 - Grey = 0xff888888 - Yellow = 0xffaaaa00 - White = 0xffcccccc ) func init() { @@ -83,7 +77,7 @@ func checkError(name string, err error) { common.Error("status \"%s\": %s\n", name, err) u := newUpdate(name) - u.drawText(0, Red, fmt.Sprintf("[err: %s]", name)) + u.drawText(0, config.Red, fmt.Sprintf("[err: %s]", name)) updates <- u sleep(5) @@ -93,8 +87,8 @@ func sleep(s int) { time.Sleep(time.Duration(s) * time.Second) } -func blendGYR(percent int) uint32 { - return drw.Blend3(Green, Yellow, Red, percent) +func blendGYR(percent int) color.Color { + return drw.Blend3(config.Green, config.Yellow, config.Red, percent) } func (s *status) furthest(x int) { @@ -103,13 +97,13 @@ func (s *status) furthest(x int) { } } -func (s *status) drawText(x int, col uint32, text string) (int, error) { +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 uint32, fill bool) { +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) } diff --git a/status/01-bat.go b/status/01-bat.go @@ -5,6 +5,7 @@ import ( "path" "errors" "strings" + "image/color" // "hhvn.uk/hbspbar/drw" "hhvn.uk/hbspbar/config" @@ -58,13 +59,13 @@ func bat(name string) error { avgcap := (usedcap * 100) / totalcap - var c uint32 + var c color.Color // Colour of outline = charge indicator switch stati { case nothing: c = config.Fg - case charging: c = Green - case discharging: c = Red + case charging: c = config.Green + case discharging: c = config.Red } var iw int = 20 @@ -78,8 +79,8 @@ func bat(name string) error { u.drawRect(4, 3, iw + 2, ih + 2, c, false) switch { - case avgcap < 25: c = Red - case avgcap >= 99: c = Green + case avgcap < 25: c = config.Red + case avgcap >= 99: c = config.Green default: c = config.FgDark }