Skip to content

Commit 7a8c3a4

Browse files
committed
refactor: serialize-addon, rename some APIs in SerializeAddon
1 parent 84f5f20 commit 7a8c3a4

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

addons/xterm-addon-serialize/src/SerializeAddon.ts

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/**
22
* Copyright (c) 2019 The xterm.js authors. All rights reserved.
33
* @license MIT
4+
*
5+
* (EXPERIMENTAL) This Addon is still under development
46
*/
57

68
import { Terminal, ITerminalAddon, IBuffer, IBufferCell } from 'xterm';
79

8-
function crop(value: number | undefined, low: number, high: number, initial: number): number {
9-
if (value === undefined) {
10-
return initial;
11-
}
10+
function constrain(value: number, low: number, high: number): number {
1211
return Math.max(low, Math.min(value, high));
1312
}
1413

@@ -22,43 +21,34 @@ abstract class BaseSerializeHandler {
2221
const cell2 = this._buffer.getNullCell();
2322
let oldCell = cell1;
2423

25-
this._serializeStart(endRow - startRow);
24+
this._beforeSerialize(endRow - startRow);
2625

2726
for (let row = startRow; row < endRow; row++) {
2827
const line = this._buffer.getLine(row);
29-
3028
if (line) {
3129
for (let col = 0; col < line.length; col++) {
32-
const newCell = line.getCell(col, oldCell === cell1 ? cell2 : cell1);
33-
34-
if (!newCell) {
30+
const c = line.getCell(col, oldCell === cell1 ? cell2 : cell1);
31+
if (!c) {
3532
console.warn(`Can't get cell at row=${row}, col=${col}`);
3633
continue;
3734
}
38-
39-
this._nextCell(newCell, oldCell, row, col);
40-
41-
oldCell = newCell;
35+
this._nextCell(c, oldCell, row, col);
36+
oldCell = c;
4237
}
4338
}
44-
4539
this._rowEnd(row);
4640
}
4741

48-
this._serializeEnd();
42+
this._afterSerialize();
4943

50-
return this._serializeFinished();
44+
return this._serializeString();
5145
}
5246

5347
protected _nextCell(cell: IBufferCell, oldCell: IBufferCell, row: number, col: number): void { }
54-
5548
protected _rowEnd(row: number): void { }
56-
57-
protected _serializeStart(rows: number): void { }
58-
59-
protected _serializeEnd(): void { }
60-
61-
protected _serializeFinished(): string { return ''; }
49+
protected _beforeSerialize(rows: number): void { }
50+
protected _afterSerialize(): void { }
51+
protected _serializeString(): string { return ''; }
6252
}
6353

6454
function equalFg(cell1: IBufferCell, cell2: IBufferCell): boolean {
@@ -91,7 +81,7 @@ class StringSerializeHandler extends BaseSerializeHandler {
9181
super(buffer);
9282
}
9383

94-
protected _serializeStart(rows: number): void {
84+
protected _beforeSerialize(rows: number): void {
9585
this._allRows = new Array<string>(rows);
9686
}
9787

@@ -153,15 +143,13 @@ class StringSerializeHandler extends BaseSerializeHandler {
153143
this._currentRow += cell.char;
154144
}
155145

156-
protected _serializeFinished(): string {
146+
protected _serializeString(): string {
157147
let rowEnd = this._allRows.length;
158-
159148
for (; rowEnd > 0; rowEnd--) {
160149
if (this._allRows[rowEnd - 1]) {
161150
break;
162151
}
163152
}
164-
165153
return this._allRows.slice(0, rowEnd).join('\r\n');
166154
}
167155
}
@@ -186,7 +174,7 @@ export class SerializeAddon implements ITerminalAddon {
186174
const maxRows = this._terminal.buffer.length;
187175
const handler = new StringSerializeHandler(this._terminal.buffer);
188176

189-
rows = crop(rows, 0, maxRows, maxRows);
177+
rows = (rows === undefined) ? maxRows : constrain(rows, 0, maxRows);
190178

191179
return handler.serialize(maxRows - rows, maxRows);
192180
}

0 commit comments

Comments
 (0)