terminalService.ts 19.6 KB
Newer Older
D
Daniel Imms 已提交
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 Event, {Emitter} from 'vs/base/common/event';
D
Daniel Imms 已提交
7 8 9 10
import platform = require('vs/base/common/platform');
import {Builder} from 'vs/base/browser/builder';
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
import {IContextKeyService, IContextKey} from 'vs/platform/contextkey/common/contextkey';
11
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
D
Daniel Imms 已提交
12
import {IPanelService} from 'vs/workbench/services/panel/common/panelService';
13
import {IPartService} from 'vs/workbench/services/part/common/partService';
14
import {ITerminalInstance, ITerminalService, KEYBINDING_CONTEXT_TERMINAL_FOCUS, TERMINAL_PANEL_ID} from 'vs/workbench/parts/terminal/electron-browser/terminal';
15
import {TPromise} from 'vs/base/common/winjs.base';
D
Daniel Imms 已提交
16 17
import {TerminalConfigHelper} from 'vs/workbench/parts/terminal/electron-browser/terminalConfigHelper';
import {TerminalInstance} from 'vs/workbench/parts/terminal/electron-browser/terminalInstance';
D
Daniel Imms 已提交
18 19

export class TerminalService implements ITerminalService {
20
	public _serviceBrand: any;
D
Daniel Imms 已提交
21

22 23 24 25
	private _activeTerminalInstanceIndex: number = 0;
	private _terminalInstances: ITerminalInstance[] = [];
	public get activeTerminalInstanceIndex(): number { return this._activeTerminalInstanceIndex; }
	public get terminalInstances(): ITerminalInstance[] { return this._terminalInstances; }
26

27 28 29
	private _onActiveInstanceChanged: Emitter<string>;
	private _onInstancesChanged: Emitter<string>;
	private _onInstanceTitleChanged: Emitter<string>;
30 31 32 33
	public get onActiveInstanceChanged(): Event<string> { return this._onActiveInstanceChanged.event; }
	public get onInstancesChanged(): Event<string> { return this._onInstancesChanged.event; }
	public get onInstanceTitleChanged(): Event<string> { return this._onInstanceTitleChanged.event; }

D
Daniel Imms 已提交
34 35 36
	private terminalContainer: HTMLElement;
	private configHelper: TerminalConfigHelper;
	private terminalFocusContextKey: IContextKey<boolean>;
37

D
Daniel Imms 已提交
38
	constructor(
D
Daniel Imms 已提交
39
		@IConfigurationService private configurationService: IConfigurationService,
40
		@IContextKeyService private contextKeyService: IContextKeyService,
41
		@IInstantiationService private instantiationService: IInstantiationService,
42
		@IPanelService private panelService: IPanelService,
43
		@IPartService private partService: IPartService
D
Daniel Imms 已提交
44
	) {
45 46 47
		this._onActiveInstanceChanged = new Emitter<string>();
		this._onInstancesChanged = new Emitter<string>();
		this._onInstanceTitleChanged = new Emitter<string>();
D
Daniel Imms 已提交
48 49
		this.terminalFocusContextKey = KEYBINDING_CONTEXT_TERMINAL_FOCUS.bindTo(this.contextKeyService);
		this.configHelper = <TerminalConfigHelper>this.instantiationService.createInstance(TerminalConfigHelper, platform.platform);
50 51
	}

52
	public createInstance(): ITerminalInstance {
D
Daniel Imms 已提交
53 54
		console.log('creating instance');
		console.log('config helper', this.configHelper);
55
		let terminalInstance = <TerminalInstance>this.instantiationService.createInstance(TerminalInstance,
D
Daniel Imms 已提交
56
			this.terminalFocusContextKey, this.onTerminalInstanceDispose.bind(this), this.configHelper, this.terminalContainer);
57 58 59
		this.terminalInstances.push(terminalInstance);
		this._onInstancesChanged.fire();
		return terminalInstance;
60 61
	}

62 63
	private onTerminalInstanceDispose(terminalInstance: TerminalInstance): void {
		// TODO: Handle terminal exit here
64 65
	}

66 67 68 69 70
	public getActiveInstance(): ITerminalInstance {
		if (this.activeTerminalInstanceIndex < 0 || this.activeTerminalInstanceIndex >= this.terminalInstances.length) {
			return null;
		}
		return this.terminalInstances[this.activeTerminalInstanceIndex];
D
Daniel Imms 已提交
71 72
	}

73 74
	public getInstanceFromId(terminalId: number): ITerminalInstance {
		return this.terminalInstances[this.getIndexFromId(terminalId)];
75 76
	}

77 78
	public setActiveInstance(terminalInstance: ITerminalInstance): void {
		this.setActiveInstanceByIndex(this.getIndexFromId(terminalInstance.id));
79 80
	}

81 82 83 84
	public setActiveInstanceByIndex(terminalIndex: number): void {
		this._activeTerminalInstanceIndex = terminalIndex;
		// TODO: Inform the panel
		this._onActiveInstanceChanged.fire();
85 86
	}

87 88 89 90 91 92 93 94 95
	public setActiveInstanceToNext(): void {
		if (this.terminalInstances.length <= 1) {
			return;
		}
		let newIndex = this.activeTerminalInstanceIndex++;
		if (newIndex >= this.terminalInstances.length) {
			newIndex = 0;
		}
		this.setActiveInstanceByIndex(newIndex);
96 97
	}

98 99 100 101 102 103 104 105 106
	public setActiveInstanceToPrevious(): void {
		if (this.terminalInstances.length <= 1) {
			return;
		}
		let newIndex = this.activeTerminalInstanceIndex--;
		if (newIndex < 0) {
			newIndex = this.terminalInstances.length - 1;
		}
		this.setActiveInstanceByIndex(newIndex);
107 108
	}

D
Daniel Imms 已提交
109
	public setContainers(panelContainer: Builder, terminalContainer: HTMLElement): void {
D
Daniel Imms 已提交
110
		console.log('set containers');
D
Daniel Imms 已提交
111
		this.terminalContainer = terminalContainer;
112
		this._terminalInstances.forEach(terminalInstance => {
D
Daniel Imms 已提交
113
			terminalInstance.attachToElement(this.terminalContainer);
114
		});
D
Daniel Imms 已提交
115
		this.configHelper.panelContainer = panelContainer;
116 117
	}

118 119
	public showPanel(focus?: boolean): TPromise<void> {
		return new TPromise<void>((complete) => {
120 121 122 123
			let panel = this.panelService.getActivePanel();
			if (!panel || panel.getId() !== TERMINAL_PANEL_ID) {
				return this.panelService.openPanel(TERMINAL_PANEL_ID, focus).then(() => {
					panel = this.panelService.getActivePanel();
124 125
					//complete(<TerminalPanel>panel);
					complete(void 0);
126 127
				});
			} else {
128 129 130
				if (focus) {
					panel.focus();
				}
131 132
				complete(void 0);
				//complete(<TerminalPanel>panel);
133 134
			}
		});
135 136
	}

137
	public togglePanel(): TPromise<any> {
138 139 140 141 142
		const panel = this.panelService.getActivePanel();
		if (panel && panel.getId() === TERMINAL_PANEL_ID) {
			this.partService.setPanelHidden(true);
			return TPromise.as(null);
		}
143 144
		this.setActiveInstance(this.getActiveInstance());
		this.showPanel(true);
145 146
	}

147 148 149 150 151
	private getIndexFromId(terminalId: number): number {
		let terminalIndex = -1;
		this.terminalInstances.forEach((terminalInstance, i) => {
			if (terminalInstance.id === terminalId) {
				terminalIndex = i;
152
			}
153
		});
154 155
		if (terminalIndex === -1) {
			throw new Error(`Terminal with ID ${terminalId} does not exist (has it already been disposed?)`);
156
		}
157
		return terminalIndex;
158
	}
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
}

