-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathcopy.ts
133 lines (115 loc) · 4.09 KB
/
copy.ts
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
/*
* Copyright 2019 The Kubernetes Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { XtermResponse, XtermResponseCell } from '@kui-shell/core'
import { prepareCellForDomRenderer } from '@kui-shell/xterm-helpers'
import { IBufferCell, IBufferLine, Terminal } from 'xterm'
/**
* @return a XtermResponseCell matching the styling of the given xtermjs
* IBufferCell.
*
*/
function createCell(cell: IBufferCell): XtermResponseCell {
const { classList, style, textContent } = prepareCellForDomRenderer(cell)
return {
innerText: cell.getChars(),
classList,
style,
textContent
}
}
/** Do the two cells have the same styling? */
function sameStyle(cell1: IBufferCell, cell2: IBufferCell): boolean {
if (cell1.isAttributeDefault() && cell2.isAttributeDefault()) {
return true
} else if (
cell1.getFgColorMode() === cell2.getFgColorMode() &&
cell1.getBgColorMode() === cell2.getBgColorMode() &&
cell1.isBold() === cell2.isBold() &&
cell1.isItalic() === cell2.isItalic() &&
cell1.isDim() === cell2.isDim() &&
cell1.isUnderline() === cell2.isUnderline() &&
cell1.isStrikethrough() === cell2.isStrikethrough() &&
cell1.isBlink() === cell2.isBlink() &&
cell1.isInvisible() === cell2.isInvisible()
) {
return true
} else {
return false
}
}
/**
* In one xterm-row, squash consecutive spans that have the same
* className. We do this to avoid costly reflows, which xterm.js
* causes a huge number of, and that cost O(numSpans). xterm.js
* creates one span per character :(
*
*/
function squashRow(row: IBufferLine, previous: IBufferCell, current: IBufferCell, cells: XtermResponseCell[]) {
let runningSquash: XtermResponseCell
row.getCell(0, previous) // this copies the first cell into `previous`
if (cells[cells.length - 1] && sameStyle(previous, current)) {
runningSquash = cells[cells.length - 1]
runningSquash.innerText += previous.getChars()
} else {
runningSquash = createCell(previous)
cells.push(runningSquash)
}
for (let idx = 1; idx < row.length; idx++) {
row.getCell(idx - 1, previous) // previous now holds cells[idx-1]
row.getCell(idx, current) // current now holds cells[idx]
if (sameStyle(previous, current)) {
// same decoration from one cell to the next
runningSquash.innerText += current.getChars()
} else {
// we need to start a new span, because the decoration changed
runningSquash = createCell(current)
cells.push(runningSquash)
}
}
}
function lastFullLineIdx(terminal: Terminal, current: IBufferCell): number {
let ridx = terminal.buffer.active.length - 1
while (ridx >= 0) {
const line = terminal.buffer.active.getLine(ridx)
for (let cidx = 0; cidx < line.length; cidx++) {
line.getCell(cidx, current)
if (current.getCode() !== 0) {
return ridx
}
}
ridx--
}
return -1
}
export default function copy(terminal: Terminal): XtermResponse['rows'] {
const rows: XtermResponse['rows'] = []
// templates, to avoid creating lots of temporary objects
const previous = terminal.buffer.active.getNullCell()
const current = terminal.buffer.active.getNullCell()
const nLines = lastFullLineIdx(terminal, current) + 1
let prevRow: XtermResponseCell[] = undefined // eslint-disable-line
for (let ridx = 0; ridx < nLines; ridx++) {
const line = terminal.buffer.active.getLine(ridx)
if (line.isWrapped && prevRow !== undefined) {
squashRow(line, previous, current, prevRow)
} else {
prevRow = []
squashRow(line, previous, current, prevRow)
rows.push(prevRow)
}
}
return rows
}