forked from quantum/gonsole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme.go
225 lines (180 loc) · 5.5 KB
/
theme.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package gonsole
import "github.com/nsf/termbox-go"
type Attribute uint16
const (
ColorDefault Attribute = iota
ColorBlack
ColorRed
ColorGreen
ColorYellow
ColorBlue
ColorMagenta
ColorCyan
ColorWhite
)
const (
AttrBold Attribute = 1 << (iota + 9)
AttrUnderline
AttrReverse
)
type LineType int
const (
LineNone = iota
LineTransparent
LineSingle
LineSingleCorners
LineDouble
LineDoubleCorners
LineDashed
LineDotted
)
type HorizontalAlignment int
const (
HorizontalAlignmentLeft = iota
HorizontalAlignmentCenter
HorizontalAlignmentRight
)
type VerticalAlignment int
const (
HorizontalAlignmentTop = iota
HorizontalAlignmentMiddle
HorizontalAlignmentBottom
)
type Theme struct {
baseTheme *Theme
prefix string
dict map[string]interface{}
}
func NewTheme(prefix string, baseTheme *Theme) *Theme {
theme := &Theme{
baseTheme: baseTheme,
prefix: prefix,
dict: make(map[string]interface{}),
}
return theme
}
func (t *Theme) getKey(key string) string {
if t.prefix != "" {
return t.prefix + "." + key
}
return key
}
func (t *Theme) getValue(key string) interface{} {
prefixAndKey := t.getKey(key)
value, found := t.dict[prefixAndKey]
if found {
return value
}
if t.baseTheme != nil {
value, found := t.baseTheme.dict[prefixAndKey]
if found {
return value
}
}
return nil
}
func (t *Theme) setValue(key string, value interface{}) {
t.dict[t.getKey(key)] = value
}
func (t *Theme) Color(key string) Attribute {
value := t.getValue(key)
if value != nil {
return value.(Attribute)
}
return ColorDefault
}
func (t *Theme) ColorTermbox(key string) termbox.Attribute {
return termbox.Attribute(t.Color(key))
}
func (t *Theme) SetColor(key string, color Attribute) {
t.setValue(key, color)
}
func (t *Theme) Border(key string) LineType {
value := t.getValue(key)
if value != nil {
return LineType(value.(int))
}
return LineNone
}
func (t *Theme) SetBorder(key string, border LineType) {
t.setValue(key, int(border))
}
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)
}
func termboxAttr(attr Attribute) termbox.Attribute {
return termbox.Attribute(attr)
}
var defaultTheme *Theme
func init() {
defaultTheme = NewTheme("", nil)
defaultTheme.setValue("app.fg", ColorWhite)
defaultTheme.setValue("app.bg", ColorBlue)
defaultTheme.setValue("window.fg", ColorBlack)
defaultTheme.setValue("window.bg", ColorWhite)
defaultTheme.setValue("window.shadow", LineTransparent)
defaultTheme.setValue("window.shadow.fg", ColorBlack)
defaultTheme.setValue("window.border", LineSingle)
defaultTheme.setValue("window.border.fg", ColorBlack)
defaultTheme.setValue("window.border.bg", ColorWhite)
defaultTheme.setValue("window.title.fg", ColorBlack)
defaultTheme.setValue("window.title.bg", ColorWhite)
defaultTheme.setValue("window.focused.fg", ColorBlack)
defaultTheme.setValue("window.focused.bg", ColorWhite)
defaultTheme.setValue("window.focused.shadow", LineTransparent)
defaultTheme.setValue("window.focused.shadow.fg", ColorBlack)
defaultTheme.setValue("window.focused.border", LineSingle)
defaultTheme.setValue("window.focused.border.fg", ColorBlack)
defaultTheme.setValue("window.focused.border.bg", ColorWhite)
defaultTheme.setValue("window.focused.title.fg", ColorRed)
defaultTheme.setValue("window.focused.title.bg", ColorWhite)
defaultTheme.setValue("label.fg", ColorBlack)
defaultTheme.setValue("label.bg", ColorWhite)
defaultTheme.setValue("label.border", LineNone)
defaultTheme.setValue("button.fg", ColorBlack)
defaultTheme.setValue("button.bg", ColorWhite)
defaultTheme.setValue("button.border", LineNone)
defaultTheme.setValue("button.border.fg", ColorBlack)
defaultTheme.setValue("button.border.bg", ColorWhite)
defaultTheme.setValue("button.focused.fg", ColorWhite)
defaultTheme.setValue("button.focused.bg", ColorRed)
defaultTheme.setValue("edit.fg", ColorWhite)
defaultTheme.setValue("edit.bg", ColorBlue)
defaultTheme.setValue("edit.cursor", ColorYellow)
defaultTheme.setValue("edit.border", LineNone)
defaultTheme.setValue("edit.focused.fg", ColorWhite)
defaultTheme.setValue("edit.focused.bg", ColorBlue)
defaultTheme.setValue("edit.focused.border", LineNone)
defaultTheme.setValue("list.fg", ColorBlack)
defaultTheme.setValue("list.bg", ColorWhite)
defaultTheme.setValue("list.border", LineSingle)
defaultTheme.setValue("list.border.fg", ColorBlack)
defaultTheme.setValue("list.border.bg", ColorWhite)
defaultTheme.setValue("list.selected.fg", ColorWhite)
defaultTheme.setValue("list.selected.bg", ColorBlue)
defaultTheme.setValue("list.scroll.fg", ColorBlack)
defaultTheme.setValue("list.scroll.bg", ColorWhite)
defaultTheme.setValue("list.focused.fg", ColorBlack)
defaultTheme.setValue("list.focused.bg", ColorWhite)
defaultTheme.setValue("list.focused.border", LineSingle)
defaultTheme.setValue("list.focused.border.fg", ColorBlack)
defaultTheme.setValue("list.focused.border.bg", ColorWhite)
defaultTheme.setValue("list.focused.selected.fg", ColorWhite)
defaultTheme.setValue("list.focused.selected.bg", ColorRed)
defaultTheme.setValue("list.focused.scroll.fg", ColorBlack)
defaultTheme.setValue("list.focused.scroll.bg", ColorWhite)
defaultTheme.setValue("progress.filled.fg", ColorRed|AttrReverse)
defaultTheme.setValue("progress.filled.bg", ColorBlack)
defaultTheme.setValue("progress.empty.fg", ColorWhite)
defaultTheme.setValue("progress.empty.bg", ColorBlack)
}