forked from marcusolsson/tui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runebuf.go
160 lines (135 loc) · 3.36 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
package tui
import (
"image"
"strings"
runewidth "github.com/mattn/go-runewidth"
wordwrap "github.com/mitchellh/go-wordwrap"
)
// RuneBuffer provides readline functionality for text widgets.
type RuneBuffer struct {
buf []rune
idx int
wordwrap bool
}
// 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(width int) []string {
var text string
if r.wordwrap {
text = wordwrap.WrapString(r.String(), uint(width))
} else {
text = r.String()
}
return strings.Split(text, "\n")
}
func getSplitByLine(rs []rune, width int, wrap bool) []string {
var text string
if wrap {
text = wordwrap.WrapString(string(rs), uint(width))
} else {
text = string(rs)
}
return strings.Split(text, "\n")
}
// CursorPos returns the coordinate for the cursor for a given width.
func (r *RuneBuffer) CursorPos(width int) image.Point {
if width == 0 {
return image.ZP
}
sp := getSplitByLine(r.buf[:r.idx], width, r.wordwrap)
return image.Pt(stringWidth(sp[len(sp)-1]), len(sp)-1)
}
func (r *RuneBuffer) String() string {
return string(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+1] == '\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.SplitByLine(w))
}