-
Notifications
You must be signed in to change notification settings - Fork 50
/
edit.go
280 lines (236 loc) · 6.16 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
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
package clui
import (
xs "github.com/huandu/xstrings"
term "github.com/nsf/termbox-go"
"strings"
)
// OnChange sets the callback that is called when EditField content is changed
func (e *EditField) OnChange(fn func(Event)) {
e.onChange = fn
}
// OnKeyPress sets the callback that is called when a user presses a Key while
// the controls is active. If a handler processes the key it should return
// true. If handler returns false it means that the default handler will
// process the key
func (e *EditField) OnKeyPress(fn func(term.Key, rune) bool) {
e.onKeyPress = fn
}
// SetTitle changes the EditField content and emits OnChage eventif the new value does not equal to old one
func (e *EditField) SetTitle(title string) {
e.setTitleInternal(title)
e.offset = 0
e.end()
}
func (e *EditField) setTitleInternal(title string) {
if e.title != title {
e.title = title
if e.onChange != nil {
ev := Event{Msg: title}
e.onChange(ev)
}
}
if title == "" {
e.cursorPos = xs.Len(title)
}
}
// Repaint draws the control on its View surface
func (e *EditField) Draw() {
if e.hidden {
return
}
PushAttributes()
defer PopAttributes()
x, y := e.Pos()
w, _ := e.Size()
parts := []rune(SysObject(ObjEdit))
chLeft, chRight := string(parts[0]), string(parts[1])
chStar := "*"
if len(parts) > 3 {
chStar = string(parts[3])
}
var textOut string
curOff := 0
if e.offset == 0 && xs.Len(e.title) < e.width {
if e.showStars {
textOut = strings.Repeat(chStar, xs.Len(e.title))
} else {
textOut = e.title
}
} else {
fromIdx := 0
toIdx := 0
if e.offset == 0 {
toIdx = e.width - 1
if e.showStars {
textOut = strings.Repeat(chStar, toIdx) + chRight
} else {
textOut = xs.Slice(e.title, 0, toIdx) + chRight
}
curOff = -e.offset
} else {
curOff = 1 - e.offset
fromIdx = e.offset
if e.width-1 <= xs.Len(e.title)-e.offset {
toIdx = e.offset + e.width - 2
if e.showStars {
textOut = chLeft + strings.Repeat(chStar, toIdx-fromIdx) + chRight
} else {
textOut = chLeft + xs.Slice(e.title, fromIdx, toIdx) + chRight
}
} else {
if e.showStars {
textOut = chLeft + strings.Repeat(chStar, xs.Len(e.title)-fromIdx)
} else {
textOut = chLeft + xs.Slice(e.title, fromIdx, -1)
}
}
}
}
fg, bg := RealColor(e.fg, e.Style(), ColorEditText), RealColor(e.bg, e.Style(), ColorEditBack)
if !e.Enabled() {
fg, bg = RealColor(e.fg, e.Style(), ColorDisabledText), RealColor(e.fg, e.Style(), ColorDisabledBack)
} else if e.Active() {
fg, bg = RealColor(e.fg, e.Style(), ColorEditActiveText), RealColor(e.bg, e.Style(), ColorEditActiveBack)
}
SetTextColor(fg)
SetBackColor(bg)
FillRect(x, y, w, 1, ' ')
DrawRawText(x, y, textOut)
if e.Active() {
SetCursorPos(e.cursorPos+e.x+curOff, e.y)
}
}
func (e *EditField) insertRune(ch rune) {
if e.readonly {
return
}
if e.maxWidth > 0 && xs.Len(e.title) >= e.maxWidth {
return
}
idx := e.cursorPos
if idx == 0 {
e.setTitleInternal(string(ch) + e.title)
} else if idx >= xs.Len(e.title) {
e.setTitleInternal(e.title + string(ch))
} else {
e.setTitleInternal(xs.Slice(e.title, 0, idx) + string(ch) + xs.Slice(e.title, idx, -1))
}
e.cursorPos++
if e.cursorPos >= e.width {
if e.offset == 0 {
e.offset = 2
} else {
e.offset++
}
}
}
func (e *EditField) backspace() {
if e.title == "" || e.cursorPos == 0 || e.readonly {
return
}
length := xs.Len(e.title)
if e.cursorPos >= length {
e.cursorPos--
e.setTitleInternal(xs.Slice(e.title, 0, length-1))
} else if e.cursorPos == 1 {
e.cursorPos = 0
e.setTitleInternal(xs.Slice(e.title, 1, -1))
e.offset = 0
} else {
e.cursorPos--
e.setTitleInternal(xs.Slice(e.title, 0, e.cursorPos) + xs.Slice(e.title, e.cursorPos+1, -1))
}
if length-1 < e.width {
e.offset = 0
}
}
func (e *EditField) del() {
length := xs.Len(e.title)
if e.title == "" || e.cursorPos == length || e.readonly {
return
}
if e.cursorPos == length-1 {
e.setTitleInternal(xs.Slice(e.title, 0, length-1))
} else {
e.setTitleInternal(xs.Slice(e.title, 0, e.cursorPos) + xs.Slice(e.title, e.cursorPos+1, -1))
}
if length-1 < e.width {
e.offset = 0
}
}
func (e *EditField) charLeft() {
if e.cursorPos == 0 || e.title == "" {
return
}
if e.cursorPos == e.offset {
e.offset--
}
e.cursorPos--
}
func (e *EditField) charRight() {
length := xs.Len(e.title)
if e.cursorPos == length || e.title == "" {
return
}
e.cursorPos++
if e.cursorPos != length && e.cursorPos >= e.offset+e.width-2 {
e.offset++
}
}
func (e *EditField) home() {
e.offset = 0
e.cursorPos = 0
}
func (e *EditField) end() {
length := xs.Len(e.title)
e.cursorPos = length
if length < e.width {
return
}
e.offset = length - (e.width - 2)
}
// Clear empties the EditField and emits OnChange event
func (e *EditField) Clear() {
e.home()
e.setTitleInternal("")
}
// SetMaxWidth sets the maximum lenght of the EditField text. If the current text is longer it is truncated
func (e *EditField) SetMaxWidth(w int) {
e.maxWidth = w
if w > 0 && xs.Len(e.title) > w {
e.title = xs.Slice(e.title, 0, w)
e.end()
}
}
// MaxWidth returns the current maximum text length. Zero means no limit
func (e *EditField) MaxWidth() int {
return e.maxWidth
}
// SetSize changes control size. Constant DoNotChange can be
// used as placeholder to indicate that the control attrubute
// should be unchanged.
// Method does nothing if new size is less than minimal size
// EditField height cannot be changed - it equals 1 always
func (e *EditField) SetSize(width, height int) {
if width != KeepValue && (width > 1000 || width < e.minW) {
return
}
if height != KeepValue && (height > 200 || height < e.minH) {
return
}
if width != KeepValue {
e.width = width
}
e.height = 1
}
// PasswordMode returns whether password mode is enabled for the control
func (e *EditField) PasswordMode() bool {
return e.showStars
}
// SetPasswordMode changes the way an EditField displays it content.
// If PasswordMode is false then the EditField works as regular text entry
// control. If PasswordMode is true then the EditField shows its content hidden
// with star characters ('*' by default)
func (e *EditField) SetPasswordMode(pass bool) {
e.showStars = pass
}