terminal.ts 15.3 KB
Newer Older
D
Daniel Imms 已提交
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.
 *--------------------------------------------------------------------------------------------*/
'use strict';

M
Matt Bierner 已提交
7
import { Event } from 'vs/base/common/event';
D
Dirk Baeumer 已提交
8
import { IDisposable } from 'vs/base/common/lifecycle';
D
Daniel Imms 已提交
9
import { RawContextKey, ContextKeyExpr, IContextKey } from 'vs/platform/contextkey/common/contextkey';
10 11
import { TPromise } from 'vs/base/common/winjs.base';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
D
Daniel Imms 已提交
12 13 14 15 16

export const TERMINAL_PANEL_ID = 'workbench.panel.terminal';

export const TERMINAL_SERVICE_ID = 'terminalService';

17
/**  A context key that is set when the integrated terminal has focus. */
A
Alex Dima 已提交
18
export const KEYBINDING_CONTEXT_TERMINAL_FOCUS = new RawContextKey<boolean>('terminalFocus', undefined);
19
/**  A context key that is set when the integrated terminal does not have focus. */
J
Johannes Rieken 已提交
20
export const KEYBINDING_CONTEXT_TERMINAL_NOT_FOCUSED: ContextKeyExpr = KEYBINDING_CONTEXT_TERMINAL_FOCUS.toNegated();
21

22 23 24 25 26
/** A keybinding context key that is set when the integrated terminal has text selected. */
export const KEYBINDING_CONTEXT_TERMINAL_TEXT_SELECTED = new RawContextKey<boolean>('terminalTextSelected', undefined);
/** A keybinding context key that is set when the integrated terminal does not have text selected. */
export const KEYBINDING_CONTEXT_TERMINAL_TEXT_NOT_SELECTED: ContextKeyExpr = KEYBINDING_CONTEXT_TERMINAL_TEXT_SELECTED.toNegated();

R
rebornix 已提交
27 28 29 30
/**  A context key that is set when the find widget in integrated terminal is visible. */
export const KEYBINDING_CONTEXT_TERMINAL_FIND_WIDGET_VISIBLE = new RawContextKey<boolean>('terminalFindWidgetVisible', undefined);
/**  A context key that is set when the find widget in integrated terminal is not visible. */
export const KEYBINDING_CONTEXT_TERMINAL_FIND_WIDGET_NOT_VISIBLE: ContextKeyExpr = KEYBINDING_CONTEXT_TERMINAL_FIND_WIDGET_VISIBLE.toNegated();
31 32 33 34
/**  A context key that is set when the find widget find input in integrated terminal is focused. */
export const KEYBINDING_CONTEXT_TERMINAL_FIND_WIDGET_INPUT_FOCUSED = new RawContextKey<boolean>('terminalFindWidgetInputFocused', false);
/**  A context key that is set when the find widget find input in integrated terminal is not focused. */
export const KEYBINDING_CONTEXT_TERMINAL_FIND_WIDGET_INPUT_NOT_FOCUSED: ContextKeyExpr = KEYBINDING_CONTEXT_TERMINAL_FIND_WIDGET_INPUT_FOCUSED.toNegated();
R
rebornix 已提交
35

36
export const IS_WORKSPACE_SHELL_ALLOWED_STORAGE_KEY = 'terminal.integrated.isWorkspaceShellAllowed';
37 38
export const NEVER_SUGGEST_SELECT_WINDOWS_SHELL_STORAGE_KEY = 'terminal.integrated.neverSuggestSelectWindowsShell';

B
Benjamin Pasero 已提交
39
export const ITerminalService = createDecorator<ITerminalService>(TERMINAL_SERVICE_ID);
D
Daniel Imms 已提交
40

41 42 43 44 45 46
export const TerminalCursorStyle = {
	BLOCK: 'block',
	LINE: 'line',
	UNDERLINE: 'underline'
};

47 48
export const TERMINAL_CONFIG_SECTION = 'terminal.integrated';

49 50
export type FontWeight = 'normal' | 'bold' | '100' | '200' | '300' | '400' | '500' | '600' | '700' | '800' | '900';

