From bca90245c8ff900353770a38b0155cfcb2dcfdaa Mon Sep 17 00:00:00 2001 From: javacs3 Date: Sun, 14 Jul 2019 23:22:20 +0800 Subject: [PATCH] serialize from the bottom of the terminal --- .../src/SerializeAddon.api.ts | 2 +- .../src/SerializeAddon.ts | 19 ++++++++++++++----- .../typings/xterm-addon-serialize.d.ts | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/addons/xterm-addon-serialize/src/SerializeAddon.api.ts b/addons/xterm-addon-serialize/src/SerializeAddon.api.ts index 464316c3..5543696b 100644 --- a/addons/xterm-addon-serialize/src/SerializeAddon.api.ts +++ b/addons/xterm-addon-serialize/src/SerializeAddon.api.ts @@ -83,7 +83,7 @@ describe('SerializeAddon', () => { window.term.write(${util.inspect(lines.join('\r\n'))}); `); - assert.equal(await page.evaluate(`serializeAddon.serialize(${halfRows});`), lines.slice(0, halfRows).join('\r\n')); + assert.equal(await page.evaluate(`serializeAddon.serialize(${halfRows});`), lines.slice(halfRows, 2 * halfRows).join('\r\n')); }); it('serialize 0 rows of content', async function (): Promise { diff --git a/addons/xterm-addon-serialize/src/SerializeAddon.ts b/addons/xterm-addon-serialize/src/SerializeAddon.ts index 10d920cd..5d060949 100644 --- a/addons/xterm-addon-serialize/src/SerializeAddon.ts +++ b/addons/xterm-addon-serialize/src/SerializeAddon.ts @@ -5,6 +5,10 @@ import { Terminal, ITerminalAddon } from 'xterm'; +function crop(value: number, from: number, to: number) { + return Math.max(from, Math.min(value, to)) +} + export class SerializeAddon implements ITerminalAddon { private _terminal: Terminal | undefined; @@ -19,13 +23,18 @@ export class SerializeAddon implements ITerminalAddon { if (!this._terminal) { throw new Error('Cannot use addon until it has been loaded'); } - const buffer = this._terminal.buffer; - const length = Math.max(0, Math.min((rows === undefined ? buffer.length : rows), buffer.length)); - const lines: string[] = new Array(length); + const terminalRows = this._terminal.rows; + if (rows === undefined) { + rows = terminalRows; + } + rows = crop(rows, 0, terminalRows); - for (let i = 0; i < length; i++) { + const buffer = this._terminal.buffer; + const lines: string[] = new Array(rows); + + for (let i = terminalRows - rows; i < terminalRows; i++) { const line = buffer.getLine(i); - lines[i] = line ? line.translateToString() : ''; + lines[i - terminalRows + rows] = line ? line.translateToString() : ''; } return lines.join('\r\n'); 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 78b615d7..9e7b500a 100644 --- a/addons/xterm-addon-serialize/typings/xterm-addon-serialize.d.ts +++ b/addons/xterm-addon-serialize/typings/xterm-addon-serialize.d.ts @@ -25,7 +25,7 @@ declare module 'xterm-addon-serialize' { * 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 + * @param rows The number of rows to serialize, starting from the bottom of the * terminal. This defaults to the number of rows in the viewport. */ public serialize(rows?: number): string;