// import URI from 'vs/base/common/uri';
// import Event, {Emitter} from 'vs/base/common/event';
// import cp = require('child_process');
// import nls = require('vs/nls');
// import os = require('os');
// import path = require('path');
// import platform = require('vs/base/common/platform');
// import {Builder} from 'vs/base/browser/builder';
// import {EndOfLinePreference} from 'vs/editor/common/editorCommon';
// import {ICodeEditorService} from 'vs/editor/common/services/codeEditorService';
// import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
// import {IContextKey, IContextKeyService} from 'vs/platform/contextkey/common/contextkey';
// import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
// import {IMessageService, Severity} from 'vs/platform/message/common/message';
// import {IPanelService} from 'vs/workbench/services/panel/common/panelService';
// import {IPartService} from 'vs/workbench/services/part/common/partService';
// import {IStringDictionary} from 'vs/base/common/collections';
// import {ITerminalInstance, ITerminalPanel, ITerminalService, KEYBINDING_CONTEXT_TERMINAL_FOCUS, TERMINAL_PANEL_ID} from 'vs/workbench/parts/terminal/electron-browser/terminal';
// import {TerminalInstance} from 'vs/workbench/parts/terminal/electron-browser/terminalInstance';
// import {IWorkspaceContextService, IWorkspace} from 'vs/platform/workspace/common/workspace';
// import {TPromise} from 'vs/base/common/winjs.base';
// import {TerminalConfigHelper, IShell} from 'vs/workbench/parts/terminal/electron-browser/terminalConfigHelper';
// import {TerminalPanel} from 'vs/workbench/parts/terminal/electron-browser/terminalPanel';

