forked from Thinkofname/steven-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvar.go
303 lines (264 loc) · 6.53 KB
/
cvar.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2015 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package console
import (
"bytes"
"fmt"
"strings"
"github.com/thinkofdeath/steven/format"
)
var (
cvars cvarList
configOverride bool
)
// Property is a flag for cvar to control how its handled
type Property int
const (
// Mutable marks whether the var can be changed from the
// console
Mutable Property = 1 << iota
// Serializable marks the var to be saved and loaded
Serializable
)
func (p Property) is(po Property) bool { return p&po == po }
type cvar struct {
properties Property
documentation string
callback func()
}
type cvarI interface {
Name() string
props() Property
}
func (c *cvar) props() Property { return c.properties }
// Doc sets the documentation for this cvar
func (c *cvar) Doc(d string) { c.documentation = strings.TrimSpace(d) }
// Callback sets the callback to be called when the value is updated
func (c *cvar) Callback(cb func()) { c.callback = cb }
func (c *cvar) String() string {
var buf bytes.Buffer
for _, line := range strings.Split(c.documentation, "\n") {
buf.WriteString("# ")
buf.WriteString(line)
buf.WriteRune('\n')
}
return buf.String()
}
func (c *cvar) printDoc() {
for _, line := range strings.Split(c.documentation, "\n") {
Component(format.Build("# ").
Color(format.DarkGray).
Append(line).
Color(format.Gray).
Create())
}
}
// IntVar is a console var that contains an integer
type IntVar struct {
cvar
name string
value int
}
// NewIntVar creates and registers a integer console variable
func NewIntVar(name string, val int, props ...Property) *IntVar {
i := &IntVar{
name: name,
value: val,
}
cvars = append(cvars, i)
for _, p := range props {
i.properties |= p
}
Register(fmt.Sprintf("%s", name), func() {
i.printDoc()
i.print()
})
Register(fmt.Sprintf("%s %%", name), func(v int) {
if configOverride || i.properties.is(Mutable) {
i.SetValue(v)
if !configOverride {
i.print()
}
} else {
panic("this var is not mutable")
}
})
return i
}
func (i *IntVar) String() string {
var buf bytes.Buffer
buf.WriteString(i.cvar.String())
buf.WriteString(i.name)
buf.WriteRune(' ')
fmt.Fprint(&buf, i.value)
return buf.String()
}
func (i *IntVar) Name() string { return i.name }
// Doc sets the documentation for this cvar
func (i *IntVar) Doc(d string) *IntVar { i.cvar.Doc(d); return i }
// Callback sets the callback to be called when the value is updated
func (i *IntVar) Callback(cb func()) *IntVar { i.cvar.Callback(cb); return i }
func (i *IntVar) Value() int { return i.value }
func (i *IntVar) SetValue(v int) {
i.value = v
if i.callback != nil {
i.callback()
}
if i.properties.is(Serializable) {
saveConf()
}
}
func (i *IntVar) print() {
Component(format.Build(i.name).
Color(format.Aqua).
Append(" ").
Append(fmt.Sprint(i.value)).
Color(format.Aqua).
Create(),
)
}
// StringVar is a console var that contains an string
type StringVar struct {
cvar
name string
value string
}
// NewStringVar creates and registers a string console variable
func NewStringVar(name, val string, props ...Property) *StringVar {
s := &StringVar{
name: name,
value: val,
}
cvars = append(cvars, s)
for _, p := range props {
s.properties |= p
}
Register(fmt.Sprintf("%s", name), func() {
s.printDoc()
s.print()
})
Register(fmt.Sprintf("%s %%", name), func(v string) {
if configOverride || s.properties.is(Mutable) {
s.SetValue(v)
if !configOverride {
s.print()
}
} else {
panic("this var is not mutable")
}
})
return s
}
func (s *StringVar) String() string {
var buf bytes.Buffer
buf.WriteString(s.cvar.String())
buf.WriteString(s.name)
buf.WriteRune(' ')
fmt.Fprintf(&buf, "\"%s\"", s.value)
return buf.String()
}
func (s *StringVar) Name() string { return s.name }
// Doc sets the documentation for this cvar
func (s *StringVar) Doc(d string) *StringVar { s.cvar.Doc(d); return s }
// Callback sets the callback to be called when the value is updated
func (s *StringVar) Callback(cb func()) *StringVar { s.cvar.Callback(cb); return s }
func (s *StringVar) Value() string { return s.value }
func (s *StringVar) SetValue(v string) {
s.value = v
if s.callback != nil {
s.callback()
}
if s.properties.is(Serializable) {
saveConf()
}
}
func (s *StringVar) print() {
Component(format.Build(s.name).
Color(format.Aqua).
Append(" ").
Append("\"").Color(format.Yellow).
Append(s.value).
Color(format.Aqua).
Append("\"").Color(format.Yellow).
Create(),
)
}
// BoolVar is a console var that contains an bool
type BoolVar struct {
cvar
name string
value bool
}
// NewBoolVar creates and registers a bool console variable
func NewBoolVar(name string, val bool, props ...Property) *BoolVar {
b := &BoolVar{
name: name,
value: val,
}
cvars = append(cvars, b)
for _, p := range props {
b.properties |= p
}
Register(fmt.Sprintf("%s", name), func() {
b.printDoc()
b.print()
})
Register(fmt.Sprintf("%s %%", name), func(v bool) {
if configOverride || b.properties.is(Mutable) {
b.SetValue(v)
if !configOverride {
b.print()
}
} else {
panic("this var is not mutable")
}
})
return b
}
func (b *BoolVar) String() string {
var buf bytes.Buffer
buf.WriteString(b.cvar.String())
buf.WriteString(b.name)
buf.WriteRune(' ')
fmt.Fprint(&buf, b.value)
return buf.String()
}
func (b *BoolVar) Name() string { return b.name }
// Doc sets the documentation for this cvar
func (b *BoolVar) Doc(d string) *BoolVar { b.cvar.Doc(d); return b }
// Callback sets the callback to be called when the value is updated
func (b *BoolVar) Callback(cb func()) *BoolVar { b.cvar.Callback(cb); return b }
func (b *BoolVar) Value() bool { return b.value }
func (b *BoolVar) SetValue(v bool) {
b.value = v
if b.callback != nil {
b.callback()
}
if b.properties.is(Serializable) {
saveConf()
}
}
func (b *BoolVar) print() {
col := format.Red
if b.value {
col = format.Green
}
Component(format.Build(b.name).
Color(format.Aqua).
Append(" ").
Append(fmt.Sprint(b.value)).
Color(col).
Create(),
)
}