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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
package terminal
import (
"avt-go/types"
)
type EraseMode int
const (
EraseModeNextChars EraseMode = iota
EraseModeFromCursorToEndOfView
EraseModeFromStartOfViewToCursor
EraseModeWholeView
EraseModeFromCursorToEndOfLine
EraseModeFromStartOfLineToCursor
EraseModeWholeLine
)
type EraseModeVal struct {
Mode EraseMode
N int // used by NextChars
}
// Buffer is a scrollable screen buffer backed by a slice of Lines.
type Buffer struct {
lines []types.Line
lineCount int
Cols int
Rows int
scrollbackLimit *int
trimNeeded bool
}
func NewBuffer(cols, rows int, scrollbackLimit *int, pen *types.Pen) *Buffer {
defaultPen := types.DefaultPen()
if pen == nil {
pen = &defaultPen
}
capacity := rows
if scrollbackLimit == nil {
capacity += 1000
} else if *scrollbackLimit > 0 {
capacity += *scrollbackLimit
}
buf := make([]types.Line, rows, capacity)
for i := range buf {
buf[i] = types.BlankLine(cols, *pen)
}
return &Buffer{
lines: buf,
lineCount: rows,
Cols: cols,
Rows: rows,
scrollbackLimit: scrollbackLimit,
trimNeeded: false,
}
}
func (b *Buffer) viewOffset() int { return b.lineCount - b.Rows }
func (b *Buffer) absIndex(row int) int { return b.viewOffset() + row }
func (b *Buffer) At(row int) *types.Line { return &b.lines[b.absIndex(row)] }
func (b *Buffer) Text() []string {
var result []string
var current string
for i := 0; i < b.lineCount; i++ {
line := &b.lines[i]
current += line.Text()
if !line.Wrapped {
result = append(result, trimRight(current))
current = ""
}
}
if current != "" {
result = append(result, trimRight(current))
}
return result
}
func trimRight(s string) string {
end := len(s)
for end > 0 && (s[end-1] == ' ' || s[end-1] == '\t') {
end--
}
return s[:end]
}
func (b *Buffer) Print(col, row int, ch rune, pen types.Pen) (int, bool) {
return b.At(row).PrintAt(col, ch, pen)
}
func (b *Buffer) Wrap(row int) { b.At(row).Wrapped = true }
func (b *Buffer) ShiftRight(col, row, n int, pen types.Pen) {
if n > b.Cols-col {
n = b.Cols - col
}
b.At(row).ShiftRight(col, n, pen)
}
func (b *Buffer) Delete(col, row, n int, pen *types.Pen) {
if n > b.Cols-col {
n = b.Cols - col
}
line := b.At(row)
line.DeleteAt(col, n, pen)
line.Wrapped = false
}
func (b *Buffer) Erase(col, row int, mode EraseModeVal, pen *types.Pen) {
switch mode.Mode {
case EraseModeNextChars:
n := mode.N
if n > b.Cols-col {
n = b.Cols - col
}
end := col + n
clearWrap := end == b.Cols
line := b.At(row)
line.Clear(col, end, pen)
if clearWrap {
line.Wrapped = false
}
case EraseModeFromCursorToEndOfView:
line := b.At(row)
line.Wrapped = false
line.Clear(col, b.Cols, pen)
b.clearRows(row+1, b.Rows, pen)
case EraseModeFromStartOfViewToCursor:
end := col + 1
if end > b.Cols {
end = b.Cols
}
b.At(row).Clear(0, end, pen)
b.clearRows(0, row, pen)
case EraseModeWholeView:
b.clearRows(0, b.Rows, pen)
case EraseModeFromCursorToEndOfLine:
line := b.At(row)
line.Clear(col, b.Cols, pen)
line.Wrapped = false
case EraseModeFromStartOfLineToCursor:
end := col + 1
if end > b.Cols {
end = b.Cols
}
b.At(row).Clear(0, end, pen)
case EraseModeWholeLine:
line := b.At(row)
line.Clear(0, b.Cols, pen)
line.Wrapped = false
}
}
func (b *Buffer) clearRows(start, end int, pen *types.Pen) {
for row := start; row < end; row++ {
b.At(row).Reset(b.Cols, *pen)
}
}
func (b *Buffer) ScrollUp(rangeStart, rangeEnd, n int, pen *types.Pen) {
if n > rangeEnd-rangeStart {
n = rangeEnd - rangeStart
}
if rangeEnd-1 < b.Rows-1 {
b.At(rangeEnd - 1).Wrapped = false
}
if rangeStart == 0 {
if rangeEnd == b.Rows {
b.extend(n, b.Cols, pen)
} else {
blank := types.BlankLine(b.Cols, *pen)
insertAt := b.viewOffset() + rangeEnd
for i := 0; i < n; i++ {
b.insertLine(insertAt, blank)
}
}
} else {
b.At(rangeStart - 1).Wrapped = false
rotateLines(b.lines[b.absIndex(rangeStart):b.absIndex(rangeEnd)], n)
b.clearRows(rangeEnd-n, rangeEnd, pen)
}
b.trimNeeded = true
}
func (b *Buffer) ScrollDown(rangeStart, rangeEnd, n int, pen *types.Pen) {
if n > rangeEnd-rangeStart {
n = rangeEnd - rangeStart
}
rotateLines(b.lines[b.absIndex(rangeStart):b.absIndex(rangeEnd)], -n)
b.clearRows(rangeStart, rangeStart+n, pen)
if rangeStart > 0 {
b.At(rangeStart - 1).Wrapped = false
}
b.At(rangeEnd - 1).Wrapped = false
}
func rotateLines(s []types.Line, n int) {
if len(s) == 0 {
return
}
if n > 0 {
n = n % len(s)
reverseLines(s)
reverseLines(s[:len(s)-n])
reverseLines(s[len(s)-n:])
} else if n < 0 {
n = (-n) % len(s)
reverseLines(s)
reverseLines(s[:n])
reverseLines(s[n:])
}
}
func reverseLines(s []types.Line) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
func (b *Buffer) insertLine(at int, line types.Line) {
b.lines = append(b.lines, types.Line{})
copy(b.lines[at+1:], b.lines[at:])
b.lines[at] = line
b.lineCount++
}
func (b *Buffer) extend(n, cols int, pen *types.Pen) {
blank := types.BlankLine(cols, *pen)
for i := 0; i < n; i++ {
b.lines = append(b.lines, blank)
}
b.lineCount += n
}
func (b *Buffer) Resize(newCols, newRows int, cursorCol, cursorRow int) (int, int) {
oldCols := b.Cols
oldRows := b.Rows
cursorLogCol, cursorLogRow := b.logicalPosition(cursorCol, cursorRow, oldCols, oldRows)
if newCols != oldCols {
b.lines = reflow(b.lines[:b.lineCount], newCols)
b.lineCount = len(b.lines)
if b.lineCount < oldRows {
pen := types.DefaultPen()
b.extend(oldRows-b.lineCount, newCols, &pen)
}
relCol, relRow := b.relativePosition(cursorLogCol, cursorLogRow, newCols, oldRows)
cursorCol = relCol
if relRow >= 0 {
cursorRow = relRow
} else {
cursorRow = 0
oldRows += -relRow
}
}
switch {
case newRows < oldRows:
heightDelta := oldRows - newRows
invertedCursorRow := oldRows - 1 - cursorRow
excess := heightDelta
if invertedCursorRow < excess {
excess = invertedCursorRow
}
if excess > 0 {
b.lineCount -= excess
b.lines[b.lineCount-1].Wrapped = false
}
cursorRow -= heightDelta - excess
case newRows > oldRows:
heightDelta := newRows - oldRows
scrollbackSize := b.lineCount - oldRows
if scrollbackSize < 0 {
scrollbackSize = 0
}
cursorRowShift := scrollbackSize
if heightDelta < cursorRowShift {
cursorRowShift = heightDelta
}
heightDelta -= cursorRowShift
if cursorRow < oldRows {
cursorRow += cursorRowShift
}
if heightDelta > 0 {
pen := types.DefaultPen()
b.extend(heightDelta, newCols, &pen)
}
}
b.Cols = newCols
b.Rows = newRows
b.trimNeeded = true
return cursorCol, cursorRow
}
func (b *Buffer) logicalPosition(col, row, cols, rows int) (int, int) {
visRowOffset := b.lineCount - rows
logColOffset := 0
absRow := row + visRowOffset
lastAvail := absRow
if lastAvail > b.lineCount {
lastAvail = b.lineCount
}
logRow := absRow - lastAvail
for i := 0; i < absRow && i < b.lineCount; i++ {
if b.lines[i].Wrapped {
logColOffset += cols
} else {
logColOffset = 0
logRow++
}
}
return col + logColOffset, logRow
}
func (b *Buffer) relativePosition(logCol, logRow, cols, rows int) (int, int) {
relCol := logCol
relRow := 0
r := 0
lastRow := b.lineCount - 1
for r < logRow && relRow < lastRow {
if !b.lines[relRow].Wrapped {
r++
}
relRow++
}
for relCol >= cols && relRow < b.lineCount && b.lines[relRow].Wrapped {
relCol -= cols
relRow++
}
if relCol >= cols {
relCol = cols - 1
}
relRowOffset := b.lineCount - rows
return relCol, relRow - relRowOffset
}
func (b *Buffer) View() []*types.Line {
offset := b.viewOffset()
result := make([]*types.Line, b.Rows)
for i := 0; i < b.Rows; i++ {
result[i] = &b.lines[offset+i]
}
return result
}
func (b *Buffer) Lines() []*types.Line {
result := make([]*types.Line, b.lineCount)
for i := 0; i < b.lineCount; i++ {
result[i] = &b.lines[i]
}
return result
}
func (b *Buffer) GC() []types.Line {
if !b.trimNeeded {
return nil
}
b.trimNeeded = false
return b.trimScrollback()
}
func (b *Buffer) trimScrollback() []types.Line {
if b.scrollbackLimit == nil {
return nil
}
limit := *b.scrollbackLimit
scrollbackSize := b.lineCount - b.Rows
if scrollbackSize > limit {
excess := scrollbackSize - limit
evicted := make([]types.Line, excess)
copy(evicted, b.lines[:excess])
b.lines = b.lines[excess:]
b.lineCount -= excess
return evicted
}
return nil
}
func reflow(lines []types.Line, cols int) []types.Line {
var result []types.Line
var rest *types.Line
i := 0
getNext := func() *types.Line {
if rest != nil {
r := rest
rest = nil
return r
}
if i >= len(lines) {
return nil
}
l := lines[i]
i++
return &l
}
for {
line := getNext()
if line == nil {
break
}
switch {
case cols < line.Len():
r := line.Contract(cols)
result = append(result, *line)
rest = r
case cols == line.Len():
result = append(result, *line)
default:
next := getNext()
if next == nil {
line.Expand(cols, types.DefaultPen())
line.Wrapped = false
result = append(result, *line)
} else {
done, r := line.Extend(*next, cols)
rest = r
if done {
result = append(result, *line)
} else {
rest = line
}
}
}
}
if rest != nil {
rest.Expand(cols, types.DefaultPen())
rest.Wrapped = false
result = append(result, *rest)
}
return result
}
|