/
inputbox.go
133 lines (110 loc) · 2.69 KB
/
inputbox.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
package mode
import (
"unicode/utf8"
"github.com/mattn/go-runewidth"
)
func getVoffset(text []byte, boffset int) int {
var voffset int
text = text[:boffset]
for len(text) > 0 {
r, size := utf8.DecodeRune(text)
text = text[size:]
voffset += runewidth.RuneWidth(r)
}
return voffset
}
func makeCap(s []byte, desiredCap int) []byte {
if cap(s) < desiredCap {
ns := make([]byte, len(s), desiredCap)
copy(ns, s)
return ns
}
return s
}
func removeByte(text []byte, from, to int) []byte {
size := to - from
copy(text[from:], text[to:])
text = text[:len(text)-size]
return text
}
func insertByte(text []byte, offset int, what []byte) []byte {
n := len(text) + len(what)
text = makeCap(text, n)
text = text[:n]
copy(text[offset+len(what):], text[offset:])
copy(text[offset:], what)
return text
}
type InputBox struct {
input []byte
prefix string
prefixWidth int
cursorBOffset int
cursorVOffset int
}
func (ib *InputBox) GetInputString() string {
return ib.prefix + string(ib.input)
}
func (ib *InputBox) MoveCursorTo(bOffset int) {
ib.cursorBOffset = bOffset
ib.cursorVOffset = getVoffset(ib.input, bOffset)
}
func (ib *InputBox) RuneUnderCursor() (rune, int) {
return utf8.DecodeRune(ib.input[ib.cursorBOffset:])
}
func (ib *InputBox) RuneBeforeCursor() (rune, int) {
return utf8.DecodeLastRune(ib.input[:ib.cursorBOffset])
}
func (ib *InputBox) MoveCursorOneRuneBackward() {
if ib.cursorBOffset == 0 {
return
}
_, size := ib.RuneBeforeCursor()
ib.MoveCursorTo(ib.cursorBOffset - size)
}
func (ib *InputBox) MoveCursorOneRuneForward() {
if ib.cursorBOffset == len(ib.input) {
return
}
_, size := ib.RuneUnderCursor()
ib.MoveCursorTo(ib.cursorBOffset + size)
}
func (ib *InputBox) DeleteRuneBackward() {
if ib.cursorBOffset == 0 {
return
}
ib.MoveCursorOneRuneBackward()
_, size := ib.RuneUnderCursor()
ib.input = removeByte(ib.input, ib.cursorBOffset, ib.cursorBOffset+size)
}
func (ib *InputBox) DeleteRuneForward() {
if ib.cursorBOffset == len(ib.input) {
return
}
_, size := ib.RuneUnderCursor()
ib.input = removeByte(ib.input, ib.cursorBOffset, ib.cursorBOffset+size)
}
func (ib *InputBox) DeleteAll() {
ib.MoveCursorTo(0)
ib.input = []byte{}
}
func (ib *InputBox) InsertRune(r rune) {
var buf [utf8.UTFMax]byte
n := utf8.EncodeRune(buf[:], r)
ib.input = insertByte(ib.input, ib.cursorBOffset, buf[:n])
ib.MoveCursorOneRuneForward()
}
func (ib *InputBox) InsertStr(s string) {
for _, r := range s {
ib.InsertRune(r)
}
}
func (ib *InputBox) GetCursorPos() int {
return ib.cursorVOffset + ib.prefixWidth
}
func NewInputBox(prefix string) *InputBox {
return &InputBox{
prefix: prefix,
prefixWidth: runewidth.StringWidth(prefix),
}
}