refactor SerializeAddons initial value validation

This commit is contained in:
javacs3
2019-11-21 20:41:46 +08:00
parent 2e237cc449
commit a069df7a36
2 changed files with 19 additions and 9 deletions
@@ -4,9 +4,14 @@
*/ */
import { Terminal, ITerminalAddon } from 'xterm'; import { Terminal, ITerminalAddon } from 'xterm';
// import { IBufferLine } from 'common/Types';
function crop(value: number, from: number, to: number): number { function crop(value: number | undefined, low: number, high: number, initial: number): number {
return Math.max(from, Math.min(value, to)); if (value === undefined) {
return initial;
} else {
return Math.max(low, Math.min(value, high));
}
} }
export class SerializeAddon implements ITerminalAddon { export class SerializeAddon implements ITerminalAddon {
@@ -23,18 +28,16 @@ export class SerializeAddon implements ITerminalAddon {
if (!this._terminal) { if (!this._terminal) {
throw new Error('Cannot use addon until it has been loaded'); throw new Error('Cannot use addon until it has been loaded');
} }
const terminalRows = this._terminal.rows;
if (rows === undefined) { const maxRows = this._terminal.rows;
rows = terminalRows; rows = crop(rows, 0, maxRows, maxRows);
}
rows = crop(rows, 0, terminalRows);
const buffer = this._terminal.buffer; const buffer = this._terminal.buffer;
const lines: string[] = new Array<string>(rows); const lines: string[] = new Array<string>(rows);
for (let i = terminalRows - rows; i < terminalRows; i++) { for (let i = maxRows - rows; i < maxRows; i++) {
const line = buffer.getLine(i); const line = buffer.getLine(i);
lines[i - terminalRows + rows] = line ? line.translateToString() : ''; lines[i - maxRows + rows] = line ? line.translateToString() : '';
} }
return lines.join('\r\n'); return lines.join('\r\n');
@@ -10,10 +10,17 @@
"outDir": "../out", "outDir": "../out",
"sourceMap": true, "sourceMap": true,
"removeComments": true, "removeComments": true,
"baseUrl": ".",
"paths": {
"common/*": [ "../../../src/common/*" ]
},
"strict": true "strict": true
}, },
"include": [ "include": [
"./**/*", "./**/*",
"../../../typings/xterm.d.ts" "../../../typings/xterm.d.ts"
],
"references": [
{ "path": "../../../src/common" }
] ]
} }