terminal.ts 9.7 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();

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

32 33 34 35 36 37
export const TerminalCursorStyle = {
	BLOCK: 'block',
	LINE: 'line',
	UNDERLINE: 'underline'
};

38
export interface ITerminalConfiguration {
D
Daniel Imms 已提交
39 40 41 42
	shell: {
		linux: string;
		osx: string;
		windows: string;
43
	};
D
Daniel Imms 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	shellArgs: {
		linux: string[];
		osx: string[];
		windows: string[];
	};
	rightClickCopyPaste: boolean;
	cursorBlinking: boolean;
	cursorStyle: string;
	fontFamily: string;
	fontLigatures: boolean;
	fontSize: number;
	lineHeight: number;
	setLocaleVariables: boolean;
	scrollback: number;
	commandsToSkipShell: string[];
	cwd: string;
	confirmOnExit: boolean;
61 62
}

63
export interface ITerminalConfigHelper {
64
	config: ITerminalConfiguration;
65 66 67 68 69 70 71 72 73 74 75 76
	getTheme(baseThemeId: string): string[];
	getFont(): ITerminalFont;
}

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

77
export interface IShellLaunchConfig {
A
Andre Weinand 已提交
78
	/** The name of the terminal, if this is not set the name of the process will be used. */
79 80 81 82 83 84 85 86 87 88
	name?: string;
	/** The shell executable (bash, cmd, etc.). */
	executable?: string;
	/** The CLI arguments to use with executable. */
	args?: string[];
	/**
	 * The current working directory of the terminal, this overrides the `terminal.integrated.cwd`
	 * settings key.
	 */
	cwd?: string;
89 90 91 92 93
	/**
	 * 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 };
94 95 96 97 98 99
	/**
	 * 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;
	/** Whether to wait for a key press before closing the terminal. */
100
	waitOnExit?: boolean;
101 102
}

D
Daniel Imms 已提交
103
export interface ITerminalService {
104
	_serviceBrand: any;
105

D
Daniel Imms 已提交
106
	activeTerminalInstanceIndex: number;
107
	configHelper: ITerminalConfigHelper;
108
	onActiveInstanceChanged: Event<string>;
109
	onInstanceDisposed: Event<ITerminalInstance>;
110
	onInstanceProcessIdReady: Event<ITerminalInstance>;
111 112
	onInstancesChanged: Event<string>;
	onInstanceTitleChanged: Event<string>;
113 114
	terminalInstances: ITerminalInstance[];

115
	createInstance(shell?: IShellLaunchConfig): ITerminalInstance;
116
	getInstanceFromId(terminalId: number): ITerminalInstance;
D
Daniel Imms 已提交
117
	getInstanceLabels(): string[];
118 119 120 121 122 123 124
	getActiveInstance(): ITerminalInstance;
	setActiveInstance(terminalInstance: ITerminalInstance): void;
	setActiveInstanceByIndex(terminalIndex: number): void;
	setActiveInstanceToNext(): void;
	setActiveInstanceToPrevious(): void;

	showPanel(focus?: boolean): TPromise<void>;
D
Daniel Imms 已提交
125
	hidePanel(): void;
126
	setContainers(panelContainer: HTMLElement, terminalContainer: HTMLElement): void;
127
	updateConfig(): void;
D
Daniel Imms 已提交
128
}
D
Daniel Imms 已提交
129

