This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinputField.go
374 lines (315 loc) · 7.27 KB
/
inputField.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package component
import (
"fmt"
"strings"
"github.com/jroimartin/gocui"
)
// Margin struct
type Margin struct {
top int
left int
}
// InputField struct
type InputField struct {
*gocui.Gui
label *Label
field *Field
}
// Label struct
type Label struct {
text string
width int
drawFrame bool
*Position
*Attributes
margin *Margin
}
// Field struct
type Field struct {
text string
width int
drawFrame bool
handlers Handlers
margin *Margin
mask bool
editable bool
ctype ComponentType
*Position
*Attributes
*Validator
}
var labelPrefix = "label"
// NewInputField new input label and field
func NewInputField(gui *gocui.Gui, labelText string, x, y, labelWidth, fieldWidth int) *InputField {
gui.Cursor = true
// label psition
lp := &Position{
x,
y,
x + labelWidth + 1,
y + 2,
}
// field position
fp := &Position{
lp.W,
lp.Y,
lp.W + fieldWidth,
lp.H,
}
// new label
label := &Label{
text: labelText,
width: labelWidth,
Position: lp,
Attributes: &Attributes{
textColor: gocui.ColorYellow,
textBgColor: gocui.ColorDefault,
},
drawFrame: false,
margin: &Margin{
top: 0,
left: 0,
},
}
// new field
field := &Field{
width: fieldWidth,
Position: fp,
Attributes: &Attributes{
textColor: gocui.ColorBlack,
textBgColor: gocui.ColorCyan,
},
handlers: make(Handlers),
drawFrame: false,
margin: &Margin{
top: 0,
left: 0,
},
Validator: NewValidator(gui, label.text+"validator", fp.X, fp.Y+1, fp.W, fp.H+1),
editable: true,
ctype: TypeInputField,
}
// new input field
i := &InputField{
Gui: gui,
label: label,
field: field,
}
return i
}
// AddFieldAttribute add field colors
func (i *InputField) AddFieldAttribute(textColor, textBgColor, fgColor, bgColor gocui.Attribute) *InputField {
i.field.Attributes = &Attributes{
textColor: textColor,
textBgColor: textBgColor,
hilightColor: fgColor,
hilightBgColor: bgColor,
}
return i
}
// AddLabelAttribute add label colors
func (i *InputField) AddLabelAttribute(textColor, textBgColor gocui.Attribute) *InputField {
i.label.Attributes = &Attributes{
textColor: textColor,
textBgColor: textBgColor,
}
return i
}
// AddHandler add keybinding
func (i *InputField) AddHandler(key Key, handler Handler) *InputField {
i.field.handlers[key] = handler
return i
}
// AddMarginTop add margin top
func (i *InputField) AddMarginTop(top int) *InputField {
i.label.margin.top += top
i.field.margin.top += top
return i
}
// AddMarginLeft add margin left
func (i *InputField) AddMarginLeft(left int) *InputField {
i.label.margin.left += left
i.field.margin.left += left
return i
}
// AddValidate add input validator
func (i *InputField) AddValidate(errMsg string, validate func(value string) bool) *InputField {
i.field.AddValidate(errMsg, validate)
return i
}
// SetLabelBorder draw label border
func (i *InputField) SetLabelBorder() *InputField {
i.label.drawFrame = true
return i
}
// SetFieldBorder draw field border
func (i *InputField) SetFieldBorder() *InputField {
i.field.drawFrame = true
return i
}
// SetMask set input field to mask '*'
func (i *InputField) SetMask() *InputField {
i.field.mask = true
return i
}
// SetMaskKeybinding set or unset input field to mask '*' with key
func (i *InputField) SetMaskKeybinding(key Key) *InputField {
if err := i.Gui.SetKeybinding(i.label.text, key, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error {
v.Mask ^= '*'
return nil
}); err != nil {
panic(err)
}
return i
}
// SetText set text
func (i *InputField) SetText(text string) *InputField {
i.field.text = text
return i
}
// SetCursor set input field cursor
func (i *InputField) SetCursor(b bool) *InputField {
i.Gui.Cursor = b
return i
}
// SetEditable if editmode is true can input
func (i *InputField) SetEditable(b bool) *InputField {
i.field.editable = b
return i
}
// Focus focus to input field
func (i *InputField) Focus() {
i.Gui.Cursor = true
i.Gui.SetCurrentView(i.label.text)
}
// UnFocus un focus
func (i *InputField) UnFocus() {
i.Gui.Cursor = false
}
// Edit input field editor
func (i *InputField) Edit(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
switch {
case ch != 0 && mod == 0:
v.EditWrite(ch)
case key == gocui.KeySpace:
v.EditWrite(' ')
case key == gocui.KeyBackspace || key == gocui.KeyBackspace2:
v.EditDelete(true)
case key == gocui.KeyArrowLeft:
v.MoveCursor(-1, 0, false)
case key == gocui.KeyArrowRight:
v.MoveCursor(+1, 0, false)
}
// get field text
i.field.text = i.cutNewline(v.Buffer())
// validate
i.field.Validate(i.GetFieldText())
}
// GetFieldText get input field text
func (i *InputField) GetFieldText() string {
return i.field.text
}
// GetLabel get label text
func (i *InputField) GetLabel() string {
return i.label.text
}
// GetPosition get input field position
func (i *InputField) GetPosition() *Position {
return i.field.Position
}
// Validate validate field
func (i *InputField) Validate() bool {
i.field.Validate(i.GetFieldText())
return i.field.IsValid()
}
// IsValid valid field data will be return true
func (i *InputField) IsValid() bool {
return i.field.Validator.IsValid()
}
// GetType get component type
func (i *InputField) GetType() ComponentType {
return i.field.ctype
}
// Draw draw label and field
func (i *InputField) Draw() {
// draw label
x, y, w, h := i.addMargin(i.label)
if v, err := i.Gui.SetView(labelPrefix+i.label.text, x, y, w, h); err != nil {
if err != gocui.ErrUnknownView {
panic(err)
}
v.Frame = i.label.drawFrame
v.FgColor = i.label.textColor | gocui.AttrBold
v.BgColor = i.label.textBgColor
fmt.Fprint(v, i.label.text)
}
// draw input
x, y, w, h = i.addMargin(i.field)
if v, err := i.Gui.SetView(i.label.text, x, y, w, h); err != nil {
if err != gocui.ErrUnknownView {
panic(err)
}
v.Frame = i.field.drawFrame
v.FgColor = i.field.textColor
v.BgColor = i.field.textBgColor
v.Editable = i.field.editable
v.Editor = i
if i.field.mask {
v.Mask = '*'
}
if i.field.text != "" {
fmt.Fprint(v, i.field.text)
}
// focus input field
i.Focus()
}
// set keybindings
if i.field.handlers != nil {
for key, handler := range i.field.handlers {
if err := i.Gui.SetKeybinding(i.label.text, key, gocui.ModNone, handler); err != nil {
panic(err)
}
}
}
}
// Close close input field
func (i *InputField) Close() {
views := []string{
i.label.text,
labelPrefix + i.label.text,
}
for _, v := range views {
if err := i.DeleteView(v); err != nil {
if err != gocui.ErrUnknownView {
panic(err)
}
}
}
if i.field.handlers != nil {
i.DeleteKeybindings(i.label.text)
}
if i.field.Validator != nil {
i.field.Validator.CloseValidateMsg()
}
}
func (i *InputField) addMargin(view interface{}) (int, int, int, int) {
switch v := view.(type) {
case *Field:
p := v.Position
m := v.margin
return p.X + m.left, p.Y + m.top, p.W + m.left, p.H + m.top
case *Label:
p := v.Position
m := v.margin
return p.X + m.left, p.Y + m.top, p.W + m.left, p.H + m.top
default:
panic("Unkown type")
}
}
func (i *InputField) cutNewline(text string) string {
return strings.Replace(text, "\n", "", -1)
}
// AddHandlerOnly add handler not return
func (i *InputField) AddHandlerOnly(key Key, handler Handler) {
i.AddHandler(key, handler)
}