terminal.ts 12.0 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';

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

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

export const TERMINAL_SERVICE_ID = 'terminalService';

H
hun1ahpu 已提交
18 19
export const TERMINAL_DEFAULT_RIGHT_CLICK_COPY_PASTE = platform.isWindows;

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

25 26 27 28 29
/** 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 已提交
30 31 32 33 34
/**  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();

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

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

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

46
export interface ITerminalConfiguration {
D
Daniel Imms 已提交
47 48 49 50
	shell: {
		linux: string;
		osx: string;
		windows: string;
51
	};
D
Daniel Imms 已提交
52 53 54 55 56
	shellArgs: {
		linux: string[];
		osx: string[];
		windows: string[];
	};
P
Phawin Khongkhasawan 已提交
57
	enableBold: boolean;
D
Daniel Imms 已提交
58 59 60 61 62 63 64 65 66 67 68 69
	rightClickCopyPaste: boolean;
	cursorBlinking: boolean;
	cursorStyle: string;
	fontFamily: string;
	fontLigatures: boolean;
	fontSize: number;
	lineHeight: number;
	setLocaleVariables: boolean;
	scrollback: number;
	commandsToSkipShell: string[];
	cwd: string;
	confirmOnExit: boolean;
70 71 72 73 74
	env: {
		linux: { [key: string]: string };
		osx: { [key: string]: string };
		windows: { [key: string]: string };
	};
75 76
}

77
export interface ITerminalConfigHelper {
78
	config: ITerminalConfiguration;
79
	getFont(): ITerminalFont;
80 81 82 83
	/**
	 * Merges the default shell path and args into the provided launch configuration
	 */
	mergeDefaultShellPathAndArgs(shell: IShellLaunchConfig): void;
84 85
	/** Sets whether a workspace shell configuration is allowed or not */
	setWorkspaceShellAllowed(isAllowed: boolean): void;
86 87 88 89 90 91 92 93 94 95
}

export interface ITerminalFont {
	fontFamily: string;
	fontSize: string;
	lineHeight: number;
	charWidth: number;
	charHeight: number;
}

96
export interface IShellLaunchConfig {
A
Andre Weinand 已提交
97
	/** The name of the terminal, if this is not set the name of the process will be used. */
98 99 100
	name?: string;
	/** The shell executable (bash, cmd, etc.). */
	executable?: string;
101 102
	/**
	 * The CLI arguments to use with executable, a string[] is in argv format and will be escaped,
D
Daniel Imms 已提交
103 104
	 * 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.
105 106
	 */
	args?: string[] | string;
107 108 109 110 111
	/**
	 * The current working directory of the terminal, this overrides the `terminal.integrated.cwd`
	 * settings key.
	 */
	cwd?: string;
112 113 114 115 116
	/**
	 * 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 };
117 118 119 120 121
	/**
	 * 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;
122

123
	/** Whether to wait for a key press before closing the terminal. */
124 125
	waitOnExit?: boolean | string;

126 127 128 129 130 131 132
	/**
	 * 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;
133 134
}

D
Daniel Imms 已提交
135
export interface ITerminalService {
136
	_serviceBrand: any;
137

D
Daniel Imms 已提交
138
	activeTerminalInstanceIndex: number;
139
	configHelper: ITerminalConfigHelper;
140
	onActiveInstanceChanged: Event<string>;
141
	onInstanceDisposed: Event<ITerminalInstance>;
142
	onInstanceProcessIdReady: Event<ITerminalInstance>;
D
Daniel Imms 已提交
143
	onInstanceData: Event<{ instance: ITerminalInstance, data: string }>;
144 145
	onInstancesChanged: Event<string>;
	onInstanceTitleChanged: Event<string>;
146 147
	terminalInstances: ITerminalInstance[];

148
	createInstance(shell?: IShellLaunchConfig, wasNewTerminalAction?: boolean): ITerminalInstance;
149
	getInstanceFromId(terminalId: number): ITerminalInstance;
D
Daniel Imms 已提交
150
	getInstanceLabels(): string[];
151 152 153 154 155
	getActiveInstance(): ITerminalInstance;
	setActiveInstance(terminalInstance: ITerminalInstance): void;
	setActiveInstanceByIndex(terminalIndex: number): void;
	setActiveInstanceToNext(): void;
	setActiveInstanceToPrevious(): void;
156
	getActiveOrCreateInstance(wasNewTerminalAction?: boolean): ITerminalInstance;
157 158

	showPanel(focus?: boolean): TPromise<void>;
D
Daniel Imms 已提交
159
	hidePanel(): void;
R
rebornix 已提交
160 161
	focusFindWidget(): TPromise<void>;
	hideFindWidget(): void;
162
	setContainers(panelContainer: HTMLElement, terminalContainer: HTMLElement): void;
163
	updateConfig(): void;
D
Daniel Imms 已提交
164
	selectDefaultWindowsShell(): TPromise<string>;
165
	setWorkspaceShellAllowed(isAllowed: boolean): void;
D
Daniel Imms 已提交
166
}
D
Daniel Imms 已提交
167

168
export interface ITerminalInstance {
D
jsdoc  
Daniel Imms 已提交
169 170 171 172
	/**
	 * The ID of the terminal instance, this is an arbitrary number only used to identify the
	 * terminal instance.
	 */
173
	id: number;
D
jsdoc  
Daniel Imms 已提交
174

175 176 177 178 179
	/**
	 * The process ID of the shell process.
	 */
	processId: number;

D
jsdoc  
Daniel Imms 已提交
180 181 182
	/**
	 * An event that fires when the terminal instance's title changes.
	 */
D
Daniel Imms 已提交
183
	onTitleChanged: Event<string>;
D
jsdoc  
Daniel Imms 已提交
184

185 186 187 188 189
	/**
	 * An event that fires when the terminal instance is disposed.
	 */
	onDisposed: Event<ITerminalInstance>;

D
jsdoc  
Daniel Imms 已提交
190 191 192 193 194 195
	/**
	 * 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
	 */
196 197
	title: string;

K
Kai Wood 已提交
198 199 200 201 202 203 204
	/**
	 * The focus state of the terminal before exiting.
	 *
	 * @readonly
	 */
	hadFocusOnExit: boolean;

D
jsdoc  
Daniel Imms 已提交
205 206 207
	/**
	 * Dispose the terminal instance, removing it from the panel/service and freeing up resources.
	 */
208
	dispose(): void;
D
jsdoc  
Daniel Imms 已提交
209

210 211 212 213 214 215 216
	/**
	 * 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).
217 218
	 * @param validationCallback A callback which can be used to validate the link after it has been
	 * added to the DOM.
219 220
	 * @return The ID of the new matcher, this can be used to deregister.
	 */
D
Daniel Imms 已提交
221
	registerLinkMatcher(regex: RegExp, handler: (url: string) => void, matchIndex?: number, validationCallback?: (uri: string, element: HTMLElement, callback: (isValid: boolean) => void) => void): number;
222 223 224 225 226 227 228 229

