-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_field.go
101 lines (89 loc) · 1.97 KB
/
input_field.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
package main
import (
"fmt"
"github.com/nsf/termbox-go"
)
type InputField struct {
WM *WindowManager
X, Y, Width, Cursor int
FgColor, BgColor termbox.Attribute
Value string
HasFocus, IsEnabled bool
}
func NewInputField(wm *WindowManager) *InputField {
return &InputField{
WM: wm,
Width: 50,
FgColor: termbox.ColorDefault | termbox.AttrUnderline,
BgColor: termbox.ColorDefault,
IsEnabled: true,
}
}
func (f *InputField) Event(e termbox.Event) {
if !f.IsEnabled {
return
}
if e.Type == termbox.EventKey {
switch {
case e.Key == termbox.KeyHome:
f.Home()
case e.Key == termbox.KeyEnd:
f.End()
case e.Key == termbox.KeySpace:
f.InsertRune(' ')
case e.Key == termbox.KeyBackspace || e.Key == termbox.KeyBackspace2:
f.Backspace()
case e.Key == termbox.KeyDelete:
f.Delete()
case e.Key == termbox.KeyArrowLeft && f.Cursor > 0:
f.Cursor--
case e.Key == termbox.KeyArrowRight && f.Cursor < len(f.Value):
f.Cursor++
case e.Ch != 0:
f.InsertRune(e.Ch)
default:
return
}
f.WM.Render()
}
}
func (f *InputField) InsertRune(r rune) {
f.Value = f.Value[:f.Cursor] + string(r) + f.Value[f.Cursor:]
f.Cursor++
}
func (f *InputField) Home() {
f.Cursor = 0
}
func (f *InputField) End() {
f.Cursor = len(f.Value)
}
func (f *InputField) Render() {
value := f.Value
l := len(value)
if l >= f.Width {
value = fmt.Sprintf("...%s", value[l+4-f.Width:l])
}
value = fmt.Sprintf(fmt.Sprintf("%%-%ds", f.Width), value)
f.WM.CursorX = f.X
f.WM.CursorY = f.Y
f.WM.Print(value, f.FgColor, f.BgColor)
if f.IsEnabled && f.HasFocus {
termbox.SetCursor(f.X+f.Cursor, f.Y)
}
}
func (f *InputField) Backspace() {
if f.Cursor > 0 {
f.Value = f.Value[:f.Cursor-1] + f.Value[f.Cursor:]
f.Cursor--
}
}
func (f *InputField) Delete() {
if f.Cursor < len(f.Value)-1 {
f.Value = f.Value[:f.Cursor] + f.Value[f.Cursor+1:]
}
}
func (f *InputField) Clear() {
f.Value = ""
f.Cursor = 0
f.WM.Render()
}