diff --git a/addons/xterm-addon-serialize/src/SerializeAddon.ts b/addons/xterm-addon-serialize/src/SerializeAddon.ts index 8dd5b194..720da58b 100644 --- a/addons/xterm-addon-serialize/src/SerializeAddon.ts +++ b/addons/xterm-addon-serialize/src/SerializeAddon.ts @@ -15,28 +15,21 @@ export class SerializeAddon implements ITerminalAddon { } public serialize(rows?: number): string { + // TODO: Add frontground/background color support later if (!this._terminal) { - return ''; + throw new Error('No terminal found!'); } const buffer = this._terminal.buffer; const length = Math.max(0, Math.min((rows === undefined ? buffer.length : rows), buffer.length)); - let data = ''; + const lines: string[] = new Array(length); for (let i = 0; i < length; i++) { const line = buffer.getLine(i); - const last = i === length - 1; - if (line) { - data += line.translateToString(); - } - if (!last) { - data += '\r\n'; - } + lines[i] = line ? line.translateToString() : ''; } - return data; + return lines.join('\r\n'); } - public dispose(): void { - if (this._terminal !== undefined) { } - } + public dispose(): void { } } diff --git a/addons/xterm-addon-serialize/typings/xterm-addon-serialize.d.ts b/addons/xterm-addon-serialize/typings/xterm-addon-serialize.d.ts index e36e724a..78b615d7 100644 --- a/addons/xterm-addon-serialize/typings/xterm-addon-serialize.d.ts +++ b/addons/xterm-addon-serialize/typings/xterm-addon-serialize.d.ts @@ -20,6 +20,14 @@ declare module 'xterm-addon-serialize' { */ public activate(terminal: Terminal): void; + /** + * Serializes terminal rows into a string that can be written back to the terminal + * to restore the state. The cursor will also be positioned to the correct cell. + * When restoring a terminal it is best to do before `Terminal.open` is called + * to avoid wasting CPU cycles rendering incomplete frames. + * @param rows The number of rows to serialize, starting from the top of the + * terminal. This defaults to the number of rows in the viewport. + */ public serialize(rows?: number): string; /**