From a069df7a369512a1e2ea5c850dacee9ebe8396ec Mon Sep 17 00:00:00 2001 From: javacs3 Date: Sun, 4 Aug 2019 23:02:05 +0800 Subject: [PATCH] refactor SerializeAddons initial value validation --- .../src/SerializeAddon.ts | 21 +++++++++++-------- .../xterm-addon-serialize/src/tsconfig.json | 7 +++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/addons/xterm-addon-serialize/src/SerializeAddon.ts b/addons/xterm-addon-serialize/src/SerializeAddon.ts index 9328e674..4a051c33 100644 --- a/addons/xterm-addon-serialize/src/SerializeAddon.ts +++ b/addons/xterm-addon-serialize/src/SerializeAddon.ts @@ -4,9 +4,14 @@ */ import { Terminal, ITerminalAddon } from 'xterm'; +// import { IBufferLine } from 'common/Types'; -function crop(value: number, from: number, to: number): number { - return Math.max(from, Math.min(value, to)); +function crop(value: number | undefined, low: number, high: number, initial: number): number { + if (value === undefined) { + return initial; + } else { + return Math.max(low, Math.min(value, high)); + } } export class SerializeAddon implements ITerminalAddon { @@ -23,18 +28,16 @@ export class SerializeAddon implements ITerminalAddon { if (!this._terminal) { throw new Error('Cannot use addon until it has been loaded'); } - const terminalRows = this._terminal.rows; - if (rows === undefined) { - rows = terminalRows; - } - rows = crop(rows, 0, terminalRows); + + const maxRows = this._terminal.rows; + rows = crop(rows, 0, maxRows, maxRows); const buffer = this._terminal.buffer; const lines: string[] = new Array(rows); - for (let i = terminalRows - rows; i < terminalRows; i++) { + for (let i = maxRows - rows; i < maxRows; 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'); diff --git a/addons/xterm-addon-serialize/src/tsconfig.json b/addons/xterm-addon-serialize/src/tsconfig.json index 5539aa56..57f3d6ed 100644 --- a/addons/xterm-addon-serialize/src/tsconfig.json +++ b/addons/xterm-addon-serialize/src/tsconfig.json @@ -10,10 +10,17 @@ "outDir": "../out", "sourceMap": true, "removeComments": true, + "baseUrl": ".", + "paths": { + "common/*": [ "../../../src/common/*" ] + }, "strict": true }, "include": [ "./**/*", "../../../typings/xterm.d.ts" + ], + "references": [ + { "path": "../../../src/common" } ] }