/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the Source EULA. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { Event, Emitter } from 'vs/base/common/event'; import { ITerminalService, ITerminalProcessExtHostProxy, IShellLaunchConfig, ITerminalChildProcess } from 'vs/workbench/contrib/terminal/common/terminal'; import { IDisposable } from 'vs/base/common/lifecycle'; import { URI } from 'vs/base/common/uri'; export class TerminalProcessExtHostProxy implements ITerminalChildProcess, ITerminalProcessExtHostProxy { private _disposables: IDisposable[] = []; private readonly _onProcessData = new Emitter(); public get onProcessData(): Event { return this._onProcessData.event; } private readonly _onProcessExit = new Emitter(); public get onProcessExit(): Event { return this._onProcessExit.event; } private readonly _onProcessIdReady = new Emitter(); public get onProcessIdReady(): Event { return this._onProcessIdReady.event; } private readonly _onProcessTitleChanged = new Emitter(); public get onProcessTitleChanged(): Event { return this._onProcessTitleChanged.event; } private readonly _onInput = new Emitter(); public get onInput(): Event { return this._onInput.event; } private readonly _onResize: Emitter<{ cols: number, rows: number }> = new Emitter<{ cols: number, rows: number }>(); public get onResize(): Event<{ cols: number, rows: number }> { return this._onResize.event; } private readonly _onShutdown = new Emitter(); public get onShutdown(): Event { return this._onShutdown.event; } private readonly _onRequestInitialCwd = new Emitter(); public get onRequestInitialCwd(): Event { return this._onRequestInitialCwd.event; } private readonly _onRequestCwd = new Emitter(); public get onRequestCwd(): Event { return this._onRequestCwd.event; } private readonly _onRequestLatency = new Emitter(); public get onRequestLatency(): Event { return this._onRequestLatency.event; } private _pendingInitialCwdRequests: ((value?: string | Thenable) => void)[] = []; private _pendingCwdRequests: ((value?: string | Thenable) => void)[] = []; private _pendingLatencyRequests: ((value?: number | Thenable) => void)[] = []; constructor( public terminalId: number, shellLaunchConfig: IShellLaunchConfig, activeWorkspaceRootUri: URI, cols: number, rows: number, @ITerminalService private readonly _terminalService: ITerminalService ) { this._terminalService.requestExtHostProcess(this, shellLaunchConfig, activeWorkspaceRootUri, cols, rows); setTimeout(() => this._onProcessTitleChanged.fire('Starting...'), 0); } public dispose(): void { this._disposables.forEach(d => d.dispose()); this._disposables.length = 0; } public emitData(data: string): void { this._onProcessData.fire(data); } public emitTitle(title: string): void { this._onProcessTitleChanged.fire(title); } public emitPid(pid: number): void { this._onProcessIdReady.fire(pid); } public emitExit(exitCode: number): void { this._onProcessExit.fire(exitCode); this.dispose(); } public emitInitialCwd(initialCwd: string): void { while (this._pendingInitialCwdRequests.length > 0) { this._pendingInitialCwdRequests.pop()!(initialCwd); } } public emitCwd(cwd: string): void { while (this._pendingCwdRequests.length > 0) { this._pendingCwdRequests.pop()!(cwd); } } public emitLatency(latency: number): void { while (this._pendingLatencyRequests.length > 0) { this._pendingLatencyRequests.pop()!(latency); } } public shutdown(immediate: boolean): void { this._onShutdown.fire(immediate); } public input(data: string): void { this._onInput.fire(data); } public resize(cols: number, rows: number): void { this._onResize.fire({ cols, rows }); } public getInitialCwd(): Promise { return new Promise(resolve => { this._onRequestInitialCwd.fire(); this._pendingInitialCwdRequests.push(resolve); }); } public getCwd(): Promise { return new Promise(resolve => { this._onRequestCwd.fire(); this._pendingCwdRequests.push(resolve); }); } public getLatency(): Promise { return new Promise(resolve => { this._onRequestLatency.fire(); this._pendingLatencyRequests.push(resolve); }); } }