terminalProcessExtHostProxy.ts 2.4 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { ITerminalChildProcess, IMessageToTerminalProcess, IMessageFromTerminalProcess } from 'vs/workbench/parts/terminal/node/terminal';
7
import { EventEmitter } from 'events';
8
import { ITerminalService, ITerminalProcessExtHostProxy, IShellLaunchConfig } from 'vs/workbench/parts/terminal/common/terminal';
9
import { IDisposable } from '../../../../base/common/lifecycle';
10

11
export class TerminalProcessExtHostProxy extends EventEmitter implements ITerminalChildProcess, ITerminalProcessExtHostProxy {
D
Daniel Imms 已提交
12 13
	// TODO: Set this properly
	public connected: boolean = true;
14

15
	constructor(
16
		public terminalId: number,
17 18
		shellLaunchConfig: IShellLaunchConfig,
		cols: number,
D
Daniel Imms 已提交
19
		rows: number,
20 21
		@ITerminalService private _terminalService: ITerminalService
	) {
22 23
		super();

24
		// TODO: Return TPromise<boolean> indicating success? Teardown if failure?
25
		this._terminalService.requestExtHostProcess(this, shellLaunchConfig, cols, rows);
26 27
	}

28
	public emitData(data: string): void {
D
Daniel Imms 已提交
29 30
		this.emit('message', { type: 'data', content: data } as IMessageFromTerminalProcess);
	}
31 32
	public emitTitle(title: string): void {
		this.emit('message', { type: 'title', content: title } as IMessageFromTerminalProcess);
D
Daniel Imms 已提交
33
	}
34 35
	public emitPid(pid: number): void {
		this.emit('message', { type: 'pid', content: pid } as IMessageFromTerminalProcess);
D
Daniel Imms 已提交
36 37
	}

38
	public send(message: IMessageToTerminalProcess): boolean {
D
Daniel Imms 已提交
39 40 41 42
		switch (message.event) {
			case 'input': this.emit('input', message.data); break;
			case 'resize': this.emit('resize', message.cols, message.rows); break;
			case 'shutdown': this.emit('shutdown'); break;
43
		}
44 45
		return true;
	}
46 47 48

	public onInput(listener: (data: string) => void): IDisposable {
		// TODO: Dispose of me
D
Daniel Imms 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61
		this.on('input', data => listener(data));
		return null;
	}

	public onResize(listener: (cols: number, rows: number) => void): IDisposable {
		// TODO: Dispose of me
		this.on('resize', (cols, rows) => listener(cols, rows));
		return null;
	}

	public onShutdown(listener: () => void): IDisposable {
		// TODO: Dispose of me
		this.on('shutdown', () => listener());
62 63
		return null;
	}
64
}