serialize from the bottom of the terminal

This commit is contained in:
javacs3
2019-07-14 23:28:08 +08:00
parent fe3520c685
commit bca90245c8
3 changed files with 16 additions and 7 deletions
@@ -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;