diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 20b00ef3..a2ae4e08 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -1919,7 +1919,7 @@ export class InputHandler extends Disposable implements IInputHandler { */ public saveCursor(params: number[]): void { this._terminal.buffer.savedX = this._terminal.buffer.x; - this._terminal.buffer.savedY = this._terminal.buffer.y; + this._terminal.buffer.savedY = this._terminal.buffer.ybase + this._terminal.buffer.y; this._terminal.buffer.savedCurAttrData.fg = this._terminal.curAttrData.fg; this._terminal.buffer.savedCurAttrData.bg = this._terminal.curAttrData.bg; } @@ -1932,7 +1932,7 @@ export class InputHandler extends Disposable implements IInputHandler { */ public restoreCursor(params: number[]): void { this._terminal.buffer.x = this._terminal.buffer.savedX || 0; - this._terminal.buffer.y = this._terminal.buffer.savedY || 0; + this._terminal.buffer.y = Math.max(this._terminal.buffer.savedY - this._terminal.buffer.ybase, 0); this._terminal.curAttrData.fg = this._terminal.buffer.savedCurAttrData.fg; this._terminal.curAttrData.bg = this._terminal.buffer.savedCurAttrData.bg; } diff --git a/src/common/buffer/Buffer.ts b/src/common/buffer/Buffer.ts index b06545da..084b1bea 100644 --- a/src/common/buffer/Buffer.ts +++ b/src/common/buffer/Buffer.ts @@ -126,6 +126,7 @@ export class Buffer implements IBuffer { public clear(): void { this.ydisp = 0; this.ybase = 0; + this.savedY = 0; this.y = 0; this.x = 0; this.lines = new CircularList(this._getCorrectBufferLength(this._rows)); @@ -205,6 +206,7 @@ export class Buffer implements IBuffer { this.lines.trimStart(amountToTrim); this.ybase = Math.max(this.ybase - amountToTrim, 0); this.ydisp = Math.max(this.ydisp - amountToTrim, 0); + this.savedY = Math.max(this.savedY - amountToTrim, 0); } this.lines.maxLength = newMaxLength; } @@ -215,7 +217,6 @@ export class Buffer implements IBuffer { if (addToY) { this.y += addToY; } - this.savedY = Math.min(this.savedY, newRows - 1); this.savedX = Math.min(this.savedX, newCols - 1); this.scrollTop = 0; @@ -284,6 +285,7 @@ export class Buffer implements IBuffer { this.ybase--; } } + this.savedY = Math.max(this.savedY - countRemoved, 0); } private _reflowSmaller(newCols: number, newRows: number): void { @@ -395,6 +397,7 @@ export class Buffer implements IBuffer { } } } + this.savedY = Math.min(this.savedY + linesToAdd, this.ybase + newRows - 1); } // Rearrange lines in the buffer if there are any insertions, this is done at the end rather diff --git a/test/InputHandler.api.ts b/test/InputHandler.api.ts index 3a95feca..cbf5dbd6 100644 --- a/test/InputHandler.api.ts +++ b/test/InputHandler.api.ts @@ -292,6 +292,23 @@ describe('InputHandler Integration Tests', function(): void { assert.deepEqual(await getLinesAsArray(3), ['#', ' #', 'abcd####']); }); }); + + describe('ESC', () => { + describe('DECRC: Save cursor, ESC 7', () => { + it('should save the absolute cursor position so resizing restores to the correct position', async () => { + await page.evaluate(` + window.term.resize(10, 2); + window.term.write('1\\n\\r2\\n\\r3\\n\\r4\\n\\r5'); + window.term.write('\\x1b7\\x1b[?47h'); + `); + await page.evaluate(` + window.term.resize(10, 4); + window.term.write('\\x1b[?47l\\x1b8'); + `); + assert.deepEqual(await getCursor(), {col: 1, row: 3}); + }); + }); + }); }); async function openTerminal(options: ITerminalOptions = {}): Promise {