From a9b7ef5dc38373a232f4e6d91b0516867d06f020 Mon Sep 17 00:00:00 2001 From: Andres Mejia Date: Sun, 14 Jan 2018 13:03:07 -0500 Subject: [PATCH] Clear selection manager when the terminal switches its buffer set or buffer. --- src/Interfaces.ts | 2 +- src/SelectionManager.ts | 26 +++++++++++++++++++++----- src/Terminal.ts | 1 + src/utils/TestUtils.test.ts | 2 +- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Interfaces.ts b/src/Interfaces.ts index 07610956..780a4047 100644 --- a/src/Interfaces.ts +++ b/src/Interfaces.ts @@ -169,7 +169,7 @@ export interface IBuffer { prevStop(x?: number): number; } -export interface IBufferSet { +export interface IBufferSet extends IEventEmitter { alt: IBuffer; normal: IBuffer; active: IBuffer; diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index 3e9a4d40..29003629 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -119,11 +119,27 @@ export class SelectionManager extends EventEmitter implements ISelectionManager this._mouseMoveListener = event => this._onMouseMove(event); this._mouseUpListener = event => this._onMouseUp(event); - // Only adjust the selection on trim, shiftElements is rarely used (only in - // reverseIndex) and delete in a splice is only ever used when the same - // number of elements was just added. Given this is could actually be - // beneficial to leave the selection as is for these cases. - this._buffer.lines.on('trim', (amount: number) => this._onTrim(amount)); + this._updateBufferSetHandlers(); + this._terminal.on('setup', () => { + this._onSetupHandler(); + }); + } + + private _onSetupHandler(): void { + this.clearSelection(); + this._updateBufferSetHandlers(); + } + + private _updateBufferSetHandlers(): void { + this._terminal.buffers.on('activate', (buffer: IBuffer) => { + this.clearSelection(); + }); + // Only adjust the selection on trim, shiftElements is rarely used (only in + // reverseIndex) and delete in a splice is only ever used when the same + // number of elements was just added. Given this is could actually be + // beneficial to leave the selection as is for these cases. + this._terminal.buffers.normal.lines.on('trim', (amount: number) => this._onTrim(amount)); + this._terminal.buffers.alt.lines.on('trim', (amount: number) => this._onTrim(amount)); } /** diff --git a/src/Terminal.ts b/src/Terminal.ts index 511cb384..11c2cf5e 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -292,6 +292,7 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT // Create the terminal's buffers and set the current buffer this.buffers = new BufferSet(this); + this.emit('setup'); } /** diff --git a/src/utils/TestUtils.test.ts b/src/utils/TestUtils.test.ts index 80e25277..54c52078 100644 --- a/src/utils/TestUtils.test.ts +++ b/src/utils/TestUtils.test.ts @@ -37,7 +37,7 @@ export class MockTerminal implements ITerminal { throw new Error('Method not implemented.'); } on(event: string, callback: () => void): void { - throw new Error('Method not implemented.'); + /* Simply do nothing */ } off(type: string, listener: IListenerType): void { throw new Error('Method not implemented.');