-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferline.go
More file actions
493 lines (456 loc) · 14.1 KB
/
bufferline.go
File metadata and controls
493 lines (456 loc) · 14.1 KB
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package xterm
// Ported from xterm.js src/common/buffer/BufferLine.ts.
// Each cell occupies 3 uint32 slots: content, fg, bg.
const (
cellSize = 3
cellContent = 0
cellFg = 1
cellBg = 2
// cleanupThreshold controls when shrink triggers memory cleanup.
cleanupThreshold = 2
)
// BufferLine stores a single terminal line as a flat []uint32 with 3 values per cell.
type BufferLine struct {
data []uint32
combined map[int]string
extendedAttrs map[int]*ExtendedAttrs
Len int
IsWrapped bool
}
// NewBufferLine creates a BufferLine with cols cells, filled with fillCell.
// If fillCell is nil, cells are filled with null cell defaults.
func NewBufferLine(cols int, fillCell *CellData, isWrapped bool) *BufferLine {
bl := &BufferLine{
data: make([]uint32, cols*cellSize),
combined: make(map[int]string),
extendedAttrs: make(map[int]*ExtendedAttrs),
Len: cols,
IsWrapped: isWrapped,
}
if fillCell == nil {
fillCell = CellDataFromCharData(NewCharData(0, NullCellChar, NullCellWidth, NullCellCode))
}
for i := range cols {
bl.SetCell(i, fillCell)
}
return bl
}
// --- Primitive getters ---
// GetWidth returns the display width of the cell at index.
func (bl *BufferLine) GetWidth(index int) int {
return int(bl.data[index*cellSize+cellContent] >> ContentWidthShift)
}
// HasWidth returns non-zero if the cell at index has a width set.
func (bl *BufferLine) HasWidth(index int) uint32 {
return bl.data[index*cellSize+cellContent] & ContentWidthMask
}
// GetFg returns the fg attribute of the cell at index.
func (bl *BufferLine) GetFg(index int) uint32 {
return bl.data[index*cellSize+cellFg]
}
// GetBg returns the bg attribute of the cell at index.
func (bl *BufferLine) GetBg(index int) uint32 {
return bl.data[index*cellSize+cellBg]
}
// HasContent returns non-zero if the cell at index has content.
func (bl *BufferLine) HasContent(index int) uint32 {
return bl.data[index*cellSize+cellContent] & ContentHasContentMask
}
// GetCodePoint returns the codepoint of the cell at index.
// For combined cells, returns the last char code of the combined string.
func (bl *BufferLine) GetCodePoint(index int) uint32 {
content := bl.data[index*cellSize+cellContent]
if content&ContentIsCombinedMask != 0 {
s := bl.combined[index]
if len(s) == 0 {
return 0
}
// Return last byte as charCode (matching xterm.js charCodeAt behavior for BMP)
runes := []rune(s)
return uint32(runes[len(runes)-1])
}
return content & ContentCodepointMask
}
// IsCombined returns non-zero if the cell at index has combined content.
func (bl *BufferLine) IsCombined(index int) uint32 {
return bl.data[index*cellSize+cellContent] & ContentIsCombinedMask
}
// GetString returns the string content of the cell at index.
func (bl *BufferLine) GetString(index int) string {
content := bl.data[index*cellSize+cellContent]
if content&ContentIsCombinedMask != 0 {
return bl.combined[index]
}
cp := content & ContentCodepointMask
if cp != 0 {
return string(rune(cp))
}
return ""
}
// IsProtected returns non-zero if the cell at index has the PROTECTED flag.
func (bl *BufferLine) IsProtected(index int) uint32 {
return bl.data[index*cellSize+cellBg] & BgFlagProtected
}
// --- Get/Set (legacy CharData) ---
// Get returns the cell at index as a legacy CharData tuple.
func (bl *BufferLine) Get(index int) CharData {
content := bl.data[index*cellSize+cellContent]
cp := content & ContentCodepointMask
var ch string
if content&ContentIsCombinedMask != 0 {
ch = bl.combined[index]
} else if cp != 0 {
ch = string(rune(cp))
}
var code uint32
if content&ContentIsCombinedMask != 0 {
runes := []rune(bl.combined[index])
if len(runes) > 0 {
code = uint32(runes[len(runes)-1])
}
} else {
code = cp
}
return NewCharData(
bl.data[index*cellSize+cellFg],
ch,
int(content>>ContentWidthShift),
code,
)
}
// Set sets the cell at index from a legacy CharData tuple.
func (bl *BufferLine) Set(index int, value CharData) {
bl.data[index*cellSize+cellFg] = CharDataAttr(value)
ch := CharDataChar(value)
width := CharDataWidth(value)
runes := []rune(ch)
if len(runes) > 1 {
bl.combined[index] = ch
bl.data[index*cellSize+cellContent] = ContentIsCombinedMask | (uint32(width) << ContentWidthShift)
} else if len(runes) == 1 {
bl.data[index*cellSize+cellContent] = uint32(runes[0]) | (uint32(width) << ContentWidthShift)
} else {
bl.data[index*cellSize+cellContent] = uint32(width) << ContentWidthShift
}
}
// --- Cell-level operations ---
// LoadCell loads the cell at index into the provided CellData, returning it.
func (bl *BufferLine) LoadCell(index int, cell *CellData) *CellData {
si := index * cellSize
cell.Content = bl.data[si+cellContent]
cell.Fg = bl.data[si+cellFg]
cell.Bg = bl.data[si+cellBg]
if cell.Content&ContentIsCombinedMask != 0 {
cell.CombinedData = bl.combined[index]
}
if cell.Bg&BgFlagHasExtended != 0 {
cell.Extended = bl.extendedAttrs[index]
}
return cell
}
// SetCell sets the cell at index from a CellData.
func (bl *BufferLine) SetCell(index int, cell *CellData) {
if cell.Content&ContentIsCombinedMask != 0 {
bl.combined[index] = cell.CombinedData
}
if cell.Bg&BgFlagHasExtended != 0 {
bl.extendedAttrs[index] = cell.Extended
}
si := index * cellSize
bl.data[si+cellContent] = cell.Content
bl.data[si+cellFg] = cell.Fg
bl.data[si+cellBg] = cell.Bg
}
// SetCellFromCodepoint sets a cell from a codepoint, width, and attribute data.
func (bl *BufferLine) SetCellFromCodepoint(index int, codePoint uint32, width int, attrs *AttributeData) {
if attrs.Bg&BgFlagHasExtended != 0 {
bl.extendedAttrs[index] = attrs.Extended
}
si := index * cellSize
bl.data[si+cellContent] = codePoint | (uint32(width) << ContentWidthShift)
bl.data[si+cellFg] = attrs.Fg
bl.data[si+cellBg] = attrs.Bg
}
// AddCodepointToCell adds a combining codepoint to the cell at index.
func (bl *BufferLine) AddCodepointToCell(index int, codePoint uint32, width int) {
content := bl.data[index*cellSize+cellContent]
if content&ContentIsCombinedMask != 0 {
bl.combined[index] += string(rune(codePoint))
} else {
cp := content & ContentCodepointMask
if cp != 0 {
bl.combined[index] = string(rune(cp)) + string(rune(codePoint))
content &= ^ContentCodepointMask
content |= ContentIsCombinedMask
} else {
content = codePoint | (1 << ContentWidthShift)
}
}
if width > 0 {
content &= ^ContentWidthMask
content |= uint32(width) << ContentWidthShift
}
bl.data[index*cellSize+cellContent] = content
}
// --- Bulk operations ---
// InsertCells inserts n cells at pos, shifting existing cells right.
// Cells that fall off the end are lost. New cells are filled with fillCell.
func (bl *BufferLine) InsertCells(pos, n int, fillCell *CellData) {
pos %= bl.Len
// Handle fullwidth at pos: reset cell to the left if pos is second cell of a wide char
if pos > 0 && bl.GetWidth(pos-1) == 2 {
bl.SetCellFromCodepoint(pos-1, 0, 1, &fillCell.AttributeData)
}
if n < bl.Len-pos {
var tmp CellData
for i := bl.Len - pos - n - 1; i >= 0; i-- {
bl.SetCell(pos+n+i, bl.LoadCell(pos+i, &tmp))
}
for i := range n {
bl.SetCell(pos+i, fillCell)
}
} else {
for i := pos; i < bl.Len; i++ {
bl.SetCell(i, fillCell)
}
}
// Handle fullwidth at line end
if bl.GetWidth(bl.Len-1) == 2 {
bl.SetCellFromCodepoint(bl.Len-1, 0, 1, &fillCell.AttributeData)
}
}
// DeleteCells deletes n cells at pos, shifting remaining cells left.
// Vacated cells at the end are filled with fillCell.
func (bl *BufferLine) DeleteCells(pos, n int, fillCell *CellData) {
pos %= bl.Len
if n < bl.Len-pos {
var tmp CellData
for i := range bl.Len - pos - n {
bl.SetCell(pos+i, bl.LoadCell(pos+n+i, &tmp))
}
for i := bl.Len - n; i < bl.Len; i++ {
bl.SetCell(i, fillCell)
}
} else {
for i := pos; i < bl.Len; i++ {
bl.SetCell(i, fillCell)
}
}
// Handle fullwidth at pos
if pos > 0 && bl.GetWidth(pos-1) == 2 {
bl.SetCellFromCodepoint(pos-1, 0, 1, &fillCell.AttributeData)
}
if bl.GetWidth(pos) == 0 && bl.HasContent(pos) == 0 {
bl.SetCellFromCodepoint(pos, 0, 1, &fillCell.AttributeData)
}
}
// ReplaceCells replaces cells in [start, end) with fillCell.
func (bl *BufferLine) ReplaceCells(start, end int, fillCell *CellData, respectProtect bool) {
if respectProtect {
if start > 0 && bl.GetWidth(start-1) == 2 && bl.IsProtected(start-1) == 0 {
bl.SetCellFromCodepoint(start-1, 0, 1, &fillCell.AttributeData)
}
if end < bl.Len && bl.GetWidth(end-1) == 2 && bl.IsProtected(end) == 0 {
bl.SetCellFromCodepoint(end, 0, 1, &fillCell.AttributeData)
}
for start < end && start < bl.Len {
if bl.IsProtected(start) == 0 {
bl.SetCell(start, fillCell)
}
start++
}
return
}
// Handle fullwidth at start
if start > 0 && bl.GetWidth(start-1) == 2 {
bl.SetCellFromCodepoint(start-1, 0, 1, &fillCell.AttributeData)
}
// Handle fullwidth at end
if end < bl.Len && bl.GetWidth(end-1) == 2 {
bl.SetCellFromCodepoint(end, 0, 1, &fillCell.AttributeData)
}
for start < end && start < bl.Len {
bl.SetCell(start, fillCell)
start++
}
}
// Resize resizes the line to cols cells, filling new cells with fillCell.
// Returns true if a CleanupMemory call would free excess memory.
func (bl *BufferLine) Resize(cols int, fillCell *CellData) bool {
if cols == bl.Len {
return len(bl.data)*4*cleanupThreshold < cap(bl.data)*4
}
uint32Cells := cols * cellSize
if cols > bl.Len {
if cap(bl.data) >= uint32Cells {
bl.data = bl.data[:uint32Cells]
} else {
newData := make([]uint32, uint32Cells)
copy(newData, bl.data)
bl.data = newData
}
for i := bl.Len; i < cols; i++ {
bl.SetCell(i, fillCell)
}
} else {
bl.data = bl.data[:uint32Cells]
// Remove combined data and extended attrs beyond new length
for k := range bl.combined {
if k >= cols {
delete(bl.combined, k)
}
}
for k := range bl.extendedAttrs {
if k >= cols {
delete(bl.extendedAttrs, k)
}
}
}
bl.Len = cols
return uint32Cells*4*cleanupThreshold < cap(bl.data)*4
}
// CleanupMemory reallocates the backing array if it exceeds the threshold.
// Returns 1 if cleanup happened, 0 otherwise.
func (bl *BufferLine) CleanupMemory() int {
if len(bl.data)*4*cleanupThreshold < cap(bl.data)*4 {
newData := make([]uint32, len(bl.data))
copy(newData, bl.data)
bl.data = newData
return 1
}
return 0
}
// Fill fills all cells with fillCell.
func (bl *BufferLine) Fill(fillCell *CellData, respectProtect bool) {
if respectProtect {
for i := range bl.Len {
if bl.IsProtected(i) == 0 {
bl.SetCell(i, fillCell)
}
}
return
}
bl.combined = make(map[int]string)
bl.extendedAttrs = make(map[int]*ExtendedAttrs)
for i := range bl.Len {
bl.SetCell(i, fillCell)
}
}
// CopyFrom copies all data from another BufferLine.
func (bl *BufferLine) CopyFrom(line *BufferLine) {
if bl.Len != line.Len {
bl.data = make([]uint32, len(line.data))
}
copy(bl.data, line.data)
bl.Len = line.Len
bl.combined = make(map[int]string, len(line.combined))
for k, v := range line.combined {
bl.combined[k] = v
}
bl.extendedAttrs = make(map[int]*ExtendedAttrs, len(line.extendedAttrs))
for k, v := range line.extendedAttrs {
bl.extendedAttrs[k] = v
}
bl.IsWrapped = line.IsWrapped
}
// Clone returns a deep copy of the BufferLine.
func (bl *BufferLine) Clone() *BufferLine {
newLine := &BufferLine{
data: make([]uint32, len(bl.data)),
combined: make(map[int]string, len(bl.combined)),
extendedAttrs: make(map[int]*ExtendedAttrs, len(bl.extendedAttrs)),
Len: bl.Len,
IsWrapped: bl.IsWrapped,
}
copy(newLine.data, bl.data)
for k, v := range bl.combined {
newLine.combined[k] = v
}
for k, v := range bl.extendedAttrs {
newLine.extendedAttrs[k] = v
}
return newLine
}
// --- Query ---
// GetTrimmedLength returns the number of columns with content, accounting for wide chars.
func (bl *BufferLine) GetTrimmedLength() int {
for i := bl.Len - 1; i >= 0; i-- {
if bl.data[i*cellSize+cellContent]&ContentHasContentMask != 0 {
return i + int(bl.data[i*cellSize+cellContent]>>ContentWidthShift)
}
}
return 0
}
// GetNoBgTrimmedLength returns the trimmed length considering both content and bg color.
func (bl *BufferLine) GetNoBgTrimmedLength() int {
for i := bl.Len - 1; i >= 0; i-- {
if bl.data[i*cellSize+cellContent]&ContentHasContentMask != 0 ||
bl.data[i*cellSize+cellBg]&AttrCMMask != 0 {
return i + int(bl.data[i*cellSize+cellContent]>>ContentWidthShift)
}
}
return 0
}
// CopyCellsFrom copies length cells from src starting at srcCol to bl starting at destCol.
func (bl *BufferLine) CopyCellsFrom(src *BufferLine, srcCol, destCol, length int, applyInReverse bool) {
srcData := src.data
if applyInReverse {
for cell := length - 1; cell >= 0; cell-- {
for i := range cellSize {
bl.data[(destCol+cell)*cellSize+i] = srcData[(srcCol+cell)*cellSize+i]
}
if srcData[(srcCol+cell)*cellSize+cellBg]&BgFlagHasExtended != 0 {
bl.extendedAttrs[destCol+cell] = src.extendedAttrs[srcCol+cell]
}
}
} else {
for cell := range length {
for i := range cellSize {
bl.data[(destCol+cell)*cellSize+i] = srcData[(srcCol+cell)*cellSize+i]
}
if srcData[(srcCol+cell)*cellSize+cellBg]&BgFlagHasExtended != 0 {
bl.extendedAttrs[destCol+cell] = src.extendedAttrs[srcCol+cell]
}
}
}
// Copy combined data
for k, v := range src.combined {
if k >= srcCol {
bl.combined[k-srcCol+destCol] = v
}
}
}
// TranslateToString converts the line to a string.
// If trimRight is true, trailing empty cells are excluded.
// startCol and endCol define the range (endCol is exclusive, -1 means bl.Len).
func (bl *BufferLine) TranslateToString(trimRight bool, startCol, endCol int) string {
if endCol < 0 {
endCol = bl.Len
}
if trimRight {
tl := bl.GetTrimmedLength()
if tl < endCol {
endCol = tl
}
}
result := make([]byte, 0, endCol-startCol)
for startCol < endCol {
content := bl.data[startCol*cellSize+cellContent]
cp := content & ContentCodepointMask
var chars string
if content&ContentIsCombinedMask != 0 {
chars = bl.combined[startCol]
} else if cp != 0 {
chars = string(rune(cp))
} else {
chars = WhitespaceCellChar
}
result = append(result, chars...)
w := int(content >> ContentWidthShift)
if w == 0 {
w = 1
}
startCol += w
}
return string(result)
}