-
Notifications
You must be signed in to change notification settings - Fork 0
/
editbox.go
159 lines (138 loc) · 3.41 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
package main
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
termbox "github.com/nsf/termbox-go"
)
const preferredHorizontalThreshold = 3
const editboxWidth = 14
type editbox struct {
text []byte
curByteOffset int // cursor offset in bytes
x, y int
w, h int
warn, err bool
}
func newEditbox() editbox {
return editbox{
w: editboxWidth,
h: 1,
}
}
// Draws the editbox at (x,y) position.
func (eb *editbox) draw() {
drawRect(eb.x, eb.y, eb.w, eb.h)
t := eb.text
lx := 0
for {
if len(t) == 0 {
break
}
r, size := utf8.DecodeRune(t)
// topdown words
if lx == 0 && unicode.IsLetter(r) {
termbox.SetCell(eb.x, eb.y, '↓', fgcolor|termbox.AttrBold, bgcolor)
}
// leftright words
if lx == 0 && unicode.IsNumber(r) {
termbox.SetCell(eb.x, eb.y, '→', fgcolor|termbox.AttrBold, bgcolor)
}
// pass
if lx == 0 && r == '-' {
termbox.SetCell(eb.x, eb.y, '⚐', fgcolor|termbox.AttrBold, bgcolor)
}
termbox.SetCell(eb.x+lx+2, eb.y, r, fgcolor, bgcolor)
lx += 1
t = t[size:]
}
termbox.SetCursor(eb.x+lx+2, eb.y)
// error/warning indicator
if eb.warn {
drawRectWithAttr(eb.x, eb.y, eb.w, eb.h, fgcolor, bgcolor)
termbox.SetCell(eb.x, eb.y, '❗', fgcolor, bgcolor)
eb.warn = false
} else if eb.err {
drawRectWithAttr(eb.x, eb.y, eb.w, eb.h, fgcolor, bgcolor)
termbox.SetCell(eb.x, eb.y, '⊗', fgcolor, bgcolor)
eb.err = false
}
}
func (eb *editbox) getPlaceWord() (string, string, error) {
s := strings.Fields(string(eb.text))
if len(s) == 1 && s[0] == "-" {
return s[0], "", nil
}
if len(s) != 2 {
return "", "", fmt.Errorf("`B2 SELAM` formatinda olmali")
}
return s[0], s[1], nil
}
func (eb *editbox) clear() {
eb.moveCursorTo(0)
eb.text = eb.text[:eb.curByteOffset]
}
func (eb *editbox) moveCursorTo(boffset int) {
eb.curByteOffset = boffset
}
func (eb *editbox) runeUnderCursor() (rune, int) {
return utf8.DecodeRune(eb.text[eb.curByteOffset:])
}
func (eb *editbox) runeBeforeCursor() (rune, int) {
return utf8.DecodeLastRune(eb.text[:eb.curByteOffset])
}
func (eb *editbox) moveCursorOneRuneBackward() {
if eb.curByteOffset == 0 {
return
}
_, size := eb.runeBeforeCursor()
eb.moveCursorTo(eb.curByteOffset - size)
}
func (eb *editbox) moveCursorOneRuneForward() {
if eb.curByteOffset == len(eb.text) {
return
}
_, size := eb.runeUnderCursor()
eb.moveCursorTo(eb.curByteOffset + size)
}
func (eb *editbox) deleteRuneBackward() {
if eb.curByteOffset == 0 {
return
}
eb.moveCursorOneRuneBackward()
_, size := eb.runeUnderCursor()
eb.text = byteSliceRemove(eb.text, eb.curByteOffset, eb.curByteOffset+size)
}
func (eb *editbox) insertRune(r rune) {
// ___ _______ -> total of 11 runes
if utf8.RuneCount(eb.text) >= 11 {
return
}
var buf [utf8.UTFMax]byte
n := utf8.EncodeRune(buf[:], r)
eb.text = byteSliceInsert(eb.text, eb.curByteOffset, buf[:n])
eb.moveCursorOneRuneForward()
}
func byteSliceGrow(s []byte, desired_cap int) []byte {
if cap(s) < desired_cap {
ns := make([]byte, len(s), desired_cap)
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
}