-
Notifications
You must be signed in to change notification settings - Fork 18
/
viewbuffer.go
251 lines (231 loc) · 5.81 KB
/
viewbuffer.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package slit
import (
"context"
"github.com/tigrawap/slit/filters"
"github.com/tigrawap/slit/logging"
"io"
"sync"
)
type viewBuffer struct {
fetcher *Fetcher
lock sync.RWMutex // buffer updated in big chunks from Fetcher with rolling back-forward copies
buffer []Line
pos int // zero position in buffer
resetPos Pos // Zero Line to start displaying from if buffer is empty
window int // height of window, buffer size calculated in multiplies of window
eofReached bool
originalPos Pos
}
func (b *viewBuffer) getLine(offset int) (Line, error) {
if b.pos+offset >= len(b.buffer) && !b.eofReached {
b.fill()
}
if b.pos+offset >= len(b.buffer) || len(b.buffer) == 0 {
b.eofReached = true
return Line{}, io.EOF
//return ansi.NewAstring([]byte{}), io.EOF
}
return b.buffer[b.pos+offset], nil // TODO: What happens if we reached the end? panic!
}
type fillResult struct {
lastLineChanged bool
newLines int
}
func (b *viewBuffer) fill() fillResult {
b.lock.Lock()
defer b.lock.Unlock()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fillFrom := b.resetPos
if len(b.buffer) > 0 {
fillFrom = b.buffer[len(b.buffer)-1].Pos //Will start getting from next Line, not from current
}
dataChan := b.fetcher.Get(ctx, fillFrom)
result := fillResult{}
for data := range dataChan {
if len(b.buffer) > 0 {
curPos := len(b.buffer) - 1
if b.buffer[curPos].Offset == data.Offset {
// Same line as current
if len(b.buffer[curPos].Str.Runes) != len(data.Str.Runes) {
result.lastLineChanged = true
b.buffer[curPos] = data // Line changed, replacing
}
continue
}
}
b.buffer = append(b.buffer, data) // will shrink it later(make async as well?), first let's fill the window
result.newLines++
if len(b.buffer[b.pos:]) >= b.window*3 {
break
}
}
tail := len(b.buffer[:b.pos])
if tail > b.window*3 { // tail is too long, trim it
b.eofReached = false
cutFrom := tail - b.window*3
b.buffer = b.buffer[cutFrom:]
b.pos -= cutFrom
}
return result
}
func (b *viewBuffer) backFill() {
b.lock.Lock()
defer b.lock.Unlock()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var prevLine Pos
if len(b.buffer) != 0 {
logging.Debug("Using previous Line using buffer", b.buffer[0].Pos)
prevLine = b.buffer[0].Pos
} else {
logging.Debug("Going with resetPos set on reset", b.resetPos)
prevLine = b.resetPos
}
logging.Debug("Trying to backfill from", prevLine)
if prevLine.Offset < 0 {
return // Nothing to backfill
}
dataChan := b.fetcher.GetBack(ctx, prevLine)
newData := make([]Line, 0, b.window*3)
for data := range dataChan {
if len(b.buffer) != 0 && data.Offset == prevLine.Offset {
continue // reached original line
}
newData = append(newData, data)
if len(newData) >= b.window*3 {
break
}
}
if len(newData) == 0 {
return //nothing to do here
}
//oldData := b.buffer[0:]
if len(b.buffer) > b.window*2 {
b.buffer = b.buffer[0 : b.window*2] // shrinking forward-buffer
}
oldDataLen := len(b.buffer)
b.buffer = append(b.buffer, newData...) // Ensuring that got enough space, expanding if needed
copy(b.buffer[len(newData):], b.buffer[:oldDataLen]) // Shifting to make space on the left
for i := len(newData) - 1; i >= 0; i-- {
b.buffer[len(newData)-1-i] = newData[i] // Inserting values in reverse order
}
b.pos += len(newData)
}
func (b *viewBuffer) shift(direction int) {
logging.Debug("direction", direction)
defer func() {
b.originalPos = b.currentLine().Pos
}()
if direction < 0 {
logging.Debug("targeting ", b.pos+direction)
if b.pos+direction < 0 {
b.backFill()
}
b.pos = b.pos + direction
if b.pos < 0 {
b.pos = 0
}
return
}
//b.Line = b.Line + direction
//if len(b.buffer)
downShift := func() bool {
if b.pos+direction < len(b.buffer)-1 {
b.pos = b.pos + direction
return true
}
return false
}
if downShift() {
return
}
b.fill()
if downShift() || len(b.buffer) == 0 {
return
}
b.pos = len(b.buffer) - 1
}
func (b *viewBuffer) searchForward(searchFunc filters.SearchFunc) int {
for i, line := range b.buffer[b.pos:] {
if i == 0 {
// TODO: Maintain search index?( to navigate inside string)
continue
}
if searchFunc(line.Str.Runes) != nil {
return i
}
}
return -1
}
func (b *viewBuffer) searchBack(searchFunc filters.SearchFunc) int {
prevLines := b.buffer[:b.pos]
for i := 1; i <= len(prevLines); i++ {
if searchFunc(prevLines[len(prevLines)-i].Str.Runes) != nil {
return i
}
}
return -1
}
func (b *viewBuffer) searchForwardHighlighted() int {
for i, line := range b.buffer[b.pos:] {
if i == 0 {
// TODO: Maintain search index?( to navigate inside string)
continue
}
if line.Highlighted {
return i
}
}
return -1
}
func (b *viewBuffer) searchBackHighlighted() int {
prevLines := b.buffer[:b.pos]
for i := 1; i <= len(prevLines); i++ {
if prevLines[len(prevLines)-i].Highlighted {
return i
}
}
return -1
}
func (b *viewBuffer) lastLine() Line {
lastLine := len(b.buffer) - 1
if lastLine != -1 {
return b.buffer[lastLine]
}
return Line{}
}
func (b *viewBuffer) currentLine() Line {
if len(b.buffer) == 0 {
return Line{}
}
return b.buffer[b.pos]
}
func (b *viewBuffer) reset(toLine Pos) {
b.lock.Lock()
defer b.lock.Unlock()
b.buffer = b.buffer[:0]
b.pos = 0
b.resetPos = toLine
b.originalPos = toLine
b.eofReached = false
}
func (b *viewBuffer) refresh() {
b.reset(b.currentLine().Pos)
}
func (b *viewBuffer) isFull() bool {
b.lock.RLock()
defer b.lock.RUnlock()
return len(b.buffer)-b.pos >= b.window
}
func (b *viewBuffer) shiftToEnd() {
b.lock.Lock()
defer b.lock.Unlock()
if len(b.buffer)-b.pos < b.window {
return
}
b.pos = len(b.buffer) - b.window
}
func (b *viewBuffer) toggleCurrentHighlight() {
b.buffer[b.pos].Highlighted = !b.buffer[b.pos].Highlighted
}