forked from quantum/gonsole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.go
241 lines (195 loc) · 4.41 KB
/
edit.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
package gonsole
import (
xs "github.com/huandu/xstrings"
"github.com/nsf/termbox-go"
)
type Edit struct {
BaseControl
value string
maxWidth int
cursorPos int
startingIndex int
onSubmit func(value string)
}
func NewEdit(win AppWindow, parent Container, id string) *Edit {
edit := &Edit{}
edit.Init(win, parent, id, "edit")
edit.SetFocusable(true)
edit.SetCursorable(true)
parent.AddControl(edit)
return edit
}
func (e *Edit) Value() string {
return e.value
}
func (e *Edit) SetValue(value string) {
e.value = value
}
func (e *Edit) MaxWidth() int {
return e.maxWidth
}
func (e *Edit) SetMaxWidth(width int) {
e.maxWidth = width
}
func (e *Edit) OnSubmit(handler func(string)) {
e.onSubmit = handler
}
func (e *Edit) Repaint() {
if !e.Dirty() {
return
}
e.BaseControl.Repaint()
box := e.ContentBox()
var shownValue string
var cursorOffset int
length := xs.Len(e.value)
width := e.ContentBox().Width
if e.startingIndex == 0 {
if length < width {
shownValue = e.value
} else {
shownValue = xs.Slice(e.value, 0, width)
}
cursorOffset = e.cursorPos
} else {
if length-e.startingIndex < width {
shownValue = xs.Slice(e.value, e.startingIndex, -1)
} else {
shownValue = xs.Slice(e.value, e.startingIndex, e.startingIndex+width)
}
cursorOffset = e.cursorPos - e.startingIndex
}
t := e.Theme()
fg, bg := t.ColorTermbox("fg"), t.ColorTermbox("bg")
DrawTextSimple(shownValue, true, box, fg, bg)
if e.Focused() {
DrawTextSimple(" ", false, Box{box.Left + cursorOffset, box.Top, 1, 1}, t.ColorTermbox("cursor"), bg)
DrawCursor(box.Left+cursorOffset, box.Top)
}
}
func (e *Edit) handleChar(ch rune) {
length := xs.Len(e.value)
if e.maxWidth > 0 && length >= e.maxWidth {
return
}
if e.cursorPos == 0 {
e.value = string(ch) + e.value
} else if e.cursorPos < length {
e.value = xs.Slice(e.value, 0, e.cursorPos) + string(ch) + xs.Slice(e.value, e.cursorPos, -1)
} else {
e.value += string(ch)
}
e.cursorPos++
width := e.ContentBox().Width
if e.cursorPos >= width {
e.startingIndex++
}
}
func (e *Edit) handleBackspace() {
if e.cursorPos == 0 || e.value == "" {
return
}
length := xs.Len(e.value)
if e.cursorPos >= length {
e.cursorPos--
e.value = xs.Slice(e.value, 0, length-1)
} else if e.cursorPos == 1 {
e.cursorPos = 0
e.value = xs.Slice(e.value, 1, -1)
} else {
e.cursorPos--
e.value = xs.Slice(e.value, 0, e.cursorPos) + xs.Slice(e.value, e.cursorPos+1, -1)
}
width := e.ContentBox().Width
if width >= length-1 {
e.startingIndex = 0
}
}
func (e *Edit) handleDelete() {
length := xs.Len(e.value)
if e.cursorPos == length || e.value == "" {
return
}
if e.cursorPos == length-1 {
e.value = xs.Slice(e.value, 0, length-1)
} else {
e.value = xs.Slice(e.value, 0, e.cursorPos) + xs.Slice(e.value, e.cursorPos+1, -1)
}
width := e.ContentBox().Width
if width >= length-1 {
e.startingIndex = 0
}
}
func (e *Edit) handleLeft() {
if e.cursorPos == 0 || e.value == "" {
return
}
if e.cursorPos == e.startingIndex {
e.startingIndex--
}
e.cursorPos--
}
func (e *Edit) handleRight() {
length := xs.Len(e.value)
if e.cursorPos == length || e.value == "" {
return
}
e.cursorPos++
width := e.ContentBox().Width
if e.cursorPos < length && e.cursorPos >= e.startingIndex+width-1 {
e.startingIndex++
}
}
func (e *Edit) handleHome() {
e.cursorPos = 0
e.startingIndex = 0
}
func (e *Edit) handleEnd() {
length := xs.Len(e.value)
e.cursorPos = length
width := e.ContentBox().Width
if length >= width {
e.startingIndex = length - width + 1
}
}
func (e *Edit) ParseEvent(ev *termbox.Event) (handled, repaint bool) {
switch ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeySpace:
e.handleChar(' ')
return true, true
case termbox.KeyBackspace, termbox.KeyBackspace2:
e.handleBackspace()
return true, true
case termbox.KeyDelete:
e.handleDelete()
return true, true
case termbox.KeyArrowLeft:
e.handleLeft()
return true, true
case termbox.KeyArrowRight:
e.handleRight()
return true, true
case termbox.KeyHome:
e.handleHome()
return true, true
case termbox.KeyEnd:
e.handleEnd()
return true, true
case termbox.KeyEnter:
if e.onSubmit != nil {
e.onSubmit(e.value)
}
return true, false
default:
if ev.Ch != 0 {
e.handleChar(ev.Ch)
return true, true
}
}
case termbox.EventError:
panic(ev.Err)
}
return false, false
}