forked from marcusolsson/tui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runebuf.go
177 lines (150 loc) · 3.59 KB
/
runebuf.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
package tui
import (
"image"
"strings"
"unicode/utf8"
"github.com/marcusolsson/tui-go/wordwrap"
runewidth "github.com/mattn/go-runewidth"
)
// RuneBuffer provides readline functionality for text widgets.
type RuneBuffer struct {
buf []rune
idx int
wordwrap bool
width int
}
// SetMaxWidth sets the maximum text width.
func (r *RuneBuffer) SetMaxWidth(w int) {
r.width = w
}
// Width returns the width of the rune buffer, taking into account for CJK.
func (r *RuneBuffer) Width() int {
return runewidth.StringWidth(string(r.buf))
}
// Set the buffer and the index at the end of the buffer.
func (r *RuneBuffer) Set(buf []rune) {
r.SetWithIdx(len(buf), buf)
}
// SetWithIdx set the the buffer with a given index.
func (r *RuneBuffer) SetWithIdx(idx int, buf []rune) {
r.buf = buf
r.idx = idx
}
// WriteRune appends a rune to the buffer.
func (r *RuneBuffer) WriteRune(s rune) {
r.WriteRunes([]rune{s})
}
// WriteRunes appends runes to the buffer.
func (r *RuneBuffer) WriteRunes(s []rune) {
tail := append(s, r.buf[r.idx:]...)
r.buf = append(r.buf[:r.idx], tail...)
r.idx += len(s)
}
// Pos returns the current index in the buffer.
func (r *RuneBuffer) Pos() int {
return r.idx
}
// Len returns the number of runes in the buffer.
func (r *RuneBuffer) Len() int {
return len(r.buf)
}
// SplitByLine returns the lines for a given width.
func (r *RuneBuffer) SplitByLine() []string {
return r.getSplitByLine(r.width)
}
func (r *RuneBuffer) getSplitByLine(w int) []string {
var text string
if r.wordwrap {
text = wordwrap.WrapString(r.String(), w)
} else {
text = r.String()
}
return strings.Split(text, "\n")
}
// CursorPos returns the coordinate for the cursor for a given width.
func (r *RuneBuffer) CursorPos() image.Point {
if r.width == 0 {
return image.Point{}
}
sp := r.SplitByLine()
var x, y int
remaining := r.idx
for _, l := range sp {
if utf8.RuneCountInString(l) < remaining {
y++
remaining -= utf8.RuneCountInString(l) + 1
} else {
x = remaining
break
}
}
return image.Pt(stringWidth(string(r.buf[:x])), y)
}
func (r *RuneBuffer) String() string {
return string(r.buf)
}
// Runes return the buffer
func (r *RuneBuffer) Runes() []rune {
return r.buf
}
// MoveBackward moves the cursor back by one rune.
func (r *RuneBuffer) MoveBackward() {
if r.idx == 0 {
return
}
r.idx--
}
// MoveForward moves the cursor forward by one rune.
func (r *RuneBuffer) MoveForward() {
if r.idx == len(r.buf) {
return
}
r.idx++
}
// MoveToLineStart moves the cursor to the start of the current line.
func (r *RuneBuffer) MoveToLineStart() {
for i := r.idx; i > 0; i-- {
if r.buf[i-1] == '\n' {
r.idx = i
return
}
}
r.idx = 0
}
// MoveToLineEnd moves the cursor to the end of the current line.
func (r *RuneBuffer) MoveToLineEnd() {
for i := r.idx; i < len(r.buf)-1; i++ {
if r.buf[i] == '\n' {
r.idx = i
return
}
}
r.idx = len(r.buf)
}
// Backspace deletes the rune left of the cursor.
func (r *RuneBuffer) Backspace() {
if r.idx == 0 {
return
}
r.idx--
r.buf = append(r.buf[:r.idx], r.buf[r.idx+1:]...)
}
// Delete deletes the rune at the current cursor position.
func (r *RuneBuffer) Delete() {
if r.idx == len(r.buf) {
return
}
r.buf = append(r.buf[:r.idx], r.buf[r.idx+1:]...)
}
// Kill deletes all runes from the cursor until the end of the line.
func (r *RuneBuffer) Kill() {
newlineIdx := strings.IndexRune(string(r.buf[r.idx:]), '\n')
if newlineIdx < 0 {
r.buf = r.buf[:r.idx]
} else {
r.buf = append(r.buf[:r.idx], r.buf[r.idx+newlineIdx+1:]...)
}
}
func (r *RuneBuffer) heightForWidth(w int) int {
return len(r.getSplitByLine(w))
}