51
export interface ITerminalConfiguration {
D
Daniel Imms 已提交
52 53 54 55
	shell: {
		linux: string;
		osx: string;
		windows: string;
56
	};
D
Daniel Imms 已提交
57 58 59 60 61
	shellArgs: {
		linux: string[];
		osx: string[];
		windows: string[];
	};
D
Daniel Imms 已提交
62
	macOptionIsMeta: boolean;
63
	rightClickBehavior: 'default' | 'copyPaste' | 'selectWord';
D
Daniel Imms 已提交
64 65 66
	cursorBlinking: boolean;
	cursorStyle: string;
	fontFamily: string;
67 68
	fontWeight: FontWeight;
	fontWeightBold: FontWeight;
69
	// fontLigatures: boolean;
D
Daniel Imms 已提交
70 71 72 73 74 75 76
	fontSize: number;
	lineHeight: number;
	setLocaleVariables: boolean;
	scrollback: number;
	commandsToSkipShell: string[];
	cwd: string;
	confirmOnExit: boolean;
D
Daniel Imms 已提交
77
	enableBell: boolean;
78 79 80 81 82
	env: {
		linux: { [key: string]: string };
		osx: { [key: string]: string };
		windows: { [key: string]: string };
	};
B
bpceee 已提交
83
	showExitAlert: boolean;
84
	experimentalRestore: boolean;
85 86
}

87
export interface ITerminalConfigHelper {
88
	config: ITerminalConfiguration;
89
	getFont(): ITerminalFont;
90 91 92 93
	/**
	 * Merges the default shell path and args into the provided launch configuration
	 */
	mergeDefaultShellPathAndArgs(shell: IShellLaunchConfig): void;
94 95
	/** Sets whether a workspace shell configuration is allowed or not */
	setWorkspaceShellAllowed(isAllowed: boolean): void;
96 97 98 99
}

export interface ITerminalFont {
	fontFamily: string;
D
Daniel Imms 已提交
100
	fontSize: number;
101
	lineHeight: number;
102 103
	charWidth?: number;
	charHeight?: number;
104 105
}

106
export interface IShellLaunchConfig {
A
Andre Weinand 已提交
107
	/** The name of the terminal, if this is not set the name of the process will be used. */
108 109 110
	name?: string;
	/** The shell executable (bash, cmd, etc.). */
	executable?: string;
111 112
	/**
	 * The CLI arguments to use with executable, a string[] is in argv format and will be escaped,
D
Daniel Imms 已提交
113 114
	 * a string is in "CommandLine" pre-escaped format and will be used as is. The string option is
	 * only supported on Windows and will throw an exception if used on macOS or Linux.
115 116
	 */
	args?: string[] | string;
117 118 119 120 121
	/**
	 * The current working directory of the terminal, this overrides the `terminal.integrated.cwd`
	 * settings key.
	 */
	cwd?: string;
122 123 124 125 126
	/**
	 * A custom environment for the terminal, if this is not set the environment will be inherited
	 * from the VS Code process.
	 */
	env?: { [key: string]: string };
127 128 129 130 131
	/**
	 * Whether to ignore a custom cwd from the `terminal.integrated.cwd` settings key (eg. if the
	 * shell is being launched by an extension).
	 */
	ignoreConfigurationCwd?: boolean;
132

133
	/** Whether to wait for a key press before closing the terminal. */
134 135
	waitOnExit?: boolean | string;

136 137 138 139 140 141 142
	/**
	 * A string including ANSI escape sequences that will be written to the terminal emulator
	 * _before_ the terminal process has launched, a trailing \n is added at the end of the string.
	 * This allows for example the terminal instance to display a styled message as the first line
	 * of the terminal. Use \x1b over \033 or \e for the escape control character.
	 */
	initialText?: string;
143 144
}