130
export interface ITerminalInstance {
D
jsdoc  
Daniel Imms 已提交
131 132 133 134
	/**
	 * The ID of the terminal instance, this is an arbitrary number only used to identify the
	 * terminal instance.
	 */
135
	id: number;
D
jsdoc  
Daniel Imms 已提交
136

137 138 139 140 141
	/**
	 * The process ID of the shell process.
	 */
	processId: number;

D
jsdoc  
Daniel Imms 已提交
142 143 144
	/**
	 * An event that fires when the terminal instance's title changes.
	 */
D
Daniel Imms 已提交
145
	onTitleChanged: Event<string>;
D
jsdoc  
Daniel Imms 已提交
146

147 148 149 150 151
	/**
	 * An event that fires when the terminal instance is disposed.
	 */
	onDisposed: Event<ITerminalInstance>;

D
jsdoc  
Daniel Imms 已提交
152 153 154 155 156 157
	/**
	 * 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
	 */
158 159
	title: string;

K
Kai Wood 已提交
160 161 162 163 164 165 166
	/**
	 * The focus state of the terminal before exiting.
	 *
	 * @readonly
	 */
	hadFocusOnExit: boolean;

D
jsdoc  
Daniel Imms 已提交
167 168 169
	/**
	 * Dispose the terminal instance, removing it from the panel/service and freeing up resources.
	 */
170
	dispose(): void;
D
jsdoc  
Daniel Imms 已提交
171

172 173 174 175 176 177 178
	/**
	 * 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).
179 180
	 * @param validationCallback A callback which can be used to validate the link after it has been
	 * added to the DOM.
181 182
	 * @return The ID of the new matcher, this can be used to deregister.
	 */
183
	registerLinkMatcher(regex: RegExp, handler: (url: string) => void, matchIndex?: number, validationCallback?: (uri: string, callback: (isValid: boolean) => void) => void): number;
184 185 186 187 188 189 190 191

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

192 193 194 195 196
	/**
	 * Check if anything is selected in terminal.
	 */
	hasSelection(): boolean;

D
jsdoc  
Daniel Imms 已提交
197 198 199
	/**
	 * Copies the terminal selection to the clipboard.
	 */
200
	copySelection(): void;
D
jsdoc  
Daniel Imms 已提交
201

202 203 204 205 206
	/**
	 * Clear current selection.
	 */
	clearSelection(): void;

D
jsdoc  
Daniel Imms 已提交
207 208 209 210 211
	/**
	 * Focuses the terminal instance.
	 *
	 * @param focus Force focus even if there is a selection.
	 */
D
Daniel Imms 已提交
212
	focus(force?: boolean): void;
D
jsdoc  
Daniel Imms 已提交
213 214 215 216

	/**
	 * Focuses and pastes the contents of the clipboard into the terminal instance.
	 */
217
	paste(): void;
D
jsdoc  
Daniel Imms 已提交
218 219 220 221 222 223 224 225 226 227

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

230 231 232 233
	/** Scroll the terminal buffer down 1 line. */
	scrollDownLine(): void;
	/** Scroll the terminal buffer down 1 page. */
	scrollDownPage(): void;
234 235
	/** Scroll the terminal buffer to the bottom. */
	scrollToBottom(): void;
236 237 238 239
	/** Scroll the terminal buffer up 1 line. */
	scrollUpLine(): void;
	/** Scroll the terminal buffer up 1 page. */
	scrollUpPage(): void;
240 241
	/** Scroll the terminal buffer to the top. */
	scrollToTop(): void;
D
Daniel Imms 已提交
242

D
Daniel Imms 已提交
243 244 245 246 247
	/**
	 * Clears the terminal buffer, leaving only the prompt line.
	 */
	clear(): void;

D
jsdoc  
Daniel Imms 已提交
248 249 250 251 252 253
	/**
	 * 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 已提交
254
	attachToElement(container: HTMLElement): void;
D
jsdoc  
Daniel Imms 已提交
255 256

	/**
257
	 * Updates the configuration of the terminal instance.
D
jsdoc  
Daniel Imms 已提交
258
	 */
259
	updateConfig(): void;
D
Daniel Imms 已提交
260

D
jsdoc  
Daniel Imms 已提交
261 262 263 264 265
	/**
	 * Configure the dimensions of the terminal instance.
	 *
	 * @param dimension The dimensions of the container.
	 */
266
	layout(dimension: { width: number, height: number }): void;
D
jsdoc  
Daniel Imms 已提交
267 268 269 270 271 272

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

	/**
	 * Attach a listener to the data stream from the terminal's pty process.
	 *
278 279
	 * @param listener The listener function which takes the processes' data stream (including
	 * ANSI escape sequences).
280
	 */
D
Dirk Baeumer 已提交
281
	onData(listener: (data: string) => void): IDisposable;
282 283 284 285 286 287 288

	/**
	 * 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 已提交
289
	onExit(listener: (exitCode: number) => void): IDisposable;
290 291 292 293 294 295

	/**
	 * Immediately kills the terminal's current pty process and launches a new one to replace it.
	 *
	 * @param shell The new launch configuration.
	 */
296
	reuseTerminal(shell?: IShellLaunchConfig): void;
297 298 299 300 301 302 303 304 305

	/**
	 * Show a message in the terminal for a brief period.
	 *
	 * @param left The left position to show the message at.
	 * @param top The top position to show the message at.
	 * @param text The message to show.
	 */
	showMessage(left: number, top: number, text: string): void;
D
Daniel Imms 已提交
306
}