-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinebuffer.go
230 lines (173 loc) · 4.47 KB
/
linebuffer.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"fmt"
"strings"
"github.com/gdamore/tcell/v2"
)
type LineBuffer struct {
gapSize int
gapStart int
gapEnd int
buffer []rune
screen *Screen
cursor *Cursor
}
func (lb *LineBuffer) String() string {
return fmt.Sprintf("Gs: %v, Ge: %v, len: %v", lb.gapStart, lb.gapEnd, lb.GetText())
}
func NewLineBuffer(s string, gapSize int, screen *Screen, cursor *Cursor) *LineBuffer {
//TODO make sure this accepts an array of runes instead of a string
runes := []rune(s)
buffer := make([]rune, gapSize+len(runes))
copy(buffer, runes)
gapStart := len(runes)
gapEnd := (gapStart + gapSize) - 1
lb := &LineBuffer{
gapStart: gapStart,
gapSize: gapSize,
gapEnd: gapEnd,
buffer: buffer,
screen: screen,
cursor: cursor,
}
// lb.Write(s)
return lb
}
// redraws the line on the screen at the specified position
func (lb *LineBuffer) ReDraw(x, y int) {
//buffer : [1, 2, 0, 0, 0] str: "12"
//specified position is 1
// from 1 to the width of the screen
str := lb.GetText()
width, _ := lb.screen.tScreen.Size()
for ; x < width; x++ {
if x >= len(str) {
lb.screen.tScreen.SetContent(x, y, ' ', nil, tcell.StyleDefault)
} else {
lb.screen.tScreen.SetContent(x, y, rune(str[x]), nil, tcell.StyleDefault)
}
}
lb.screen.tScreen.Show()
}
func (lb *LineBuffer) GetGapSize() int {
return (lb.gapEnd - lb.gapStart) + 1
}
func (lb *LineBuffer) GetText() string {
var sb strings.Builder
sb.Grow(len(lb.buffer) - lb.GetGapSize())
sb.WriteString(string(lb.buffer[:lb.gapStart]))
sb.WriteString(string(lb.buffer[lb.gapEnd+1:]))
return sb.String()
}
func (lb *LineBuffer)GetBufferWithoutGap() []rune{
first := lb.buffer[:lb.gapStart]
second := lb.buffer[lb.gapEnd+1:]
return append(first, second...)
}
func (lb *LineBuffer) GetRunes() string {
first := lb.buffer[:lb.gapStart]
second := lb.buffer[lb.gapEnd+1:]
return string(append(first, second...))
}
func (lb *LineBuffer) Write(s string) {
for _, r := range s {
lb.Insert(r)
}
// lb.screen.WriteDebug("Done writing string")
}
// adds a rune to the buffer, without changing the cursor
func (lb *LineBuffer) Add(r rune) {
if lb.GetGapSize() <= 1 {
lb.Grow()
}
lb.buffer[lb.gapStart] = r
lb.gapStart++
}
func (lb *LineBuffer) AddString(s string) {
for _, r := range s {
lb.Add(rune(r))
}
}
func (lb *LineBuffer) Insert(r rune) {
if lb.GetGapSize() <= 1 {
lb.Grow()
}
lb.GoTo(lb.cursor.x)
lb.buffer[lb.gapStart] = r
// x := lb.gapStart
lb.cursor.SetPos(lb.cursor.x+1, lb.cursor.y, lb.screen.tabBuffer)
lb.gapStart++
lb.screen.tScreen.Show()
}
func (lb *LineBuffer) Delete() {
cursor := lb.cursor
cursorX, cursorY := cursor.GetCursorPos()
if cursorX <= 0 && cursorY <=0 {
return
}
if cursorX <= 0 {
// delete the newline character
lb.screen.tabBuffer.DeleteLine(cursorY)
return
}
lb.GoTo(cursorX)
lb.buffer[lb.gapStart-1] = 0
lb.gapStart--
lb.cursor.SetPos(lb.cursor.x-1, lb.cursor.y, lb.screen.tabBuffer)
}
func (lb *LineBuffer) Grow() {
newCapacity := lb.gapSize * 2
newBuffer := make([]rune, newCapacity+len(lb.buffer))
copy(newBuffer, lb.buffer[:lb.gapStart])
newGapEnd := lb.gapStart + newCapacity
copy(newBuffer[newGapEnd:], lb.buffer[lb.gapEnd:])
lb.buffer = newBuffer
lb.gapEnd = newGapEnd
}
func (lb *LineBuffer) GoLeft() {
if lb.gapStart <= 0 {
return
}
lb.buffer[lb.gapEnd] = lb.buffer[lb.gapStart-1]
lb.gapEnd--
lb.buffer[lb.gapStart-1] = 0 // Clear the original position
lb.gapStart--
}
func (lb *LineBuffer) GoRight() {
if lb.gapEnd >= len(lb.buffer)-1 {
return
}
lb.buffer[lb.gapStart] = lb.buffer[lb.gapEnd+1]
lb.gapStart++
lb.buffer[lb.gapEnd+1] = 0 // Clear the original position
lb.gapEnd++
}
// Moves the gap to start at the specified position, so it is the same as the cursor position
func (lb *LineBuffer) GoTo(pos int) {
// lb.screen.WriteDebug(fmt.Sprintf("Old gap start: %d, pos: %d", lb.gapStart, pos), 3)
if pos < 0 || pos >= len(lb.buffer) {
return
}
if pos < lb.gapStart {
// loop till gapstart
diff := lb.gapStart - pos
for i := 0; i < diff; i++ {
lb.GoLeft()
}
} else if pos > lb.gapStart {
// loop till gapend
diff := pos - lb.gapStart
for i := 0; i < diff; i++ {
lb.GoRight()
}
}
// lb.screen.WriteDebug(fmt.Sprintf("new gap start: %d", lb.gapStart), 4)
}
func (lb *LineBuffer) GoToEnd() {
index := len(lb.buffer) - lb.GetGapSize()
lb.GoTo(index)
}
// returns the length of the buffer excluding the gap
func (lb *LineBuffer) Len() int {
return len(lb.buffer) - lb.GetGapSize()
}