D
Daniel Imms 已提交
145
export interface ITerminalService {
146
	_serviceBrand: any;
147

D
Daniel Imms 已提交
148
	activeTabIndex: number;
149
	configHelper: ITerminalConfigHelper;
D
Daniel Imms 已提交
150
	onActiveTabChanged: Event<void>;
151
	onTabDisposed: Event<ITerminalTab>;
152
	onInstanceCreated: Event<ITerminalInstance>;
153
	onInstanceDisposed: Event<ITerminalInstance>;
154
	onInstanceProcessIdReady: Event<ITerminalInstance>;
155
	onInstancesChanged: Event<void>;
156
	onInstanceTitleChanged: Event<string>;
157
	terminalInstances: ITerminalInstance[];
D
Daniel Imms 已提交
158
	terminalTabs: ITerminalTab[];
159

160
	createInstance(shell?: IShellLaunchConfig, wasNewTerminalAction?: boolean): ITerminalInstance;
161
	getInstanceFromId(terminalId: number): ITerminalInstance;
T
t-amqi 已提交
162
	getInstanceFromIndex(terminalIndex: number): ITerminalInstance;
D
Daniel Imms 已提交
163
	getTabLabels(): string[];
164 165 166
	getActiveInstance(): ITerminalInstance;
	setActiveInstance(terminalInstance: ITerminalInstance): void;
	setActiveInstanceByIndex(terminalIndex: number): void;
167
	getActiveOrCreateInstance(wasNewTerminalAction?: boolean): ITerminalInstance;
168
	splitInstance(instance: ITerminalInstance, shell?: IShellLaunchConfig): void;
169

170
	getActiveTab(): ITerminalTab;
171 172 173 174
	setActiveTabToNext(): void;
	setActiveTabToPrevious(): void;
	setActiveTabByIndex(tabIndex: number): void;

175
	showPanel(focus?: boolean): TPromise<void>;
D
Daniel Imms 已提交
176
	hidePanel(): void;
177 178
	focusFindWidget(): TPromise<void>;
	hideFindWidget(): void;
179 180 181
	showNextFindTermFindWidget(): void;
	showPreviousFindTermFindWidget(): void;

182
	setContainers(panelContainer: HTMLElement, terminalContainer: HTMLElement): void;
D
Daniel Imms 已提交
183
	selectDefaultWindowsShell(): TPromise<string>;
184
	setWorkspaceShellAllowed(isAllowed: boolean): void;
D
Daniel Imms 已提交
185
}
D
Daniel Imms 已提交
186

D
Daniel Imms 已提交
187 188 189 190 191 192 193
export const enum Direction {
	Left = 0,
	Right = 1,
	Up = 2,
	Down = 3
}

194
export interface ITerminalTab {
D
Daniel Imms 已提交
195
	activeInstance: ITerminalInstance;
196
	terminalInstances: ITerminalInstance[];
D
Daniel Imms 已提交
197
	title: string;
D
Daniel Imms 已提交
198
	onDisposed: Event<ITerminalTab>;
199
	onInstancesChanged: Event<void>;
200

201 202
	focusPreviousPane(): void;
	focusNextPane(): void;
D
Daniel Imms 已提交
203
	resizePane(direction: Direction): void;
204
	setActiveInstanceByIndex(index: number): void;
D
Daniel Imms 已提交
205
	attachToElement(element: HTMLElement): void;
D
Daniel Imms 已提交
206 207
	setVisible(visible: boolean): void;
	layout(width: number, height: number): void;
208
	addDisposable(disposable: IDisposable): void;
D
Daniel Imms 已提交
209
	split(terminalFocusContextKey: IContextKey<boolean>, configHelper: ITerminalConfigHelper, shellLaunchConfig: IShellLaunchConfig): ITerminalInstance;
210 211
}