// export class TerminalService implements ITerminalService {
// 	public _serviceBrand: any;

// 	private activeTerminalIndex: number = 0;
// 	private terminalProcesses: ITerminalProcess[] = [];
// 	private nextTerminalName: string;
// 	protected _terminalFocusContextKey: IContextKey<boolean>;

// 	private configHelper: TerminalConfigHelper;
// 	private _onActiveInstanceChanged: Emitter<string>;
// 	private _onInstancesChanged: Emitter<string>;
// 	private _onInstanceTitleChanged: Emitter<string>;

// 	constructor(
// 		@ICodeEditorService private codeEditorService: ICodeEditorService,
// 		@IConfigurationService private configurationService: IConfigurationService,
// 		@IContextKeyService private contextKeyService: IContextKeyService,
// 		@IMessageService private messageService: IMessageService,
// 		@IPanelService private panelService: IPanelService,
// 		@IPartService private partService: IPartService,
// 		@IWorkspaceContextService private contextService: IWorkspaceContextService
// 	) {
// 		this._onActiveInstanceChanged = new Emitter<string>();
// 		this._onInstancesChanged = new Emitter<string>();
// 		this._onInstanceTitleChanged = new Emitter<string>();
// 		this._terminalFocusContextKey = KEYBINDING_CONTEXT_TERMINAL_FOCUS.bindTo(this.contextKeyService);
// 	}

// 	public get onActiveInstanceChanged(): Event<string> {
// 		return this._onActiveInstanceChanged.event;
// 	}

// 	public get onInstancesChanged(): Event<string> {
// 		return this._onInstancesChanged.event;
// 	}

// 	public get onInstanceTitleChanged(): Event<string> {
// 		return this._onInstanceTitleChanged.event;
// 	}

// 	public setActiveTerminal(index: number): TPromise<any> {
// 		return this.show(false).then((terminalPanel) => {
// 			this.activeTerminalIndex = index;
// 			terminalPanel.setActiveTerminal(this.activeTerminalIndex);
// 			this._onActiveInstanceChanged.fire();
// 		});
// 	}

// 	public setActiveTerminalById(terminalId: number): void {
// 		this.setActiveTerminal(this.getTerminalIndexFromId(terminalId));
// 	}

// 	private getTerminalIndexFromId(terminalId: number): number {
// 		let terminalIndex = -1;
// 		this.terminalProcesses.forEach((terminalProcess, i) => {
// 			if (terminalProcess.process.pid === terminalId) {
// 				terminalIndex = i;
// 			}
// 		});
// 		if (terminalIndex === -1) {
// 			throw new Error(`Terminal with ID ${terminalId} does not exist (has it already been disposed?)`);
// 		}
// 		return terminalIndex;
// 	}

// 	public focusNext(): TPromise<any> {
// 		return this.focus().then((terminalPanel) => {
// 			if (this.terminalProcesses.length <= 1) {
// 				return;
// 			}
// 			this.activeTerminalIndex++;
// 			if (this.activeTerminalIndex >= this.terminalProcesses.length) {
// 				this.activeTerminalIndex = 0;
// 			}
// 			terminalPanel.setActiveTerminal(this.activeTerminalIndex);
// 			terminalPanel.focus();
// 			this._onActiveInstanceChanged.fire();
// 		});
// 	}

// 	public focusPrevious(): TPromise<any> {
// 		return this.focus().then((terminalPanel) => {
// 			if (this.terminalProcesses.length <= 1) {
// 				return;
// 			}
// 			this.activeTerminalIndex--;
// 			if (this.activeTerminalIndex < 0) {
// 				this.activeTerminalIndex = this.terminalProcesses.length - 1;
// 			}
// 			terminalPanel.setActiveTerminal(this.activeTerminalIndex);
// 			terminalPanel.focus();
// 			this._onActiveInstanceChanged.fire();
// 		});
// 	}

