-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patheditbox.go
149 lines (121 loc) · 3.15 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
package main
import (
"unicode/utf8"
"github.com/nsf/termbox-go"
)
type Editbox struct {
text []byte
line_voffset int
cursor_boffset int // cursor offset in bytes
cursor_voffset int // visual cursor offset in termbox cells
cursor_coffset int // cursor offset in unicode code points
// colors
fg termbox.Attribute
bg termbox.Attribute
}
// Draws the Editbox in the given location
func (e *Editbox) Draw(x, y, w int) {
e.AdjustVOffset(w)
tclear(x, y, w, 1)
t := e.text
lx := 0
for {
rx := lx - e.line_voffset
if len(t) == 0 {
break
}
if rx >= w {
termbox.SetCell(x+w-1, y, '→', e.fg, e.bg)
break
}
r, size := utf8.DecodeRune(t)
if rx >= 0 {
termbox.SetCell(x+rx, y, r, e.fg, e.bg)
}
lx += 1
t = t[size:]
}
if e.line_voffset != 0 {
termbox.SetCell(x, y, '←', e.fg, e.bg)
}
}
// Adjusts line visual offset to a proper value depending on width
func (m *Editbox) AdjustVOffset(width int) {
ht := 0
threshold := width - 1
if m.line_voffset != 0 {
threshold = width - ht
}
if m.cursor_voffset-m.line_voffset >= threshold {
m.line_voffset = m.cursor_voffset + (ht - width + 1)
}
if m.line_voffset != 0 && m.cursor_voffset-m.line_voffset < ht {
m.line_voffset = m.cursor_voffset - ht
if m.line_voffset < 0 {
m.line_voffset = 0
}
}
}
func (m *Editbox) Contents() string {
return string(m.text)
}
func (m *Editbox) MoveCursorTo(boffset int) {
m.cursor_boffset = boffset
m.cursor_voffset, m.cursor_coffset = voffset_coffset(m.text, boffset)
}
func (m *Editbox) RuneUnderCursor() (rune, int) {
return utf8.DecodeRune(m.text[m.cursor_boffset:])
}
func (m *Editbox) RuneBeforeCursor() (rune, int) {
return utf8.DecodeLastRune(m.text[:m.cursor_boffset])
}
func (m *Editbox) MoveCursorOneRuneBackward() {
if m.cursor_boffset == 0 {
return
}
_, size := m.RuneBeforeCursor()
m.MoveCursorTo(m.cursor_boffset - size)
}
func (m *Editbox) MoveCursorOneRuneForward() {
if m.cursor_boffset == len(m.text) {
return
}
_, size := m.RuneUnderCursor()
m.MoveCursorTo(m.cursor_boffset + size)
}
func (m *Editbox) MoveCursorToBeginningOfTheLine() {
m.MoveCursorTo(0)
}
func (m *Editbox) MoveCursorToEndOfTheLine() {
m.MoveCursorTo(len(m.text))
}
func (m *Editbox) DeleteRuneBackward() {
if m.cursor_boffset == 0 {
return
}
m.MoveCursorOneRuneBackward()
_, size := m.RuneUnderCursor()
m.text = byte_slice_remove(m.text, m.cursor_boffset, m.cursor_boffset+size)
}
func (m *Editbox) DeleteRuneForward() {
if m.cursor_boffset == len(m.text) {
return
}
_, size := m.RuneUnderCursor()
m.text = byte_slice_remove(m.text, m.cursor_boffset, m.cursor_boffset+size)
}
func (m *Editbox) DeleteTheRestOfTheLine() {
m.text = m.text[:m.cursor_boffset]
}
func (m *Editbox) InsertRune(r rune) {
var buf [utf8.UTFMax]byte
n := utf8.EncodeRune(buf[:], r)
m.text = byte_slice_insert(m.text, m.cursor_boffset, buf[:n])
m.MoveCursorOneRuneForward()
// fmt.Println("NEW", buf, string(m.text))
}
// Please, keep in mind that cursor depends on the value of line_voffset, which
// is being set on Draw() call, so.. call this method after Draw() one.
func (m *Editbox) CursorX() int {
return m.cursor_voffset - m.line_voffset
}