From 196d9aae34222c69556092b105795ceb96ecde61 Mon Sep 17 00:00:00 2001 From: gizak Date: Mon, 21 Sep 2015 03:11:58 -0400 Subject: [PATCH] Theme map lookup --- block.go | 14 ++++++------- events.go | 3 +-- events_test.go | 18 ++++++++--------- render.go | 2 +- test/runtest.go | 1 - theme.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ theme_test.go | 31 ++++++++++++++++++++++++++++ 7 files changed, 103 insertions(+), 20 deletions(-) create mode 100644 theme_test.go diff --git a/block.go b/block.go index 85e928d..0d3b88e 100644 --- a/block.go +++ b/block.go @@ -125,22 +125,22 @@ type Block struct { func NewBlock() *Block { b := Block{} b.Display = true - b.Border = theme.HasBorder + b.Border = true b.BorderLeft = true b.BorderRight = true b.BorderTop = true b.BorderBottom = true - b.BorderBg = theme.BorderBg - b.BorderFg = theme.BorderFg - b.BorderLabelBg = theme.BorderLabelTextBg - b.BorderLabelFg = theme.BorderLabelTextFg - b.Bg = theme.BlockBg + b.BorderBg = ThemeAttr("border.bg") + b.BorderFg = ThemeAttr("border.fg") + b.BorderLabelBg = ThemeAttr("label.bg") + b.BorderLabelFg = ThemeAttr("label.fg") + b.Bg = ThemeAttr("block.bg") b.Width = 2 b.Height = 2 return &b } -// Align computes box mob.l +// Align computes box model func (b *Block) Align() { b.area.Min.X = b.X b.area.Min.Y = b.Y diff --git a/events.go b/events.go index f0f110c..188d75f 100644 --- a/events.go +++ b/events.go @@ -233,8 +233,7 @@ func (es *EvtStream) Loop() { es.RLock() defer es.RUnlock() if pattern := es.match(a.Path); pattern != "" { - h := es.Handlers[pattern] - h(a) + es.Handlers[pattern](a) } }(e) } diff --git a/events_test.go b/events_test.go index 9fc4a0e..c85634a 100644 --- a/events_test.go +++ b/events_test.go @@ -22,18 +22,18 @@ var ps = []string{ "/a/b/c/d/"} func TestMatchScore(t *testing.T) { - chk := func(a, b string, s int) { - if c := MatchScore(a, b); c != s { - t.Errorf("\na:%s\nb:%s\nshould:%d\nscore:%d", a, b, s, c) + chk := func(a, b string, s bool) { + if c := isPathMatch(a, b); c != s { + t.Errorf("\na:%s\nb:%s\nshould:%t\nactual:%t", a, b, s, c) } } - chk(ps[1], ps[1], 0) - chk(ps[1], ps[2], -1) - chk(ps[2], ps[1], 0) - chk(ps[4], ps[1], 0) - chk(ps[6], ps[2], 1) - chk(ps[4], ps[5], -1) + chk(ps[1], ps[1], true) + chk(ps[1], ps[2], true) + chk(ps[2], ps[1], false) + chk(ps[4], ps[1], false) + chk(ps[6], ps[2], false) + chk(ps[4], ps[5], false) } func TestCrtEvt(t *testing.T) { diff --git a/render.go b/render.go index 1496ef1..1912652 100644 --- a/render.go +++ b/render.go @@ -63,7 +63,7 @@ func TermHeight() int { // right could overlap on left ones. func Render(bs ...Bufferer) { // set tm bg - tm.Clear(tm.ColorDefault, toTmAttr(theme.BodyBg)) + tm.Clear(tm.ColorDefault, toTmAttr(ThemeAttr("bg"))) for _, b := range bs { buf := b.Buffer() // set cels in buf diff --git a/test/runtest.go b/test/runtest.go index 6e65305..4211c6a 100644 --- a/test/runtest.go +++ b/test/runtest.go @@ -40,7 +40,6 @@ func main() { }) termui.Handle("/timer/1s", func(e termui.Event) { - //debug.Logf("<-%v\n", e) t := e.Data.(termui.EvtTimer) if t.Count%2 == 0 { diff --git a/theme.go b/theme.go index c8ad947..d9b5a34 100644 --- a/theme.go +++ b/theme.go @@ -4,6 +4,9 @@ package termui +import "strings" + +/* // A ColorScheme represents the current look-and-feel of the dashboard. type ColorScheme struct { BodyBg Attribute @@ -82,3 +85,54 @@ func UseTheme(th string) { theme = themeDefault } } +*/ + +var ColorMap = map[string]Attribute{ + "fg": ColorWhite, + "bg": ColorDefault, + "border.fg": ColorWhite, + "label.fg": ColorGreen, + "par.fg": ColorYellow, + "par.label.bg": ColorWhite, +} + +func ThemeAttr(name string) Attribute { + return lookUpAttr(ColorMap, name) +} + +func lookUpAttr(clrmap map[string]Attribute, name string) Attribute { + + a, ok := clrmap[name] + if ok { + return a + } + + ns := strings.Split(name, ".") + for i := range ns { + nn := strings.Join(ns[i:len(ns)], ".") + a, ok = ColorMap[nn] + if ok { + break + } + } + + return a +} + +// 0<=r,g,b <= 5 +func ColorRGB(r, g, b int) Attribute { + within := func(n int) int { + if n < 0 { + return 0 + } + + if n > 5 { + return 5 + } + + return n + } + + r, b, g = within(r), within(b), within(g) + return Attribute(0x0f + 36*r + 6*g + b) +} diff --git a/theme_test.go b/theme_test.go new file mode 100644 index 0000000..b488a09 --- /dev/null +++ b/theme_test.go @@ -0,0 +1,31 @@ +package termui + +import "testing" + +var cmap = map[string]Attribute{ + "fg": ColorWhite, + "bg": ColorDefault, + "border.fg": ColorWhite, + "label.fg": ColorGreen, + "par.fg": ColorYellow, + "par.label.bg": ColorWhite, +} + +func TestLoopUpAttr(t *testing.T) { + tbl := []struct { + name string + should Attribute + }{ + {"par.label.bg", ColorWhite}, + {"par.label.fg", ColorGreen}, + {"par.bg", ColorDefault}, + {"bar.border.fg", ColorWhite}, + {"bar.label.bg", ColorDefault}, + } + + for _, v := range tbl { + if lookUpAttr(cmap, v.name) != v.should { + t.Error(v.name) + } + } +}