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

import DOM = require('vs/base/browser/dom');
J
Johannes Rieken 已提交
7
import Event, { Emitter } from 'vs/base/common/event';
8 9
import URI from 'vs/base/common/uri';
import cp = require('child_process');
10
import lifecycle = require('vs/base/common/lifecycle');
C
Christof Marti 已提交
11
import nls = require('vs/nls');
C
Christof Marti 已提交
12
import os = require('os');
13 14
import path = require('path');
import platform = require('vs/base/common/platform');
15
import xterm = require('xterm');
16
import { Dimension } from 'vs/base/browser/builder';
17
import { IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
18 19
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IMessageService, Severity } from 'vs/platform/message/common/message';
20
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
21
import { IStringDictionary } from 'vs/base/common/collections';
22
import { ITerminalInstance, KEYBINDING_CONTEXT_TERMINAL_TEXT_SELECTED, TERMINAL_PANEL_ID, IShell } from 'vs/workbench/parts/terminal/common/terminal';
23
import { IWorkspace, IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
24 25
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { TabFocus } from 'vs/editor/common/config/commonEditorConfig';
26
import { TerminalConfigHelper } from 'vs/workbench/parts/terminal/electron-browser/terminalConfigHelper';
27

D
Daniel Imms 已提交
28 29 30
/** The amount of time to consider terminal errors to be related to the launch */
const LAUNCHING_DURATION = 500;

31
export class TerminalInstance implements ITerminalInstance {
32
	private static readonly EOL_REGEX = /\r?\n/g;
C
Christof Marti 已提交
33

D
Daniel Imms 已提交
34 35
	private static _idCounter = 1;

36
	private _id: number;
D
Daniel Imms 已提交
37
	private _isExiting: boolean;
K
Kai Wood 已提交
38
	private _hadFocusOnExit: boolean;
39
	private _isLaunching: boolean;
D
Daniel Imms 已提交
40
	private _isVisible: boolean;
41
	private _onDisposed: Emitter<TerminalInstance>;
42
	private _onProcessIdReady: Emitter<TerminalInstance>;
D
Daniel Imms 已提交
43
	private _onTitleChanged: Emitter<string>;
D
Daniel Imms 已提交
44
	private _process: cp.ChildProcess;
45
	private _processId: number;
D
Daniel Imms 已提交
46
	private _skipTerminalCommands: string[];
D
Daniel Imms 已提交
47 48 49 50 51
	private _title: string;
	private _toDispose: lifecycle.IDisposable[];
	private _wrapperElement: HTMLDivElement;
	private _xterm: any;
	private _xtermElement: HTMLDivElement;
52
	private _terminalHasTextContextKey: IContextKey<boolean>;
D
Daniel Imms 已提交
53

54
	public get id(): number { return this._id; }
55
	public get processId(): number { return this._processId; }
56
	public get onClosed(): Event<TerminalInstance> { return this._onDisposed.event; }
57
	public get onProcessIdReady(): Event<TerminalInstance> { return this._onProcessIdReady.event; }
D
Daniel Imms 已提交
58
	public get onTitleChanged(): Event<string> { return this._onTitleChanged.event; }
D
Daniel Imms 已提交
59
	public get title(): string { return this._title; }
K
Kai Wood 已提交
60
	public get hadFocusOnExit(): boolean { return this._hadFocusOnExit; }
61 62

	public constructor(
D
Daniel Imms 已提交
63 64 65
		private _terminalFocusContextKey: IContextKey<boolean>,
		private _configHelper: TerminalConfigHelper,
		private _container: HTMLElement,
66
		name: string,
P
Pine Wu 已提交
67
		shell: IShell,
68
		@IContextKeyService private _contextKeyService: IContextKeyService,
D
Daniel Imms 已提交
69
		@IKeybindingService private _keybindingService: IKeybindingService,
70
		@IMessageService private _messageService: IMessageService,
71 72
		@IPanelService private _panelService: IPanelService,
		@IWorkspaceContextService private _contextService: IWorkspaceContextService
73
	) {
D
Daniel Imms 已提交
74
		this._toDispose = [];
D
Daniel Imms 已提交
75
		this._skipTerminalCommands = [];
D
Daniel Imms 已提交
76
		this._isExiting = false;
K
Kai Wood 已提交
77
		this._hadFocusOnExit = false;
78
		this._isLaunching = true;
D
Daniel Imms 已提交
79 80
		this._isVisible = false;
		this._id = TerminalInstance._idCounter++;
81
		this._terminalHasTextContextKey = KEYBINDING_CONTEXT_TERMINAL_TEXT_SELECTED.bindTo(this._contextKeyService);
D
Daniel Imms 已提交
82

83
		this._onDisposed = new Emitter<TerminalInstance>();
84 85
		this._onProcessIdReady = new Emitter<TerminalInstance>();
		this._onTitleChanged = new Emitter<string>();
86

87
		this._createProcess(this._contextService.getWorkspace(), name, shell);
D
Daniel Imms 已提交
88 89 90

		if (_container) {
			this.attachToElement(_container);
91 92 93
		}
	}

D
Daniel Imms 已提交
94
	public addDisposable(disposable: lifecycle.IDisposable): void {
D
Daniel Imms 已提交
95
		this._toDispose.push(disposable);
D
Daniel Imms 已提交
96 97
	}

98
	public attachToElement(container: HTMLElement): void {
D
Daniel Imms 已提交
99
		if (this._wrapperElement) {
100 101 102
			throw new Error('The terminal instance has already been attached to a container');
		}

D
Daniel Imms 已提交
103 104 105 106
		this._container = container;
		this._wrapperElement = document.createElement('div');
		DOM.addClass(this._wrapperElement, 'terminal-wrapper');
		this._xtermElement = document.createElement('div');
107

D
Daniel Imms 已提交
108 109
		this._xterm = xterm();
		this._xterm.open(this._xtermElement);
110

D
Daniel Imms 已提交
111
		this._process.on('message', (message) => {
D
Daniel Imms 已提交
112 113 114
			if (!this._xterm) {
				return;
			}
D
Daniel Imms 已提交
115
			if (message.type === 'data') {
D
Daniel Imms 已提交
116
				this._xterm.write(message.content);
D
Daniel Imms 已提交
117 118
			}
		});
D
Daniel Imms 已提交
119 120
		this._xterm.on('data', (data) => {
			this._process.send({
D
Daniel Imms 已提交
121
				event: 'input',
D
Daniel Imms 已提交
122
				data: this._sanitizeInput(data)
D
Daniel Imms 已提交
123 124 125
			});
			return false;
		});
D
Daniel Imms 已提交
126
		this._xterm.attachCustomKeydownHandler((event: KeyboardEvent) => {
127 128
			// Skip processing by xterm.js of keyboard events that resolve to commands described
			// within commandsToSkipShell
D
Daniel Imms 已提交
129
			const standardKeyboardEvent = new StandardKeyboardEvent(event);
130
			const keybinding = standardKeyboardEvent.toKeybinding();
D
Daniel Imms 已提交
131 132
			const resolveResult = this._keybindingService.resolve(keybinding, standardKeyboardEvent.target);
			if (resolveResult && this._skipTerminalCommands.some(k => k === resolveResult.commandId)) {
D
Daniel Imms 已提交
133 134 135 136 137 138 139 140 141
				event.preventDefault();
				return false;
			}

			// If tab focus mode is on, tab is not passed to the terminal
			if (TabFocus.getTabFocusMode() && event.keyCode === 9) {
				return false;
			}
		});
142 143 144 145
		(<HTMLElement>this._xterm.element).addEventListener('mouseup', event => {
			// Wait until mouseup has propogated through the DOM before evaluating the new selection
			// state.
			setTimeout(() => {
146 147 148 149 150 151 152 153 154 155
				this._refreshSelectionContextKey();
			}, 0);
		});

		// xterm.js currently drops selection on keyup as we need to handle this case.
		(<HTMLElement>this._xterm.element).addEventListener('keyup', event => {
			// Wait until keyup has propogated through the DOM before evaluating the new selection
			// state.
			setTimeout(() => {
				this._refreshSelectionContextKey();
156 157
			}, 0);
		});
D
Daniel Imms 已提交
158

D
Daniel Imms 已提交
159
		let xtermHelper: HTMLElement = this._xterm.element.querySelector('.xterm-helpers');
160 161 162 163 164 165 166 167 168 169 170
		let focusTrap: HTMLElement = document.createElement('div');
		focusTrap.setAttribute('tabindex', '0');
		DOM.addClass(focusTrap, 'focus-trap');
		focusTrap.addEventListener('focus', function (event: FocusEvent) {
			let currentElement = focusTrap;
			while (!DOM.hasClass(currentElement, 'part')) {
				currentElement = currentElement.parentElement;
			}
			let hidePanelElement = <HTMLElement>currentElement.querySelector('.hide-panel-action');
			hidePanelElement.focus();
		});
D
Daniel Imms 已提交
171
		xtermHelper.insertBefore(focusTrap, this._xterm.textarea);
172

D
Daniel Imms 已提交
173 174
		this._toDispose.push(DOM.addDisposableListener(this._xterm.textarea, 'focus', (event: KeyboardEvent) => {
			this._terminalFocusContextKey.set(true);
175
		}));
D
Daniel Imms 已提交
176 177
		this._toDispose.push(DOM.addDisposableListener(this._xterm.textarea, 'blur', (event: KeyboardEvent) => {
			this._terminalFocusContextKey.reset();
178
			this._refreshSelectionContextKey();
179
		}));
D
Daniel Imms 已提交
180 181
		this._toDispose.push(DOM.addDisposableListener(this._xterm.element, 'focus', (event: KeyboardEvent) => {
			this._terminalFocusContextKey.set(true);
182
		}));
D
Daniel Imms 已提交
183 184
		this._toDispose.push(DOM.addDisposableListener(this._xterm.element, 'blur', (event: KeyboardEvent) => {
			this._terminalFocusContextKey.reset();
185
			this._refreshSelectionContextKey();
186 187
		}));

D
Daniel Imms 已提交
188 189
		this._wrapperElement.appendChild(this._xtermElement);
		this._container.appendChild(this._wrapperElement);
190

191 192 193 194
		const computedStyle = window.getComputedStyle(this._container);
		const width = parseInt(computedStyle.getPropertyValue('width').replace('px', ''), 10);
		const height = parseInt(computedStyle.getPropertyValue('height').replace('px', ''), 10);
		this.layout(new Dimension(width, height));
D
Daniel Imms 已提交
195
		this.setVisible(this._isVisible);
196
		this.updateConfig();
197 198
	}

199 200 201 202
	public hasSelection(): boolean {
		return !document.getSelection().isCollapsed;
	}

D
Daniel Imms 已提交
203
	public copySelection(): void {
D
Daniel Imms 已提交
204 205 206
		if (document.activeElement.classList.contains('xterm')) {
			document.execCommand('copy');
		} else {
D
Daniel Imms 已提交
207
			this._messageService.show(Severity.Warning, nls.localize('terminal.integrated.copySelection.noSelection', 'Cannot copy terminal selection when terminal does not have focus'));
D
Daniel Imms 已提交
208
		}
D
Daniel Imms 已提交
209 210
	}

211 212 213 214
	public clearSelection(): void {
		document.getSelection().empty();
	}

D
Daniel Imms 已提交
215
	public dispose(): void {
216
		this._isExiting = true;
K
Kai Wood 已提交
217 218 219 220

		if (this._xterm && this._xterm.element) {
			this._hadFocusOnExit = DOM.hasClass(this._xterm.element, 'focus');
		}
D
Daniel Imms 已提交
221 222 223
		if (this._wrapperElement) {
			this._container.removeChild(this._wrapperElement);
			this._wrapperElement = null;
D
Daniel Imms 已提交
224
		}
D
Daniel Imms 已提交
225 226 227
		if (this._xterm) {
			this._xterm.destroy();
			this._xterm = null;
D
Daniel Imms 已提交
228
		}
D
Daniel Imms 已提交
229 230 231
		if (this._process) {
			if (this._process.connected) {
				this._process.kill();
D
Daniel Imms 已提交
232
			}
D
Daniel Imms 已提交
233
			this._process = null;
D
Daniel Imms 已提交
234
		}
235
		this._onDisposed.fire(this);
D
Daniel Imms 已提交
236
		this._toDispose = lifecycle.dispose(this._toDispose);
D
Daniel Imms 已提交
237 238
	}

D
Daniel Imms 已提交
239
	public focus(force?: boolean): void {
D
Daniel Imms 已提交
240
		if (!this._xterm) {
D
Daniel Imms 已提交
241 242 243 244
			return;
		}
		let text = window.getSelection().toString();
		if (!text || force) {
D
Daniel Imms 已提交
245
			this._xterm.focus();
D
Daniel Imms 已提交
246
		}
D
Daniel Imms 已提交
247 248 249
	}

	public paste(): void {
D
Daniel Imms 已提交
250 251
		this.focus();
		document.execCommand('paste');
D
Daniel Imms 已提交
252 253 254
	}

	public sendText(text: string, addNewLine: boolean): void {
D
Daniel Imms 已提交
255 256 257
		if (addNewLine && text.substr(text.length - os.EOL.length) !== os.EOL) {
			text += os.EOL;
		}
D
Daniel Imms 已提交
258
		this._process.send({
D
Daniel Imms 已提交
259 260 261
			event: 'input',
			data: text
		});
D
Daniel Imms 已提交
262
	}
263 264

	public setVisible(visible: boolean): void {
D
Daniel Imms 已提交
265 266 267
		this._isVisible = visible;
		if (this._wrapperElement) {
			DOM.toggleClass(this._wrapperElement, 'active', visible);
268
		}
269 270
	}

271
	public scrollDownLine(): void {
D
Daniel Imms 已提交
272
		this._xterm.scrollDisp(1);
D
Daniel Imms 已提交
273 274
	}

275
	public scrollDownPage(): void {
276
		this._xterm.scrollPages(1);
277 278
	}

279 280 281 282
	public scrollToBottom(): void {
		this._xterm.scrollToBottom();
	}

283
	public scrollUpLine(): void {
D
Daniel Imms 已提交
284
		this._xterm.scrollDisp(-1);
D
Daniel Imms 已提交
285
	}
286

287
	public scrollUpPage(): void {
288
		this._xterm.scrollPages(-1);
289 290
	}

291 292 293 294
	public scrollToTop(): void {
		this._xterm.scrollToTop();
	}

D
Daniel Imms 已提交
295 296 297 298
	public clear(): void {
		this._xterm.clear();
	}

299
	private _refreshSelectionContextKey() {
300 301 302
		const activePanel = this._panelService.getActivePanel();
		const isFocused = activePanel && activePanel.getId() === TERMINAL_PANEL_ID;
		this._terminalHasTextContextKey.set(isFocused && !window.getSelection().isCollapsed);
303 304
	}

D
Daniel Imms 已提交
305
	private _sanitizeInput(data: any) {
306
		return typeof data === 'string' ? data.replace(TerminalInstance.EOL_REGEX, os.EOL) : data;
C
Christof Marti 已提交
307 308
	}

D
Daniel Imms 已提交
309
	protected _getCwd(workspace: IWorkspace, ignoreCustomCwd: boolean): string {
B
Benjamin Pasero 已提交
310
		let cwd: string;
D
Daniel Imms 已提交
311 312

		// TODO: Handle non-existent customCwd
D
Daniel Imms 已提交
313
		if (!ignoreCustomCwd) {
314
			// Evaluate custom cwd first
D
Daniel Imms 已提交
315
			const customCwd = this._configHelper.getCwd();
316 317 318 319 320 321
			if (customCwd) {
				if (path.isAbsolute(customCwd)) {
					cwd = customCwd;
				} else if (workspace) {
					cwd = path.normalize(path.join(workspace.resource.fsPath, customCwd));
				}
D
Daniel Imms 已提交
322 323 324 325 326
			}
		}

		// If there was no custom cwd or it was relative with no workspace
		if (!cwd) {
327
			cwd = workspace ? workspace.resource.fsPath : os.homedir();
D
Daniel Imms 已提交
328 329 330 331 332
		}

		return TerminalInstance._sanitizeCwd(cwd);
	}

333
	protected _createProcess(workspace: IWorkspace, name: string, shell: IShell) {
D
Daniel Imms 已提交
334
		let locale = this._configHelper.isSetLocaleVariables() ? platform.locale : undefined;
D
Daniel Imms 已提交
335
		if (!shell.executable) {
D
Daniel Imms 已提交
336
			shell = this._configHelper.getShell();
P
Pine Wu 已提交
337
		}
D
Daniel Imms 已提交
338
		let env = TerminalInstance.createTerminalEnv(process.env, shell, this._getCwd(workspace, shell.ignoreCustomCwd), locale);
339
		this._title = name ? name : '';
D
Daniel Imms 已提交
340
		this._process = cp.fork('./terminalProcess', [], {
341 342 343 344 345
			env: env,
			cwd: URI.parse(path.dirname(require.toUrl('./terminalProcess'))).fsPath
		});
		if (!name) {
			// Only listen for process title changes when a name is not provided
D
Daniel Imms 已提交
346
			this._process.on('message', (message) => {
347
				if (message.type === 'title') {
D
Daniel Imms 已提交
348
					this._title = message.content ? message.content : '';
349
					this._onTitleChanged.fire(this._title);
350
				}
351 352
			});
		}
353 354 355 356 357 358
		this._process.on('message', (message) => {
			if (message.type === 'pid') {
				this._processId = message.content;
				this._onProcessIdReady.fire(this);
			}
		});
B
Benjamin Pasero 已提交
359
		this._process.on('exit', (exitCode: number) => {
360
			// Prevent dispose functions being triggered multiple times
D
Daniel Imms 已提交
361
			if (!this._isExiting) {
362 363
				this.dispose();
				if (exitCode) {
364 365 366 367 368 369
					if (this._isLaunching) {
						const args = shell.args && shell.args.length ? ' ' + shell.args.map(a => a.indexOf(' ') !== -1 ? `'${a}'` : a).join(' ') : '';
						this._messageService.show(Severity.Error, nls.localize('terminal.integrated.launchFailed', 'The terminal process command `{0}{1}` failed to launch (exit code: {2})', shell.executable, args, exitCode));
					} else {
						this._messageService.show(Severity.Error, nls.localize('terminal.integrated.exitedWithCode', 'The terminal process terminated with exit code: {0}', exitCode));
					}
370 371 372
				}
			}
		});
373 374
		setTimeout(() => {
			this._isLaunching = false;
D
Daniel Imms 已提交
375
		}, LAUNCHING_DURATION);
376 377
	}

378 379
	// TODO: This should be private/protected
	// TODO: locale should not be optional
D
Daniel Imms 已提交
380
	public static createTerminalEnv(parentEnv: IStringDictionary<string>, shell: IShell, cwd: string, locale?: string): IStringDictionary<string> {
D
Daniel Imms 已提交
381
		let env = TerminalInstance._cloneEnv(parentEnv);
382 383
		env['PTYPID'] = process.pid.toString();
		env['PTYSHELL'] = shell.executable;
D
Daniel Imms 已提交
384 385 386 387 388
		if (shell.args) {
			shell.args.forEach((arg, i) => {
				env[`PTYSHELLARG${i}`] = arg;
			});
		}
D
Daniel Imms 已提交
389
		env['PTYCWD'] = cwd;
390
		if (locale) {
D
Daniel Imms 已提交
391
			env['LANG'] = TerminalInstance._getLangEnvVariable(locale);
392 393
		}
		return env;
394 395
	}

D
Daniel Imms 已提交
396
	private static _sanitizeCwd(cwd: string) {
397 398 399
		// 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);
D
Daniel Imms 已提交
400
		}
401
		return cwd;
D
Daniel Imms 已提交
402 403
	}

D
Daniel Imms 已提交
404
	private static _cloneEnv(env: IStringDictionary<string>): IStringDictionary<string> {
405 406 407
		let newEnv: IStringDictionary<string> = Object.create(null);
		Object.keys(env).forEach((key) => {
			newEnv[key] = env[key];
408
		});
409
		return newEnv;
410 411
	}

D
Daniel Imms 已提交
412
	private static _getLangEnvVariable(locale: string) {
413 414 415 416
		const parts = locale.split('-');
		const n = parts.length;
		if (n > 1) {
			parts[n - 1] = parts[n - 1].toUpperCase();
D
Daniel Imms 已提交
417
		}
418
		return parts.join('_') + '.UTF-8';
D
Daniel Imms 已提交
419
	}
D
Daniel Imms 已提交
420

421 422 423 424 425 426 427
	public updateConfig(): void {
		this._setCursorBlink(this._configHelper.getCursorBlink());
		this._setCommandsToSkipShell(this._configHelper.getCommandsToSkipShell());
		this._setScrollback(this._configHelper.getScrollback());
	}

	private _setCursorBlink(blink: boolean): void {
D
Daniel Imms 已提交
428
		if (this._xterm && this._xterm.getOption('cursorBlink') !== blink) {
D
Daniel Imms 已提交
429
			this._xterm.setOption('cursorBlink', blink);
D
Daniel Imms 已提交
430
			this._xterm.refresh(0, this._xterm.rows - 1);
D
Daniel Imms 已提交
431 432 433
		}
	}

434
	private _setCommandsToSkipShell(commands: string[]): void {
D
Daniel Imms 已提交
435
		this._skipTerminalCommands = commands;
D
Daniel Imms 已提交
436 437
	}

438
	private _setScrollback(lineCount: number): void {
D
Daniel Imms 已提交
439 440 441 442 443
		if (this._xterm && this._xterm.getOption('scrollback') !== lineCount) {
			this._xterm.setOption('scrollback', lineCount);
		}
	}

444
	public layout(dimension: { width: number, height: number }): void {
D
Daniel Imms 已提交
445
		let font = this._configHelper.getFont();
446
		if (!font || !font.charWidth || !font.charHeight) {
D
Daniel Imms 已提交
447 448 449 450
			return;
		}
		if (!dimension.height) { // Minimized
			return;
451 452 453 454 455 456
		} else {
			// Trigger scroll event manually so that the viewport's scroll area is synced. This
			// needs to happen otherwise its scrollTop value is invalid when the panel is toggled as
			// it gets removed and then added back to the DOM (resetting scrollTop to 0).
			// Upstream issue: https://github.com/sourcelair/xterm.js/issues/291
			this._xterm.emit('scroll', this._xterm.ydisp);
D
Daniel Imms 已提交
457 458 459
		}
		let leftPadding = parseInt(getComputedStyle(document.querySelector('.terminal-outer-container')).paddingLeft.split('px')[0], 10);
		let innerWidth = dimension.width - leftPadding;
460 461
		let cols = Math.floor(innerWidth / font.charWidth);
		let rows = Math.floor(dimension.height / font.charHeight);
D
Daniel Imms 已提交
462 463 464
		if (this._xterm) {
			this._xterm.resize(cols, rows);
			this._xterm.element.style.width = innerWidth + 'px';
D
Daniel Imms 已提交
465
		}
D
Daniel Imms 已提交
466 467
		if (this._process.connected) {
			this._process.send({
D
Daniel Imms 已提交
468 469 470 471 472 473
				event: 'resize',
				cols: cols,
				rows: rows
			});
		}
	}
474
}