212
export interface ITerminalInstance {
D
jsdoc  
Daniel Imms 已提交
213 214 215 216
	/**
	 * The ID of the terminal instance, this is an arbitrary number only used to identify the
	 * terminal instance.
	 */
217
	id: number;
D
jsdoc  
Daniel Imms 已提交
218

219 220 221 222 223
	/**
	 * The process ID of the shell process.
	 */
	processId: number;

D
jsdoc  
Daniel Imms 已提交
224 225 226
	/**
	 * An event that fires when the terminal instance's title changes.
	 */
D
Daniel Imms 已提交
227
	onTitleChanged: Event<string>;
D
jsdoc  
Daniel Imms 已提交
228

229 230 231 232 233
	/**
	 * An event that fires when the terminal instance is disposed.
	 */
	onDisposed: Event<ITerminalInstance>;

234 235
	onFocused: Event<ITerminalInstance>;

236 237
	onProcessIdReady: Event<ITerminalInstance>;

D
Daniel Imms 已提交
238 239
	processReady: TPromise<void>;

D
jsdoc  
Daniel Imms 已提交
240 241 242 243 244 245
	/**
	 * The title of the terminal. This is either title or the process currently running or an
	 * explicit name given to the terminal instance through the extension API.
	 *
	 * @readonly
	 */
246 247
	title: string;

K
Kai Wood 已提交
248 249 250 251 252 253 254
	/**
	 * The focus state of the terminal before exiting.
	 *
	 * @readonly
	 */
	hadFocusOnExit: boolean;

A
Amy Qiu 已提交
255 256 257 258 259 260
	/**
	 * False when the title is set by an API or the user. We check this to make sure we
	 * do not override the title when the process title changes in the terminal.
	 */
	isTitleSetByProcess: boolean;

261 262 263 264 265
	/**
	 * The shell launch config used to launch the shell.
	 */
	shellLaunchConfig: IShellLaunchConfig;

266 267 268 269 270 271 272
	/**
	 * Whether to disable layout for the terminal. This is useful when the size of the terminal is
	 * being manipulating (eg. adding a split pane) and we want the terminal to ignore particular
	 * resize events.
	 */
	disableLayout: boolean;

D
Daniel Imms 已提交
273 274 275 276 277 278
	/**
	 * An object that tracks when commands are run and enables navigating and selecting between
	 * them.
	 */
	readonly commandTracker: ITerminalCommandTracker;

D
jsdoc  
Daniel Imms 已提交
279 280 281
	/**
	 * Dispose the terminal instance, removing it from the panel/service and freeing up resources.
	 */
282
	dispose(): void;
D
jsdoc  
Daniel Imms 已提交
283

284 285 286 287 288 289 290
	/**
	 * Registers a link matcher, allowing custom link patterns to be matched and handled.
	 * @param regex The regular expression the search for, specifically this searches the
	 * textContent of the rows. You will want to use \s to match a space ' ' character for example.
	 * @param handler The callback when the link is called.
	 * @param matchIndex The index of the link from the regex.match(html) call. This defaults to 0
	 * (for regular expressions without capture groups).
291 292
	 * @param validationCallback A callback which can be used to validate the link after it has been
	 * added to the DOM.
293 294
	 * @return The ID of the new matcher, this can be used to deregister.
	 */
D
Daniel Imms 已提交
295
	registerLinkMatcher(regex: RegExp, handler: (url: string) => void, matchIndex?: number, validationCallback?: (uri: string, callback: (isValid: boolean) => void) => void): number;
296 297 298 299 300 301 302 303

	/**
	 * Deregisters a link matcher if it has been registered.
	 * @param matcherId The link matcher's ID (returned after register)
	 * @return Whether a link matcher was found and deregistered.
	 */
	deregisterLinkMatcher(matcherId: number): void;

304 305 306 307 308
	/**
	 * Check if anything is selected in terminal.
	 */
	hasSelection(): boolean;

D
jsdoc  
Daniel Imms 已提交
309 310 311
	/**
	 * Copies the terminal selection to the clipboard.
	 */
312
	copySelection(): void;
D
jsdoc  
Daniel Imms 已提交
313

314 315 316 317 318
	/**
	 * Current selection in the terminal.
	 */
	readonly selection: string | undefined;

319 320 321 322 323
	/**
	 * Clear current selection.
	 */
	clearSelection(): void;

324 325 326 327 328
	/**
	 * Select all text in the terminal.
	 */
	selectAll(): void;

R
rebornix 已提交
329 330 331 332 333 334 335 336 337 338
	/**
	 * Find the next instance of the term
	*/
	findNext(term: string): boolean;

	/**
	 * Find the previous instance of the term
	 */
	findPrevious(term: string): boolean;

339 340 341 342 343
	/**
	 * Notifies the terminal that the find widget's focus state has been changed.
	 */
	notifyFindWidgetFocusChanged(isFocused: boolean): void;

D
jsdoc  
Daniel Imms 已提交
344 345 346 347 348
	/**
	 * Focuses the terminal instance.
	 *
	 * @param focus Force focus even if there is a selection.
	 */
D
Daniel Imms 已提交
349
	focus(force?: boolean): void;
D
jsdoc  
Daniel Imms 已提交
350 351 352 353

	/**
	 * Focuses and pastes the contents of the clipboard into the terminal instance.
	 */
354
	paste(): void;
D
jsdoc  
Daniel Imms 已提交
355 356 357 358 359 360 361 362 363 364

