mirror of
https://github.com/wavetermdev/xterm.js.git
synced 2026-08-05 13:43:48 -07:00
serialize from the bottom of the terminal
This commit is contained in:
@@ -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<any> {
|
||||
|
||||
@@ -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<string>(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<string>(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');
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user