diff --git a/src/common/InputHandler.test.ts b/src/common/InputHandler.test.ts index baae735c94..c2198b8281 100644 --- a/src/common/InputHandler.test.ts +++ b/src/common/InputHandler.test.ts @@ -438,6 +438,37 @@ describe('InputHandler', () => { inputHandler.eraseInLine(Params.fromArray([2])); assert.equal(bufferService.buffer.lines.get(2)!.isWrapped, false); }); + it('ED2 with scrollOnDisplayErase turned on', async () => { + const inputHandler = new TestInputHandler( + bufferService, + new MockCharsetService(), + new MockCoreService(), + new MockLogService(), + new MockOptionsService({ scrollOnDisplayErase: true }), + new MockOscLinkService(), + new MockCoreMouseService(), + new MockUnicodeService() + ); + const aLine = Array(bufferService.cols + 1).join('a'); + // add 2 full lines of text. + await inputHandler.parseP(aLine); + await inputHandler.parseP(aLine); + + inputHandler.eraseInDisplay(Params.fromArray([2])); + // those 2 lines should have been pushed to scrollback. + assert.equal(bufferService.rows + 2, bufferService.buffer.lines.length); + assert.equal(bufferService.buffer.ybase, 2); + assert.equal(bufferService.buffer.lines.get(0)?.translateToString(), aLine); + assert.equal(bufferService.buffer.lines.get(1)?.translateToString(), aLine); + + // Move to last line and add more text. + bufferService.buffer.y = bufferService.rows - 1; + bufferService.buffer.x = 0; + await inputHandler.parseP(aLine); + inputHandler.eraseInDisplay(Params.fromArray([2])); + // Screen should have been scrolled by a full screen size. + assert.equal(bufferService.rows * 2 + 2, bufferService.buffer.lines.length); + }); it('eraseInDisplay', async () => { const bufferService = new MockBufferService(80, 7); const inputHandler = new TestInputHandler( diff --git a/src/common/InputHandler.ts b/src/common/InputHandler.ts index b94d785544..81cf086cc2 100644 --- a/src/common/InputHandler.ts +++ b/src/common/InputHandler.ts @@ -1220,12 +1220,27 @@ export class InputHandler extends Disposable implements IInputHandler { this._dirtyRowTracker.markDirty(0); break; case 2: - j = this._bufferService.rows; - this._dirtyRowTracker.markDirty(j - 1); - while (j--) { - this._resetBufferLine(j, respectProtect); + if (this._optionsService.rawOptions.scrollOnDisplayErase) { + j = this._bufferService.rows; + this._dirtyRowTracker.markRangeDirty(0, j - 1); + while (j--) { + const currentLine = this._activeBuffer.lines.get(this._activeBuffer.ybase + j); + if (currentLine?.getTrimmedLength()) { + break; + } + } + for (; j >= 0; j--) { + this._bufferService.scroll(this._eraseAttrData()); + } + } + else { + j = this._bufferService.rows; + this._dirtyRowTracker.markDirty(j - 1); + while (j--) { + this._resetBufferLine(j, respectProtect); + } + this._dirtyRowTracker.markDirty(0); } - this._dirtyRowTracker.markDirty(0); break; case 3: // Clear scrollback (everything not in viewport) diff --git a/src/common/services/OptionsService.ts b/src/common/services/OptionsService.ts index a757c17916..772b0a0af8 100644 --- a/src/common/services/OptionsService.ts +++ b/src/common/services/OptionsService.ts @@ -54,7 +54,8 @@ export const DEFAULT_OPTIONS: Readonly> = { convertEol: false, termName: 'xterm', cancelEvents: false, - overviewRuler: {} + overviewRuler: {}, + scrollOnDisplayErase: false }; const FONT_WEIGHT_OPTIONS: Extract[] = ['normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900']; diff --git a/src/common/services/Services.ts b/src/common/services/Services.ts index 0ceff36c4e..080f4a8e30 100644 --- a/src/common/services/Services.ts +++ b/src/common/services/Services.ts @@ -252,6 +252,7 @@ export interface ITerminalOptions { windowOptions?: IWindowOptions; wordSeparator?: string; overviewRuler?: IOverviewRulerOptions; + scrollOnDisplayErase?: boolean; [key: string]: any; cancelEvents: boolean; diff --git a/typings/xterm-headless.d.ts b/typings/xterm-headless.d.ts index 3cbde44b2b..b7aa1b599e 100644 --- a/typings/xterm-headless.d.ts +++ b/typings/xterm-headless.d.ts @@ -248,6 +248,13 @@ declare module '@xterm/headless' { * All features are disabled by default for security reasons. */ windowOptions?: IWindowOptions; + + /** + * If enabled ED2 (clear screen) escape sequence will push + * erased text to scrollback. + * This emulates PuTTY default clear screen behavior. + */ + scrollOnDisplayErase?: boolean; } /** diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index f9cf14f9c7..9becb0b35b 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -331,6 +331,13 @@ declare module '@xterm/xterm' { * decorations underneath the scroll bar. */ overviewRuler?: IOverviewRulerOptions; + + /** + * If enabled ED2 (clear screen) escape sequence will push + * erased text to scrollback. + * This emulates PuTTY default clear screen behavior. + */ + scrollOnDisplayErase?: boolean; } /**