From dbceda7d39614d441813b6211b66f7d202a00fb2 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sun, 15 Jan 2017 19:02:29 -0800 Subject: [PATCH] Start of allowing terminals to be reused --- .../parts/terminal/common/terminal.ts | 7 ++++ .../electron-browser/terminalInstance.ts | 38 +++++++++++++++---- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/vs/workbench/parts/terminal/common/terminal.ts b/src/vs/workbench/parts/terminal/common/terminal.ts index aaff277951a..17b62d064f0 100644 --- a/src/vs/workbench/parts/terminal/common/terminal.ts +++ b/src/vs/workbench/parts/terminal/common/terminal.ts @@ -265,4 +265,11 @@ export interface ITerminalInstance { * null means the process was killed as a result of the ITerminalInstance being disposed. */ onExit(listener: (exitCode: number) => void): void; + + /** + * Immediately kills the terminal's current pty process and launches a new one to replace it. + * + * @param shell The new launch configuration. + */ + reuseTerminal(shell: IShellLaunchConfig): void; } diff --git a/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts b/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts index f54dc1069ca..d8ae919075b 100644 --- a/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts +++ b/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts @@ -110,14 +110,7 @@ export class TerminalInstance implements ITerminalInstance { }); this._xterm.open(this._xtermElement); - this._process.on('message', (message) => { - if (!this._xterm) { - return; - } - if (message.type === 'data') { - this._xterm.write(message.content); - } - }); + this._process.on('message', (message) => this._sendPtyDataToXterm(message)); this._xterm.on('data', (data) => { this._process.send({ event: 'input', @@ -371,6 +364,15 @@ export class TerminalInstance implements ITerminalInstance { }, LAUNCHING_DURATION); } + private _sendPtyDataToXterm(message: { type: string, content: string }): void { + if (!this._xterm) { + return; + } + if (message.type === 'data') { + this._xterm.write(message.content); + } + } + private _onPtyProcessExit(exitCode: number): void { // Prevent dispose functions being triggered multiple times if (this._isExiting) { @@ -418,6 +420,26 @@ export class TerminalInstance implements ITerminalInstance { } } + public reuseTerminal(shell: IShellLaunchConfig): void { + if (this._process) { + this._process.removeAllListeners('exit'); + if (this._process.connected) { + this._process.kill(); + } + this._process = null; + } + // Ensure new processes' output starts at start of new line + this._xterm.write('\n\x1b[G'); + this._createProcess(this._contextService.getWorkspace(), shell.name, shell); + this._process.on('message', (message) => this._sendPtyDataToXterm(message)); + // TODO: Get rid of wait for any key listeners and any other listeners that are no longer valid + if (this._isExiting && this._shellLaunchConfig.waitOnExit) { + this._xterm.setOption('disableStdin', false); + } + // Set the new shell launch config + this._shellLaunchConfig = shell; + } + // TODO: This should be private/protected // TODO: locale should not be optional public static createTerminalEnv(parentEnv: IStringDictionary, shell: IShellLaunchConfig, cwd: string, locale?: string): IStringDictionary { -- GitLab