	/**
	 * 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;

230 231 232 233 234
	/**
	 * Check if anything is selected in terminal.
	 */
	hasSelection(): boolean;

D
jsdoc  
Daniel Imms 已提交
235 236 237
	/**
	 * Copies the terminal selection to the clipboard.
	 */
238
	copySelection(): void;
D
jsdoc  
Daniel Imms 已提交
239

240 241 242 243 244
	/**
	 * Current selection in the terminal.
	 */
	readonly selection: string | undefined;

245 246 247 248 249
	/**
	 * Clear current selection.
	 */
	clearSelection(): void;

250 251 252 253 254
	/**
	 * Select all text in the terminal.
	 */
	selectAll(): void;

R
rebornix 已提交
255 256 257 258 259 260 261 262 263 264
	/**
	 * Find the next instance of the term
	*/
	findNext(term: string): boolean;

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

265 266 267 268 269
	/**
	 * Notifies the terminal that the find widget's focus state has been changed.
	 */
	notifyFindWidgetFocusChanged(isFocused: boolean): void;

D
jsdoc  
Daniel Imms 已提交
270 271 272 273 274
	/**
	 * Focuses the terminal instance.
	 *
	 * @param focus Force focus even if there is a selection.
	 */
D
Daniel Imms 已提交
275
	focus(force?: boolean): void;
D
jsdoc  
Daniel Imms 已提交
276 277 278 279

	/**
	 * Focuses and pastes the contents of the clipboard into the terminal instance.
	 */
280
	paste(): void;
D
jsdoc  
Daniel Imms 已提交
281 282 283 284 285 286 287 288 289 290

	/**
	 * 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`.
	 */
291
	sendText(text: string, addNewLine: boolean): void;
D
jsdoc  
Daniel Imms 已提交
292

293 294 295 296
	/** Scroll the terminal buffer down 1 line. */
	scrollDownLine(): void;
	/** Scroll the terminal buffer down 1 page. */
	scrollDownPage(): void;
297 298
	/** Scroll the terminal buffer to the bottom. */
	scrollToBottom(): void;
299 300 301 302
	/** Scroll the terminal buffer up 1 line. */
	scrollUpLine(): void;
	/** Scroll the terminal buffer up 1 page. */
	scrollUpPage(): void;
303 304
	/** Scroll the terminal buffer to the top. */
	scrollToTop(): void;
D
Daniel Imms 已提交
305

D
Daniel Imms 已提交
306 307 308 309 310
	/**
	 * Clears the terminal buffer, leaving only the prompt line.
	 */
	clear(): void;

D
jsdoc  
Daniel Imms 已提交
311 312 313 314 315 316
	/**
	 * 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 已提交
317
	attachToElement(container: HTMLElement): void;
D
jsdoc  
Daniel Imms 已提交
318 319

	/**
320
	 * Updates the configuration of the terminal instance.
D
jsdoc  
Daniel Imms 已提交
321
	 */
322
	updateConfig(): void;
D
Daniel Imms 已提交
323

D
jsdoc  
Daniel Imms 已提交
324 325 326 327 328
	/**
	 * Configure the dimensions of the terminal instance.
	 *
	 * @param dimension The dimensions of the container.
	 */
329
	layout(dimension: { width: number, height: number }): void;
D
jsdoc  
Daniel Imms 已提交
330 331 332 333 334 335

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

	/**
	 * Attach a listener to the data stream from the terminal's pty process.
	 *
341 342
	 * @param listener The listener function which takes the processes' data stream (including
	 * ANSI escape sequences).
343
	 */
D
Dirk Baeumer 已提交
344
	onData(listener: (data: string) => void): IDisposable;
345 346 347 348 349 350 351

	/**
	 * 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 已提交
352
	onExit(listener: (exitCode: number) => void): IDisposable;
353 354 355 356 357 358

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

B
Ben Stein 已提交
361 362 363 364
	/**
	 * Sets the title of the terminal instance.
	 */
	setTitle(title: string): void;
D
Daniel Imms 已提交
365
}