// 	public runSelectedText(): TPromise<any> {
// 		return this.focus().then((terminalPanel) => {
// 			let editor = this.codeEditorService.getFocusedCodeEditor();
// 			let selection = editor.getSelection();
// 			let text: string;
// 			if (selection.isEmpty()) {
// 				text = editor.getValue();
// 			} else {
// 				let endOfLinePreference = os.EOL === '\n' ? EndOfLinePreference.LF : EndOfLinePreference.CRLF;
// 				text = editor.getModel().getValueInRange(selection, endOfLinePreference);
// 			}
// 			terminalPanel.sendTextToActiveTerminal(text, true);
// 		});
// 	}

// 	public show(focus: boolean): TPromise<TerminalPanel> {
// 		return new TPromise<TerminalPanel>((complete) => {
// 			let panel = this.panelService.getActivePanel();
// 			if (!panel || panel.getId() !== TERMINAL_PANEL_ID) {
// 				return this.panelService.openPanel(TERMINAL_PANEL_ID, focus).then(() => {
// 					panel = this.panelService.getActivePanel();
// 					complete(<TerminalPanel>panel);
// 				});
// 			} else {
// 				if (focus) {
// 					panel.focus();
// 				}
// 				complete(<TerminalPanel>panel);
// 			}
// 		});
// 	}

// 	public focus(): TPromise<TerminalPanel> {
// 		return this.show(true);
// 	}

// 	public hide(): TPromise<any> {
// 		const panel = this.panelService.getActivePanel();
// 		if (panel && panel.getId() === TERMINAL_PANEL_ID) {
// 			this.partService.setPanelHidden(true);
// 		}
// 		return TPromise.as(void 0);
// 	}

// 	public hideTerminalInstance(terminalId: number): TPromise<any> {
// 		const panel = this.panelService.getActivePanel();
// 		if (panel && panel.getId() === TERMINAL_PANEL_ID) {
// 			if (this.terminalProcesses[this.getActiveTerminalIndex()].process.pid === terminalId) {
// 				this.partService.setPanelHidden(true);
// 			}
// 		}
// 		return TPromise.as(void 0);
// 	}

// 	public toggle(): TPromise<any> {
// 		const panel = this.panelService.getActivePanel();
// 		if (panel && panel.getId() === TERMINAL_PANEL_ID) {
// 			this.partService.setPanelHidden(true);
// 			return TPromise.as(null);
// 		}
// 		return this.focus();
// 	}

// 	public createNew(name?: string): TPromise<number> {
// 		let processCount = this.terminalProcesses.length;

// 		// When there are 0 processes it means that the panel is not yet created, so the name needs
// 		// to be stored for when createNew is called from TerminalPanel.create. This has to work
// 		// like this as TerminalPanel.setVisible must create a terminal if there is none due to how
// 		// the TerminalPanel is restored on launch if it was open previously.

// 		if (processCount === 0 && !name) {
// 			name = this.nextTerminalName;
// 			this.nextTerminalName = undefined;
// 		} else {
// 			this.nextTerminalName = name;
// 		}

// 		return this.focus().then((terminalPanel) => {
// 			// If the terminal panel has not been initialized yet skip this, the terminal will be
// 			// created via a call from TerminalPanel.setVisible
// 			if (terminalPanel === null) {
// 				return;
// 			}

// 			// Only create a new process if none have been created since toggling the terminal
// 			// panel. This happens when createNew is called when the panel is either empty or no yet
// 			// created.
// 			if (processCount !== this.terminalProcesses.length) {
// 				return TPromise.as(this.terminalProcesses[this.terminalProcesses.length - 1].process.pid);
// 			}

// 			this.initConfigHelper(terminalPanel.getContainer());
// 			return terminalPanel.createNewTerminalInstance(this.createTerminalProcess(name), this._terminalFocusContextKey).then((terminalId) => {
// 				this._onInstancesChanged.fire();
// 				return TPromise.as(terminalId);
// 			});
// 		});
// 	}

// 	public close(): TPromise<any> {
// 		return this.focus().then((terminalPanel) => {
// 			return terminalPanel.closeActiveTerminal();
// 		});
// 	}

// 	public closeById(terminalId: number): TPromise<any> {
// 		return this.show(false).then((terminalPanel) => {
// 			return terminalPanel.closeTerminalById(terminalId);
// 		});
// 	}

// 	public copySelection(): TPromise<any> {
// 		if (document.activeElement.classList.contains('xterm')) {
// 			document.execCommand('copy');
// 		} else {
// 			this.messageService.show(Severity.Warning, nls.localize('terminal.integrated.copySelection.noSelection', 'Cannot copy terminal selection when terminal does not have focus'));
// 		}
// 		return TPromise.as(void 0);
// 	}

// 	public paste(): TPromise<any> {
// 		return this.focus().then(() => {
// 			document.execCommand('paste');
// 		});
// 	}