	/**
	 * Send text to the terminal instance. The text is written to the stdin of the underlying pty
	 * process (shell) of the terminal instance.
	 *
	 * @param text The text to send.
	 * @param addNewLine Whether to add a new line to the text being sent, this is normally
	 * required to run a command in the terminal. The character(s) added are \n or \r\n
	 * depending on the platform. This defaults to `true`.
	 */
365
	sendText(text: string, addNewLine: boolean): void;
D
jsdoc  
Daniel Imms 已提交
366

367 368 369 370
	/** Scroll the terminal buffer down 1 line. */
	scrollDownLine(): void;
	/** Scroll the terminal buffer down 1 page. */
	scrollDownPage(): void;
371 372
	/** Scroll the terminal buffer to the bottom. */
	scrollToBottom(): void;
373 374 375 376
	/** Scroll the terminal buffer up 1 line. */
	scrollUpLine(): void;
	/** Scroll the terminal buffer up 1 page. */
	scrollUpPage(): void;
377 378
	/** Scroll the terminal buffer to the top. */
	scrollToTop(): void;
D
Daniel Imms 已提交
379

D
Daniel Imms 已提交
380 381 382 383 384
	/**
	 * Clears the terminal buffer, leaving only the prompt line.
	 */
	clear(): void;

D
jsdoc  
Daniel Imms 已提交
385 386 387 388 389 390
	/**
	 * Attaches the terminal instance to an element on the DOM, before this is called the terminal
	 * instance process may run in the background but cannot be displayed on the UI.
	 *
	 * @param container The element to attach the terminal instance to.
	 */
D
Daniel Imms 已提交
391
	attachToElement(container: HTMLElement): void;
D
jsdoc  
Daniel Imms 已提交
392 393

	/**
394
	 * Updates the configuration of the terminal instance.
D
jsdoc  
Daniel Imms 已提交
395
	 */
396
	updateConfig(): void;
D
Daniel Imms 已提交
397

398 399 400 401 402 403
	/**
	 * Updates the accessibility support state of the terminal instance.
	 * @param isEnabled Whether it's enabled.
	 */
	updateAccessibilitySupport(isEnabled: boolean): void;

D
jsdoc  
Daniel Imms 已提交
404 405 406 407 408
	/**
	 * Configure the dimensions of the terminal instance.
	 *
	 * @param dimension The dimensions of the container.
	 */
409
	layout(dimension: { width: number, height: number }): void;
D
jsdoc  
Daniel Imms 已提交
410 411 412 413 414 415

	/**
	 * Sets whether the terminal instance's element is visible in the DOM.
	 *
	 * @param visible Whether the element is visible.
	 */
D
Daniel Imms 已提交
416
	setVisible(visible: boolean): void;
417

418 419 420 421 422
	/**
	 * Attach a listener to listen for new lines added to this terminal instance.
	 *
	 * @param listener The listener function which takes new line strings added to the terminal,
	 * excluding ANSI escape sequences. The line event will fire when an LF character is added to
423 424 425 426
	 * the terminal (ie. the line is not wrapped). Note that this means that the line data will
	 * not fire for the last line, until either the line is ended with a LF character of the process
	 * is exited. The lineData string will contain the fully wrapped line, not containing any LF/CR
	 * characters.
427 428 429
	 */
	onLineData(listener: (lineData: string) => void): IDisposable;

430 431 432 433 434 435
	/**
	 * Attach a listener that fires when the terminal's pty process exits.
	 *
	 * @param listener The listener function which takes the processes' exit code, an exit code of
	 * null means the process was killed as a result of the ITerminalInstance being disposed.
	 */
D
Dirk Baeumer 已提交
436
	onExit(listener: (exitCode: number) => void): IDisposable;
437 438 439 440 441 442

	/**
	 * Immediately kills the terminal's current pty process and launches a new one to replace it.
	 *
	 * @param shell The new launch configuration.
	 */
443
	reuseTerminal(shell?: IShellLaunchConfig): void;
D
Daniel Imms 已提交
444

B
Ben Stein 已提交
445 446 447
	/**
	 * Sets the title of the terminal instance.
	 */
A
Amy Qiu 已提交
448
	setTitle(title: string, eventFromProcess: boolean): void;
449 450

	addDisposable(disposable: IDisposable): void;
D
Daniel Imms 已提交
451
}
D
Daniel Imms 已提交
452 453

export interface ITerminalCommandTracker {
454 455
	scrollToPreviousCommand(): void;
	scrollToNextCommand(): void;
D
Daniel Imms 已提交
456 457
	selectToPreviousCommand(): void;
	selectToNextCommand(): void;
458
}