forked from methos2016/DuckyManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditbox.go
252 lines (206 loc) · 5.59 KB
/
editbox.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
package main
// Adaptation (pruned code + of the "editBox" example from termbox
import (
"unicode/utf8"
"github.com/mattn/go-runewidth"
"github.com/nsf/termbox-go"
)
const coldef = termbox.ColorDefault
const preferedHorizontalThreshold = 5
const tabstopLength = 8
func printEditBox(eB editBox, editBoxWidth int, title string) (err error) {
w, h := termbox.Size()
midy := h / 2
midx := (w - editBoxWidth) / 2
// unicode box drawing chars around the edit box
termbox.SetCell(midx-1, midy, '│', coldef, coldef)
termbox.SetCell(midx+editBoxWidth, midy, '│', coldef, coldef)
termbox.SetCell(midx-1, midy-1, '┌', coldef, coldef)
termbox.SetCell(midx-1, midy+1, '└', coldef, coldef)
termbox.SetCell(midx+editBoxWidth, midy-1, '┐', coldef, coldef)
termbox.SetCell(midx+editBoxWidth, midy+1, '┘', coldef, coldef)
fill(midx, midy-1, editBoxWidth, 1, termbox.Cell{Ch: '─'})
fill(midx, midy+1, editBoxWidth, 1, termbox.Cell{Ch: '─'})
// Title
guiPrint(midx, midy-1, editBoxWidth, termbox.AttrBold, coldef, title)
eB.Draw(midx, midy, editBoxWidth, 1)
termbox.SetCursor(midx+eB.CursorX(), midy)
err = termbox.Flush()
return
}
func fill(x, y, w, h int, cell termbox.Cell) {
for ly := 0; ly < h; ly++ {
for lx := 0; lx < w; lx++ {
termbox.SetCell(x+lx, y+ly, cell.Ch, cell.Fg, cell.Bg)
}
}
}
func runeAdvanceLen(r rune, pos int) int {
if r == '\t' {
return tabstopLength - pos%tabstopLength
}
return runewidth.RuneWidth(r)
}
func vOffsetCOffset(text []byte, boffset int) (voffset, coffset int) {
text = text[:boffset]
for len(text) > 0 {
r, size := utf8.DecodeRune(text)
text = text[size:]
coffset++
voffset += runeAdvanceLen(r, voffset)
}
return
}
func byteSliceGrow(s []byte, desiredCap int) []byte {
if cap(s) < desiredCap {
ns := make([]byte, len(s), desiredCap)
copy(ns, s)
return ns
}
return s
}
func byteSliceRemove(text []byte, from, to int) []byte {
size := to - from
copy(text[from:], text[to:])
text = text[:len(text)-size]
return text
}
func byteSliceInsert(text []byte, offset int, what []byte) []byte {
n := len(text) + len(what)
text = byteSliceGrow(text, n)
text = text[:n]
copy(text[offset+len(what):], text[offset:])
copy(text[offset:], what)
return text
}
type editBox struct {
text []byte
lineVOffset int
cursorBOffset int // cursor offset in bytes
cursorVOffset int // visual cursor offset in termbox cells
cursorCOffset int // cursor offset in unicode code points
}
func (eb *editBox) Draw(x, y, w, h int) {
eb.AdjustVOffset(w)
fill(x, y, w, h, termbox.Cell{Ch: ' '})
t := eb.text
lx := 0
tabstop := 0
for len(t) != 0 {
rx := lx - eb.lineVOffset
if lx == tabstop {
tabstop += tabstopLength
}
if rx >= w {
termbox.SetCell(x+w-1, y, '→',
coldef, coldef)
break
}
r, size := utf8.DecodeRune(t)
if r == '\t' {
for ; lx < tabstop; lx++ {
rx = lx - eb.lineVOffset
if rx >= 0 && rx < w {
termbox.SetCell(x+rx, y, ' ', coldef, coldef)
}
}
} else {
if rx >= 0 {
termbox.SetCell(x+rx, y, r, coldef, coldef)
}
lx += runewidth.RuneWidth(r)
}
t = t[size:]
}
if eb.lineVOffset != 0 {
termbox.SetCell(x, y, '←', coldef, coldef)
}
}
// Adjusts line visual offset to a proper value depending on width
func (eb *editBox) AdjustVOffset(width int) {
ht := preferedHorizontalThreshold
maxHThreshold := (width - 1) / 2
if ht > maxHThreshold {
ht = maxHThreshold
}
threshold := width - 1
if eb.lineVOffset != 0 {
threshold = width - ht
}
if eb.cursorVOffset-eb.lineVOffset >= threshold {
eb.lineVOffset = eb.cursorVOffset + (ht - width + 1)
}
if eb.lineVOffset != 0 && eb.cursorVOffset-eb.lineVOffset < ht {
eb.lineVOffset = eb.cursorVOffset - ht
if eb.lineVOffset < 0 {
eb.lineVOffset = 0
}
}
}
func (eb *editBox) MoveCursorTo(boffset int) {
eb.cursorBOffset = boffset
eb.cursorVOffset, eb.cursorCOffset = vOffsetCOffset(eb.text, boffset)
}
func (eb *editBox) RuneUnderCursor() (rune, int) {
return utf8.DecodeRune(eb.text[eb.cursorBOffset:])
}
func (eb *editBox) RuneBeforeCursor() (rune, int) {
return utf8.DecodeLastRune(eb.text[:eb.cursorBOffset])
}
func (eb *editBox) MoveCursorOneRuneBackward() {
if eb.cursorBOffset == 0 {
return
}
if eb.cursorCOffset > len(eb.text) {
eb.MoveCursorToBeginningOfTheLine()
return
}
_, size := eb.RuneBeforeCursor()
eb.MoveCursorTo(eb.cursorBOffset - size)
}
func (eb *editBox) MoveCursorOneRuneForward() {
if eb.cursorBOffset == len(eb.text) {
return
}
if eb.cursorCOffset > len(eb.text) {
eb.MoveCursorTo(len(eb.text))
}
_, size := eb.RuneUnderCursor()
eb.MoveCursorTo(eb.cursorBOffset + size)
}
func (eb *editBox) MoveCursorToBeginningOfTheLine() {
eb.MoveCursorTo(0)
}
func (eb *editBox) MoveCursorToEndOfTheLine() {
eb.MoveCursorTo(len(eb.text))
}
func (eb *editBox) DeleteRuneBackward() {
if eb.cursorBOffset == 0 {
return
}
eb.MoveCursorOneRuneBackward()
_, size := eb.RuneUnderCursor()
eb.text = byteSliceRemove(eb.text, eb.cursorBOffset, eb.cursorBOffset+size)
}
func (eb *editBox) DeleteRuneForward() {
if eb.cursorBOffset == len(eb.text) {
return
}
_, size := eb.RuneUnderCursor()
eb.text = byteSliceRemove(eb.text, eb.cursorBOffset, eb.cursorBOffset+size)
}
func (eb *editBox) DeleteTheRestOfTheLine() {
eb.text = eb.text[:eb.cursorBOffset]
}
func (eb *editBox) InsertRune(r rune) {
var buf [utf8.UTFMax]byte
if eb.cursorCOffset > len(eb.text) {
eb.MoveCursorToBeginningOfTheLine()
}
n := utf8.EncodeRune(buf[:], r)
eb.text = byteSliceInsert(eb.text, eb.cursorBOffset, buf[:n])
eb.MoveCursorOneRuneForward()
}
func (eb *editBox) CursorX() int {
return eb.cursorVOffset - eb.lineVOffset
}