terminal.ts 8.2 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');
9
import processes = require('vs/base/node/processes');
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';

18 19
export const TERMINAL_DEFAULT_SHELL_LINUX = !platform.isWindows ? (process.env.SHELL || 'sh') : 'sh';
export const TERMINAL_DEFAULT_SHELL_OSX = !platform.isWindows ? (process.env.SHELL || 'sh') : 'sh';
20
export const TERMINAL_DEFAULT_SHELL_WINDOWS = processes.getWindowsShell();
21

H
hun1ahpu 已提交
22 23
export const TERMINAL_DEFAULT_RIGHT_CLICK_COPY_PASTE = platform.isWindows;

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

29 30 31 32 33
/** 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 已提交
34
export const ITerminalService = createDecorator<ITerminalService>(TERMINAL_SERVICE_ID);
D
Daniel Imms 已提交
35

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

63 64 65 66 67
export interface ITerminalConfigHelper {
	getTheme(baseThemeId: string): string[];
	getFont(): ITerminalFont;
	getFontLigaturesEnabled(): boolean;
	getCursorBlink(): boolean;
H
hun1ahpu 已提交
68
	getRightClickCopyPaste(): boolean;
69
	getCommandsToSkipShell(): string[];
D
Daniel Imms 已提交
70
	getScrollback(): number;
D
Daniel Imms 已提交
71
	getCwd(): string;
72 73 74 75 76 77 78 79 80 81
}

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

82
export interface IShellLaunchConfig {
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	/** The name of the terminal, this this is not set the name of the process will be used. */
	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;
	/**
	 * 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 152

	/**
	 * 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
	 */
153 154
	title: string;

K
Kai Wood 已提交
155 156 157 158 159 160 161
	/**
	 * The focus state of the terminal before exiting.
	 *
	 * @readonly
	 */
	hadFocusOnExit: boolean;

D
jsdoc  
Daniel Imms 已提交
162 163 164
	/**
	 * Dispose the terminal instance, removing it from the panel/service and freeing up resources.
	 */
165
	dispose(): void;
D
jsdoc  
Daniel Imms 已提交
166

167 168 169 170 171
	/**
	 * Check if anything is selected in terminal.
	 */
	hasSelection(): boolean;

D
jsdoc  
Daniel Imms 已提交
172 173 174
	/**
	 * Copies the terminal selection to the clipboard.
	 */
175
	copySelection(): void;
D
jsdoc  
Daniel Imms 已提交
176

177 178 179 180 181
	/**
	 * Clear current selection.
	 */
	clearSelection(): void;

D
jsdoc  
Daniel Imms 已提交
182 183 184 185 186
	/**
	 * Focuses the terminal instance.
	 *
	 * @param focus Force focus even if there is a selection.
	 */
D
Daniel Imms 已提交
187
	focus(force?: boolean): void;
D
jsdoc  
Daniel Imms 已提交
188 189 190 191

	/**
	 * Focuses and pastes the contents of the clipboard into the terminal instance.
	 */
192
	paste(): void;
D
jsdoc  
Daniel Imms 已提交
193 194 195 196 197 198 199 200 201 202

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

205 206 207 208
	/** Scroll the terminal buffer down 1 line. */
	scrollDownLine(): void;
	/** Scroll the terminal buffer down 1 page. */
	scrollDownPage(): void;
209 210
	/** Scroll the terminal buffer to the bottom. */
	scrollToBottom(): void;
211 212 213 214
	/** Scroll the terminal buffer up 1 line. */
	scrollUpLine(): void;
	/** Scroll the terminal buffer up 1 page. */
	scrollUpPage(): void;
215 216
	/** Scroll the terminal buffer to the top. */
	scrollToTop(): void;
D
Daniel Imms 已提交
217

D
Daniel Imms 已提交
218 219 220 221 222
	/**
	 * Clears the terminal buffer, leaving only the prompt line.
	 */
	clear(): void;

D
jsdoc  
Daniel Imms 已提交
223 224 225 226 227 228
	/**
	 * 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 已提交
229
	attachToElement(container: HTMLElement): void;
D
jsdoc  
Daniel Imms 已提交
230 231

	/**
232
	 * Updates the configuration of the terminal instance.
D
jsdoc  
Daniel Imms 已提交
233
	 */
234
	updateConfig(): void;
D
Daniel Imms 已提交
235

D
jsdoc  
Daniel Imms 已提交
236 237 238 239 240
	/**
	 * Configure the dimensions of the terminal instance.
	 *
	 * @param dimension The dimensions of the container.
	 */
241
	layout(dimension: { width: number, height: number }): void;
D
jsdoc  
Daniel Imms 已提交
242 243 244 245 246 247

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

	/**
	 * Attach a listener to the data stream from the terminal's pty process.
	 *
253 254
	 * @param listener The listener function which takes the processes' data stream (including
	 * ANSI escape sequences).
255 256
	 */
	onData(listener: (data: string) => void): void;
257 258 259 260 261 262 263 264

	/**
	 * 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.
	 */
	onExit(listener: (exitCode: number) => void): void;
D
Daniel Imms 已提交
265
}