add missing api docs and refactor serialize implementation by using list join instead and some code clean up

This commit is contained in:
javacs3
2019-07-12 23:23:06 +08:00
parent 57a8255654
commit 31f812cf94
2 changed files with 14 additions and 13 deletions
@@ -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<string>(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 { }
}
@@ -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;
/**