-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialize_addon.go
More file actions
688 lines (602 loc) · 18 KB
/
serialize_addon.go
File metadata and controls
688 lines (602 loc) · 18 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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
package xterm
// Ported from xterm.js addons/addon-serialize/src/SerializeAddon.ts.
// Serializes terminal buffer state as escape sequences that can be replayed
// to reconstruct the terminal content.
import (
"fmt"
"strings"
)
// SerializeRange specifies a row range for serialization.
type SerializeRange struct {
Start int
End int
}
// SerializeOptions controls what gets serialized.
type SerializeOptions struct {
// Scrollback limits how many scrollback lines to include.
// If nil, all scrollback is included.
Scrollback *int
// Range serializes only the specified line range instead of the full buffer.
Range *SerializeRange
// ExcludeAltBuffer skips alternate buffer serialization.
ExcludeAltBuffer bool
// ExcludeModes skips mode and scroll region serialization.
ExcludeModes bool
}
// SerializeAddon serializes terminal buffer state as escape sequences.
type SerializeAddon struct {
terminal *Terminal
}
// NewSerializeAddon creates a new SerializeAddon attached to the given terminal.
func NewSerializeAddon(t *Terminal) *SerializeAddon {
return &SerializeAddon{terminal: t}
}
// Serialize returns the terminal buffer content as escape sequences.
func (sa *SerializeAddon) Serialize(opts *SerializeOptions) []byte {
if opts == nil {
opts = &SerializeOptions{}
}
t := sa.terminal
// Normal buffer
var content string
if opts.Range != nil {
content = sa.serializeBufferByRange(t, t.NormalBuffer(), *opts.Range, true)
} else {
content = sa.serializeBufferByScrollback(t, t.NormalBuffer(), opts.Scrollback)
}
// Alternate buffer
if !opts.ExcludeAltBuffer && t.IsAltBufferActive() {
altContent := sa.serializeBufferByScrollback(t, t.AltBuffer(), nil)
content += "\x1b[?1049h\x1b[H" + altContent
}
// Modes and scroll region
if !opts.ExcludeModes {
content += sa.serializeModes(t)
content += sa.serializeScrollRegion(t)
}
return []byte(content)
}
func (sa *SerializeAddon) serializeBufferByScrollback(t *Terminal, buffer *Buffer, scrollback *int) string {
maxRows := buffer.Lines.Length()
correctRows := maxRows
if scrollback != nil {
correctRows = constrain(*scrollback+t.Rows(), 0, maxRows)
}
return sa.serializeBufferByRange(t, buffer, SerializeRange{
Start: maxRows - correctRows,
End: maxRows - 1,
}, false)
}
func (sa *SerializeAddon) serializeBufferByRange(t *Terminal, buffer *Buffer, r SerializeRange, excludeFinalCursorPosition bool) string {
handler := newStringSerializeHandler(buffer, t)
return handler.serialize(r.Start, r.End, excludeFinalCursorPosition)
}
func (sa *SerializeAddon) serializeScrollRegion(t *Terminal) string {
scrollTop := t.ScrollTop()
scrollBottom := t.ScrollBottom()
if scrollTop != 0 || scrollBottom != t.Rows()-1 {
return fmt.Sprintf("\x1b[%d;%dr", scrollTop+1, scrollBottom+1)
}
return ""
}
func (sa *SerializeAddon) serializeModes(t *Terminal) string {
var content strings.Builder
modes := t.DecPrivateModes()
// Default: false
if modes.ApplicationCursorKeys {
content.WriteString("\x1b[?1h")
}
if modes.ApplicationKeypad {
content.WriteString("\x1b[?66h")
}
if modes.BracketedPasteMode {
content.WriteString("\x1b[?2004h")
}
if t.Modes().InsertMode {
content.WriteString("\x1b[4h")
}
if modes.Origin {
content.WriteString("\x1b[?6h")
}
if modes.ReverseWraparound {
content.WriteString("\x1b[?45h")
}
if modes.SendFocus {
content.WriteString("\x1b[?1004h")
}
// Default: true
if !modes.Wraparound {
content.WriteString("\x1b[?7l")
}
// Mouse tracking
switch modes.MouseTrackingMode {
case "X10":
content.WriteString("\x1b[?9h")
case "VT200":
content.WriteString("\x1b[?1000h")
case "DRAG":
content.WriteString("\x1b[?1002h")
case "ANY":
content.WriteString("\x1b[?1003h")
}
// Cursor visibility (DECTCEM) - default: visible
if t.IsCursorHidden() {
content.WriteString("\x1b[?25l")
}
return content.String()
}
// constrain clamps v to [min, max].
func constrain(v, min, max int) int {
if v < min {
return min
}
if v > max {
return max
}
return v
}
// --- stringSerializeHandler ---
// stringSerializeHandler serializes buffer content as escape sequences.
// Ported from xterm.js StringSerializeHandler.
type stringSerializeHandler struct {
buffer *Buffer
terminal *Terminal
cols int
allRows []string
allRowSeparators []string
currentRow strings.Builder
nullCellCount int
rowIndex int
// cursorStyle tracks the last style we emitted, so we can diff.
cursorStyle *CellData
// cursorStyleRow/Col: where the cursor style came from.
cursorStyleRow int
cursorStyleCol int
// backgroundCell: a null cell for checking whether background is empty.
backgroundCell *CellData
// Reusable cells for rowEnd wrapped-line handling.
thisRowLastChar *CellData
thisRowLastSecondChar *CellData
nextRowFirstChar *CellData
firstRow int
lastCursorRow int
lastCursorCol int
lastContentCursorRow int
lastContentCursorCol int
}
func newStringSerializeHandler(buffer *Buffer, terminal *Terminal) *stringSerializeHandler {
return &stringSerializeHandler{
buffer: buffer,
terminal: terminal,
cols: terminal.Cols(),
cursorStyle: NewCellData(),
backgroundCell: NewCellData(),
thisRowLastChar: NewCellData(),
thisRowLastSecondChar: NewCellData(),
nextRowFirstChar: NewCellData(),
}
}
func (h *stringSerializeHandler) serialize(start, end int, excludeFinalCursorPosition bool) string {
rows := end - start + 1
if rows <= 0 {
return ""
}
h.allRows = make([]string, rows)
h.allRowSeparators = make([]string, rows)
h.rowIndex = 0
h.lastContentCursorRow = start
h.lastCursorRow = start
h.lastContentCursorCol = 0
h.lastCursorCol = 0
h.firstRow = start
// We need two of them to flip between old and new cell.
cell1 := NewCellData()
cell2 := NewCellData()
oldCell := cell1
for row := start; row <= end; row++ {
// Guard against concurrent resize shrinking the buffer.
if row >= h.buffer.Lines.Length() {
h.rowEnd(row, row == end)
continue
}
line := h.buffer.Lines.Get(row)
if line == nil {
h.rowEnd(row, row == end)
continue
}
// Use the line's actual length, not the cached cols, to avoid
// index-out-of-range panics when a concurrent resize shrinks lines.
lineCols := line.Len
if lineCols > h.cols {
lineCols = h.cols
}
for col := range lineCols {
var c *CellData
if oldCell == cell1 {
c = cell2
} else {
c = cell1
}
line.LoadCell(col, c)
h.nextCell(c, oldCell, row, col)
oldCell = c
}
h.rowEnd(row, row == end)
}
return h.serializeString(excludeFinalCursorPosition)
}
func (h *stringSerializeHandler) rowEnd(row int, isLastRow bool) {
// If there are colorful empty cells at line end, pad them back
if h.nullCellCount > 0 && !equalBg(h.cursorStyle, h.backgroundCell) {
h.currentRow.WriteString(fmt.Sprintf("\x1b[%dX", h.nullCellCount))
}
rowSeparator := ""
if !isLastRow {
// Enable BCE: update background cell if we're past the initial viewport
if row-h.firstRow >= h.terminal.Rows() {
if h.cursorStyleRow < h.buffer.Lines.Length() {
bgLine := h.buffer.Lines.Get(h.cursorStyleRow)
if bgLine != nil && h.cursorStyleCol < bgLine.Len {
bgLine.LoadCell(h.cursorStyleCol, h.backgroundCell)
}
}
}
// Guard against concurrent resize shrinking the buffer.
var currentLine, nextLine *BufferLine
if row < h.buffer.Lines.Length() {
currentLine = h.buffer.Lines.Get(row)
}
if row+1 < h.buffer.Lines.Length() {
nextLine = h.buffer.Lines.Get(row + 1)
}
if currentLine == nil || nextLine == nil {
rowSeparator = "\r\n"
h.lastCursorRow = row + 1
h.lastCursorCol = 0
} else if !nextLine.IsWrapped {
rowSeparator = "\r\n"
h.lastCursorRow = row + 1
h.lastCursorCol = 0
} else {
// Wrapped line handling
rowSeparator = ""
if currentLine.Len > 0 {
currentLine.LoadCell(currentLine.Len-1, h.thisRowLastChar)
}
if currentLine.Len >= 2 {
currentLine.LoadCell(currentLine.Len-2, h.thisRowLastSecondChar)
}
if nextLine.Len > 0 {
nextLine.LoadCell(0, h.nextRowFirstChar)
}
isNextRowFirstCharDoubleWidth := h.nextRowFirstChar.GetWidth() > 1
isValid := false
if h.nextRowFirstChar.GetChars() != "" {
maxNullCount := 0
if isNextRowFirstCharDoubleWidth {
maxNullCount = 1
}
if h.nullCellCount <= maxNullCount {
if (h.thisRowLastChar.GetChars() != "" || h.thisRowLastChar.GetWidth() == 0) &&
equalBg(h.thisRowLastChar, h.nextRowFirstChar) {
isValid = true
}
if isNextRowFirstCharDoubleWidth &&
(h.thisRowLastSecondChar.GetChars() != "" || h.thisRowLastSecondChar.GetWidth() == 0) &&
equalBg(h.thisRowLastChar, h.nextRowFirstChar) &&
equalBg(h.thisRowLastSecondChar, h.nextRowFirstChar) {
isValid = true
}
}
}
if !isValid {
// Force the wrap with magic
rowSeparator = strings.Repeat("-", h.nullCellCount+1)
rowSeparator += "\x1b[1D\x1b[1X"
if h.nullCellCount > 0 {
rowSeparator += "\x1b[A"
rowSeparator += fmt.Sprintf("\x1b[%dC", currentLine.Len-h.nullCellCount)
rowSeparator += fmt.Sprintf("\x1b[%dX", h.nullCellCount)
rowSeparator += fmt.Sprintf("\x1b[%dD", currentLine.Len-h.nullCellCount)
rowSeparator += "\x1b[B"
}
h.lastContentCursorRow = row + 1
h.lastContentCursorCol = 0
h.lastCursorRow = row + 1
h.lastCursorCol = 0
}
}
}
h.allRows[h.rowIndex] = h.currentRow.String()
h.allRowSeparators[h.rowIndex] = rowSeparator
h.rowIndex++
h.currentRow.Reset()
h.nullCellCount = 0
}
func (h *stringSerializeHandler) diffStyle(cell, oldCell *CellData) []string {
var sgrSeq []string
if attributesEquals(cell, oldCell) {
return sgrSeq
}
fgChanged := !equalFg(cell, oldCell)
bgChanged := !equalBg(cell, oldCell)
flagsChanged := !equalFlags(cell, oldCell)
if cell.IsAttributeDefault() {
if !oldCell.IsAttributeDefault() {
sgrSeq = append(sgrSeq, "0")
}
} else {
if fgChanged {
color := cell.GetFgColor()
if cell.IsFgRGB() {
sgrSeq = append(sgrSeq,
"38", "2",
fmt.Sprintf("%d", (uint32(color)>>16)&0xFF),
fmt.Sprintf("%d", (uint32(color)>>8)&0xFF),
fmt.Sprintf("%d", uint32(color)&0xFF))
} else if cell.IsFgPalette() {
if color >= 16 {
sgrSeq = append(sgrSeq, "38", "5", fmt.Sprintf("%d", color))
} else {
if color&8 != 0 {
sgrSeq = append(sgrSeq, fmt.Sprintf("%d", 90+(color&7)))
} else {
sgrSeq = append(sgrSeq, fmt.Sprintf("%d", 30+(color&7)))
}
}
} else {
sgrSeq = append(sgrSeq, "39")
}
}
if bgChanged {
color := cell.GetBgColor()
if cell.IsBgRGB() {
sgrSeq = append(sgrSeq,
"48", "2",
fmt.Sprintf("%d", (uint32(color)>>16)&0xFF),
fmt.Sprintf("%d", (uint32(color)>>8)&0xFF),
fmt.Sprintf("%d", uint32(color)&0xFF))
} else if cell.IsBgPalette() {
if color >= 16 {
sgrSeq = append(sgrSeq, "48", "5", fmt.Sprintf("%d", color))
} else {
if color&8 != 0 {
sgrSeq = append(sgrSeq, fmt.Sprintf("%d", 100+(color&7)))
} else {
sgrSeq = append(sgrSeq, fmt.Sprintf("%d", 40+(color&7)))
}
}
} else {
sgrSeq = append(sgrSeq, "49")
}
}
if flagsChanged {
if cell.IsInverse() != oldCell.IsInverse() {
if cell.IsInverse() != 0 {
sgrSeq = append(sgrSeq, "7")
} else {
sgrSeq = append(sgrSeq, "27")
}
}
if cell.IsBold() != oldCell.IsBold() {
if cell.IsBold() != 0 {
sgrSeq = append(sgrSeq, "1")
} else {
sgrSeq = append(sgrSeq, "22")
}
}
if cell.IsUnderline() != oldCell.IsUnderline() {
if cell.IsUnderline() != 0 {
sgrSeq = append(sgrSeq, "4")
} else {
sgrSeq = append(sgrSeq, "24")
}
}
if cell.IsOverline() != oldCell.IsOverline() {
if cell.IsOverline() != 0 {
sgrSeq = append(sgrSeq, "53")
} else {
sgrSeq = append(sgrSeq, "55")
}
}
if cell.IsBlink() != oldCell.IsBlink() {
if cell.IsBlink() != 0 {
sgrSeq = append(sgrSeq, "5")
} else {
sgrSeq = append(sgrSeq, "25")
}
}
if cell.IsInvisible() != oldCell.IsInvisible() {
if cell.IsInvisible() != 0 {
sgrSeq = append(sgrSeq, "8")
} else {
sgrSeq = append(sgrSeq, "28")
}
}
if cell.IsItalic() != oldCell.IsItalic() {
if cell.IsItalic() != 0 {
sgrSeq = append(sgrSeq, "3")
} else {
sgrSeq = append(sgrSeq, "23")
}
}
if cell.IsDim() != oldCell.IsDim() {
if cell.IsDim() != 0 {
sgrSeq = append(sgrSeq, "2")
} else {
sgrSeq = append(sgrSeq, "22")
}
}
if cell.IsStrikethrough() != oldCell.IsStrikethrough() {
if cell.IsStrikethrough() != 0 {
sgrSeq = append(sgrSeq, "9")
} else {
sgrSeq = append(sgrSeq, "29")
}
}
}
}
return sgrSeq
}
func (h *stringSerializeHandler) nextCell(cell, oldCell *CellData, row, col int) {
// Width-0 cells are placeholders after CJK characters
if cell.GetWidth() == 0 {
return
}
isEmptyCell := cell.GetChars() == ""
sgrSeq := h.diffStyle(cell, h.cursorStyle)
// The empty cell style is only assumed to be changed when background changed,
// because foreground is always 0.
var styleChanged bool
if isEmptyCell {
styleChanged = !equalBg(h.cursorStyle, cell)
} else {
styleChanged = len(sgrSeq) > 0
}
if styleChanged {
// Before updating style, fill empty cells back
if h.nullCellCount > 0 {
// Use clear right to set background.
if !equalBg(h.cursorStyle, h.backgroundCell) {
h.currentRow.WriteString(fmt.Sprintf("\x1b[%dX", h.nullCellCount))
}
// Use move right to move cursor.
h.currentRow.WriteString(fmt.Sprintf("\x1b[%dC", h.nullCellCount))
h.nullCellCount = 0
}
h.lastContentCursorRow = row
h.lastContentCursorCol = col
h.lastCursorRow = row
h.lastCursorCol = col
h.currentRow.WriteString(fmt.Sprintf("\x1b[%sm", strings.Join(sgrSeq, ";")))
// Update the last cursor style.
if row < h.buffer.Lines.Length() {
line := h.buffer.Lines.Get(row)
if line != nil && col < line.Len {
line.LoadCell(col, h.cursorStyle)
h.cursorStyleRow = row
h.cursorStyleCol = col
}
}
}
if isEmptyCell {
h.nullCellCount += cell.GetWidth()
} else {
if h.nullCellCount > 0 {
// Use move right when background is empty, use clear right when there is background.
if equalBg(h.cursorStyle, h.backgroundCell) {
h.currentRow.WriteString(fmt.Sprintf("\x1b[%dC", h.nullCellCount))
} else {
h.currentRow.WriteString(fmt.Sprintf("\x1b[%dX", h.nullCellCount))
h.currentRow.WriteString(fmt.Sprintf("\x1b[%dC", h.nullCellCount))
}
h.nullCellCount = 0
}
h.currentRow.WriteString(cell.GetChars())
h.lastContentCursorRow = row
h.lastContentCursorCol = col + cell.GetWidth()
h.lastCursorRow = row
h.lastCursorCol = col + cell.GetWidth()
}
}
func (h *stringSerializeHandler) serializeString(excludeFinalCursorPosition bool) string {
rowEnd := len(h.allRows)
// Trim trailing empty rows when buffer fits in viewport
if h.buffer.Lines.Length()-h.firstRow <= h.terminal.Rows() {
rowEnd = h.lastContentCursorRow + 1 - h.firstRow
if rowEnd < 0 {
rowEnd = 0
}
if rowEnd > len(h.allRows) {
rowEnd = len(h.allRows)
}
h.lastCursorCol = h.lastContentCursorCol
h.lastCursorRow = h.lastContentCursorRow
}
var content strings.Builder
for i := range rowEnd {
content.WriteString(h.allRows[i])
if i+1 < rowEnd {
content.WriteString(h.allRowSeparators[i])
}
}
// Restore cursor position
if !excludeFinalCursorPosition {
realCursorRow := h.buffer.YBase + h.buffer.Y
realCursorCol := h.buffer.X
cursorMoved := realCursorRow != h.lastCursorRow || realCursorCol != h.lastCursorCol
if cursorMoved {
rowOffset := realCursorRow - h.lastCursorRow
if rowOffset > 0 {
content.WriteString(fmt.Sprintf("\x1b[%dB", rowOffset))
} else if rowOffset < 0 {
content.WriteString(fmt.Sprintf("\x1b[%dA", -rowOffset))
}
colOffset := realCursorCol - h.lastCursorCol
if colOffset > 0 {
content.WriteString(fmt.Sprintf("\x1b[%dC", colOffset))
} else if colOffset < 0 {
content.WriteString(fmt.Sprintf("\x1b[%dD", -colOffset))
}
}
}
// Restore the cursor's current style, see https://github.com/xtermjs/xterm.js/issues/3677
// HACK: Internal API access since it's awkward to expose this in the API and serialize will
// likely be the only consumer
curAttr := h.terminal.CurAttrData()
curAttrCell := NewCellData()
curAttrCell.Fg = curAttr.Fg
curAttrCell.Bg = curAttr.Bg
sgrSeq := h.diffStyle(curAttrCell, h.cursorStyle)
if len(sgrSeq) > 0 {
content.WriteString(fmt.Sprintf("\x1b[%sm", strings.Join(sgrSeq, ";")))
}
return content.String()
}
// --- Helper functions ---
// equalFg compares foreground color mode and color value (ignoring flags).
func equalFg(a, b *CellData) bool {
return a.GetFgColorMode() == b.GetFgColorMode() &&
a.GetFgColor() == b.GetFgColor()
}
// equalBg compares background color mode and color value (ignoring flags).
func equalBg(a, b *CellData) bool {
return a.GetBgColorMode() == b.GetBgColorMode() &&
a.GetBgColor() == b.GetBgColor()
}
// equalUnderline compares underline style and color between two cells.
func equalUnderline(a, b *CellData) bool {
if a.IsUnderline() == 0 && b.IsUnderline() == 0 {
return true
}
if a.GetUnderlineStyle() != b.GetUnderlineStyle() {
return false
}
aDefault := a.IsUnderlineColorDefault()
bDefault := b.IsUnderlineColorDefault()
if aDefault && bDefault {
return true
}
if aDefault != bDefault {
return false
}
return a.GetUnderlineColor() == b.GetUnderlineColor() &&
a.GetUnderlineColorMode() == b.GetUnderlineColorMode()
}
// equalFlags compares all attribute flags between two cells.
func equalFlags(a, b *CellData) bool {
return a.IsInverse() == b.IsInverse() &&
a.IsBold() == b.IsBold() &&
a.IsUnderline() == b.IsUnderline() &&
equalUnderline(a, b) &&
a.IsOverline() == b.IsOverline() &&
a.IsBlink() == b.IsBlink() &&
a.IsInvisible() == b.IsInvisible() &&
a.IsItalic() == b.IsItalic() &&
a.IsDim() == b.IsDim() &&
a.IsStrikethrough() == b.IsStrikethrough()
}
// attributesEquals is the fast-path check that compares all attributes.
func attributesEquals(a, b *CellData) bool {
return equalFg(a, b) && equalBg(a, b) && equalFlags(a, b)
}