Theme map lookup

This commit is contained in:
gizak 2015-09-21 03:11:58 -04:00
parent 1cb28b0a3c
commit 196d9aae34
7 changed files with 103 additions and 20 deletions

View File

@ -125,22 +125,22 @@ type Block struct {
func NewBlock() *Block { func NewBlock() *Block {
b := Block{} b := Block{}
b.Display = true b.Display = true
b.Border = theme.HasBorder b.Border = true
b.BorderLeft = true b.BorderLeft = true
b.BorderRight = true b.BorderRight = true
b.BorderTop = true b.BorderTop = true
b.BorderBottom = true b.BorderBottom = true
b.BorderBg = theme.BorderBg b.BorderBg = ThemeAttr("border.bg")
b.BorderFg = theme.BorderFg b.BorderFg = ThemeAttr("border.fg")
b.BorderLabelBg = theme.BorderLabelTextBg b.BorderLabelBg = ThemeAttr("label.bg")
b.BorderLabelFg = theme.BorderLabelTextFg b.BorderLabelFg = ThemeAttr("label.fg")
b.Bg = theme.BlockBg b.Bg = ThemeAttr("block.bg")
b.Width = 2 b.Width = 2
b.Height = 2 b.Height = 2
return &b return &b
} }
// Align computes box mob.l // Align computes box model
func (b *Block) Align() { func (b *Block) Align() {
b.area.Min.X = b.X b.area.Min.X = b.X
b.area.Min.Y = b.Y b.area.Min.Y = b.Y

View File

@ -233,8 +233,7 @@ func (es *EvtStream) Loop() {
es.RLock() es.RLock()
defer es.RUnlock() defer es.RUnlock()
if pattern := es.match(a.Path); pattern != "" { if pattern := es.match(a.Path); pattern != "" {
h := es.Handlers[pattern] es.Handlers[pattern](a)
h(a)
} }
}(e) }(e)
} }

View File

@ -22,18 +22,18 @@ var ps = []string{
"/a/b/c/d/"} "/a/b/c/d/"}
func TestMatchScore(t *testing.T) { func TestMatchScore(t *testing.T) {
chk := func(a, b string, s int) { chk := func(a, b string, s bool) {
if c := MatchScore(a, b); c != s { if c := isPathMatch(a, b); c != s {
t.Errorf("\na:%s\nb:%s\nshould:%d\nscore:%d", a, b, s, c) t.Errorf("\na:%s\nb:%s\nshould:%t\nactual:%t", a, b, s, c)
} }
} }
chk(ps[1], ps[1], 0) chk(ps[1], ps[1], true)
chk(ps[1], ps[2], -1) chk(ps[1], ps[2], true)
chk(ps[2], ps[1], 0) chk(ps[2], ps[1], false)
chk(ps[4], ps[1], 0) chk(ps[4], ps[1], false)
chk(ps[6], ps[2], 1) chk(ps[6], ps[2], false)
chk(ps[4], ps[5], -1) chk(ps[4], ps[5], false)
} }
func TestCrtEvt(t *testing.T) { func TestCrtEvt(t *testing.T) {

View File

@ -63,7 +63,7 @@ func TermHeight() int {
// right could overlap on left ones. // right could overlap on left ones.
func Render(bs ...Bufferer) { func Render(bs ...Bufferer) {
// set tm bg // set tm bg
tm.Clear(tm.ColorDefault, toTmAttr(theme.BodyBg)) tm.Clear(tm.ColorDefault, toTmAttr(ThemeAttr("bg")))
for _, b := range bs { for _, b := range bs {
buf := b.Buffer() buf := b.Buffer()
// set cels in buf // set cels in buf

View File

@ -40,7 +40,6 @@ func main() {
}) })
termui.Handle("/timer/1s", func(e termui.Event) { termui.Handle("/timer/1s", func(e termui.Event) {
//debug.Logf("<-%v\n", e)
t := e.Data.(termui.EvtTimer) t := e.Data.(termui.EvtTimer)
if t.Count%2 == 0 { if t.Count%2 == 0 {

View File

@ -4,6 +4,9 @@
package termui package termui
import "strings"
/*
// A ColorScheme represents the current look-and-feel of the dashboard. // A ColorScheme represents the current look-and-feel of the dashboard.
type ColorScheme struct { type ColorScheme struct {
BodyBg Attribute BodyBg Attribute
@ -82,3 +85,54 @@ func UseTheme(th string) {
theme = themeDefault 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)
}

31
theme_test.go Normal file
View File

@ -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)
}
}
}