// 	public scrollDown(): TPromise<any> {
// 		return this.focus().then((terminalPanel) => {
// 			terminalPanel.scrollDown();
// 		});
// 	}

// 	public scrollUp(): TPromise<any> {
// 		return this.focus().then((terminalPanel) => {
// 			terminalPanel.scrollUp();
// 		});
// 	}

// 	public getActiveTerminalIndex(): number {
// 		return this.activeTerminalIndex;
// 	}

// 	public getTerminalInstanceTitles(): string[] {
// 		return this.terminalProcesses.map((process, index) => `${index + 1}: ${process.title}`);
// 	}

// 	public initConfigHelper(panelElement: Builder): void {
// 		if (!this.configHelper) {
// 			this.configHelper = new TerminalConfigHelper(platform.platform, this.configurationService, panelElement);
// 		}
// 	}

// 	public killTerminalProcess(terminalProcess: ITerminalProcess): void {
// 		if (terminalProcess.process.connected) {
// 			terminalProcess.process.disconnect();
// 			terminalProcess.process.kill();
// 		}

// 		let index = this.terminalProcesses.indexOf(terminalProcess);
// 		if (index >= 0) {
// 			let wasActiveTerminal = (index === this.getActiveTerminalIndex());
// 			// Push active index back if the closed process was before the active process
// 			if (this.getActiveTerminalIndex() >= index) {
// 				this.activeTerminalIndex = Math.max(0, this.activeTerminalIndex - 1);
// 			}
// 			this.terminalProcesses.splice(index, 1);
// 			this._onInstancesChanged.fire();
// 			if (wasActiveTerminal) {
// 				this._onActiveInstanceChanged.fire();
// 			}
// 		}
// 	}

// 	private createTerminalProcess(name?: string): ITerminalProcess {
// 		let locale = this.configHelper.isSetLocaleVariables() ? platform.locale : undefined;
// 		let env = TerminalService.createTerminalEnv(process.env, this.configHelper.getShell(), this.contextService.getWorkspace(), locale);
// 		let terminalProcess = {
// 			title: name ? name : '',
// 			process: cp.fork('./terminalProcess', [], {
// 				env: env,
// 				cwd: URI.parse(path.dirname(require.toUrl('./terminalProcess'))).fsPath
// 			})
// 		};
// 		this.terminalProcesses.push(terminalProcess);
// 		this._onInstancesChanged.fire();
// 		this.activeTerminalIndex = this.terminalProcesses.length - 1;
// 		this._onActiveInstanceChanged.fire();
// 		if (!name) {
// 			// Only listen for process title changes when a name is not provided
// 			terminalProcess.process.on('message', (message) => {
// 				if (message.type === 'title') {
// 					terminalProcess.title = message.content ? message.content : '';
// 					this._onInstanceTitleChanged.fire();
// 				}
// 			});
// 		}
// 		return terminalProcess;
// 	}

// 	public static createTerminalEnv(parentEnv: IStringDictionary<string>, shell: IShell, workspace: IWorkspace, locale?: string): IStringDictionary<string> {
// 		let env = this.cloneEnv(parentEnv);
// 		env['PTYPID'] = process.pid.toString();
// 		env['PTYSHELL'] = shell.executable;
// 		shell.args.forEach((arg, i) => {
// 			env[`PTYSHELLARG${i}`] = arg;
// 		});
// 		env['PTYCWD'] = this.sanitizeCwd(workspace ? workspace.resource.fsPath : os.homedir());
// 		if (locale) {
// 			env['LANG'] = this.getLangEnvVariable(locale);
// 		}
// 		return env;
// 	}

// 	private static sanitizeCwd(cwd: string) {
// 		// Make the drive letter uppercase on Windows (see #9448)
// 		if (platform.platform === platform.Platform.Windows && cwd && cwd[1] === ':') {
// 			return cwd[0].toUpperCase() + cwd.substr(1);
// 		}
// 		return cwd;
// 	}

// 	private static cloneEnv(env: IStringDictionary<string>): IStringDictionary<string> {
// 		let newEnv: IStringDictionary<string> = Object.create(null);
// 		Object.keys(env).forEach((key) => {
// 			newEnv[key] = env[key];
// 		});
// 		return newEnv;
// 	}

// 	private static getLangEnvVariable(locale: string) {
// 		const parts = locale.split('-');
// 		const n = parts.length;
// 		if (n > 1) {
// 			parts[n - 1] = parts[n - 1].toUpperCase();
// 		}
// 		return parts.join('_') + '.UTF-8';
// 	}
// }