Add exclude mode/alt buffer options to serialize addon

Fixes #3472
This commit is contained in:
Daniel Imms
2021-09-15 05:04:11 -07:00
parent be7fcbe485
commit c186fbeafc
3 changed files with 42 additions and 14 deletions
@@ -433,26 +433,37 @@ export class SerializeAddon implements ITerminalAddon {
return content; return content;
} }
public serialize(scrollback?: number): string { public serialize(options?: ISerializeOptions): string {
// TODO: Add combinedData support // TODO: Add combinedData support
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');
} }
// Normal buffer // Normal buffer
let content = this._serializeBuffer(this._terminal, this._terminal.buffer.normal, scrollback); let content = this._serializeBuffer(this._terminal, this._terminal.buffer.normal, options?.scrollback);
// Alternate buffer // Alternate buffer
if (this._terminal.buffer.active.type === 'alternate') { if (!options?.excludeAltBuffer) {
const alternativeScreenContent = this._serializeBuffer(this._terminal, this._terminal.buffer.alternate, undefined); if (this._terminal.buffer.active.type === 'alternate') {
content += `\u001b[?1049h\u001b[H${alternativeScreenContent}`; const alternativeScreenContent = this._serializeBuffer(this._terminal, this._terminal.buffer.alternate, undefined);
content += `\u001b[?1049h\u001b[H${alternativeScreenContent}`;
}
} }
// Modes // Modes
content += this._serializeModes(this._terminal); if (!options?.excludeModes) {
content += this._serializeModes(this._terminal);
}
return content; return content;
} }
public dispose(): void { } public dispose(): void { }
} }
interface ISerializeOptions {
scrollback?: number;
excludeModes?: boolean;
excludeAltBuffer?: boolean;
}
@@ -146,7 +146,7 @@ describe('SerializeAddon', () => {
const cols = 10; const cols = 10;
const lines = newArray<string>((index: number) => digitsString(cols, index), rows); const lines = newArray<string>((index: number) => digitsString(cols, index), rows);
await writeSync(page, lines.join('\\r\\n')); await writeSync(page, lines.join('\\r\\n'));
assert.equal(await page.evaluate(`serializeAddon.serialize(${halfScrollback});`), lines.slice(halfScrollback, rows).join('\r\n')); assert.equal(await page.evaluate(`serializeAddon.serialize({ scrollback: ${halfScrollback} });`), lines.slice(halfScrollback, rows).join('\r\n'));
}); });
it('serialize 0 rows of scrollback', async function(): Promise<any> { it('serialize 0 rows of scrollback', async function(): Promise<any> {
@@ -154,7 +154,7 @@ describe('SerializeAddon', () => {
const cols = 10; const cols = 10;
const lines = newArray<string>((index: number) => digitsString(cols, index), rows); const lines = newArray<string>((index: number) => digitsString(cols, index), rows);
await writeSync(page, lines.join('\\r\\n')); await writeSync(page, lines.join('\\r\\n'));
assert.equal(await page.evaluate(`serializeAddon.serialize(0);`), lines.slice(rows - 10, rows).join('\r\n')); assert.equal(await page.evaluate(`serializeAddon.serialize({ scrollback: 0 });`), lines.slice(rows - 10, rows).join('\r\n'));
}); });
it('serialize all rows of content with color16', async function(): Promise<any> { it('serialize all rows of content with color16', async function(): Promise<any> {
@@ -7,14 +7,14 @@ import { Terminal, ITerminalAddon } from 'xterm';
declare module 'xterm-addon-serialize' { declare module 'xterm-addon-serialize' {
/** /**
* An xterm.js addon that enables web links. * An xterm.js addon that enables serialization of terminal contents.
*/ */
export class SerializeAddon implements ITerminalAddon { export class SerializeAddon implements ITerminalAddon {
constructor(); constructor();
/** /**
* Activates the addon * Activates the addon.
* @param terminal The terminal the addon is being loaded in. * @param terminal The terminal the addon is being loaded in.
*/ */
public activate(terminal: Terminal): void; public activate(terminal: Terminal): void;
@@ -24,15 +24,32 @@ declare module 'xterm-addon-serialize' {
* the state. The cursor will also be positioned to the correct cell. When restoring a terminal * 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 * it is best to do before `Terminal.open` is called to avoid wasting CPU cycles rendering
* incomplete frames. * incomplete frames.
* @param scrollback The number of rows in scrollback buffer to serialize, starting from the * @param options Custom options to allow control over what gets serialized.
* bottom of the scrollback buffer. This defaults to the all available rows in the scrollback
* buffer.
*/ */
public serialize(scrollback?: number): string; public serialize(options?: ISerializeOptions): string;
/** /**
* Disposes the addon. * Disposes the addon.
*/ */
public dispose(): void; public dispose(): void;
} }
export interface ISerializeOptions {
/**
* The number of rows in the scrollback buffer to serialize, starting from the bottom of the
* scrollback buffer. When not specified, all available rows in the scrollback buffer will be
* serialized.
*/
scrollback?: number;
/**
* Whether to exclude the terminal modes from the serialization. False by default.
*/
excludeModes?: boolean;
/**
* Whether to exclude the alt buffer from the serialization. False by default.
*/
excludeAltBuffer?: boolean;
}
} }