forked from ericfreese/rat
/
term_style.go
67 lines (52 loc) · 1.06 KB
/
term_style.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package rat
import (
"sync"
termbox "github.com/nsf/termbox-go"
)
var gTermStyles = NewTermStyles()
type TermStyle interface {
Fg() termbox.Attribute
Bg() termbox.Attribute
}
type TermStyles interface {
Get(termbox.Attribute, termbox.Attribute) TermStyle
Default() TermStyle
}
type termStyle struct {
fg termbox.Attribute
bg termbox.Attribute
}
func (sp *termStyle) Fg() termbox.Attribute {
return sp.fg
}
func (sp *termStyle) Bg() termbox.Attribute {
return sp.bg
}
type termStyles struct {
cache map[tsCacheKey]TermStyle
sync.Mutex
}
type tsCacheKey struct {
fg termbox.Attribute
bg termbox.Attribute
}
func NewTermStyles() TermStyles {
ts := &termStyles{}
ts.cache = make(map[tsCacheKey]TermStyle)
return ts
}
func (ts *termStyles) Get(fg, bg termbox.Attribute) TermStyle {
ts.Lock()
defer ts.Unlock()
k := tsCacheKey{fg, bg}
if s, ok := ts.cache[k]; ok {
return s
} else {
s := &termStyle{fg, bg}
ts.cache[k] = s
return s
}
}
func (ts *termStyles) Default() TermStyle {
return ts.Get(termbox.ColorDefault, termbox.ColorDefault)
}