提交 c9fe9e0d 编写于 作者: D Daniel Imms

Expose the Terminal on the TerminalRenderer

上级 a361c53d
......@@ -339,13 +339,13 @@ declare module 'vscode' {
/**
* Fires when the terminal's pty slave pseudo-device is written to. In other words, this
* provides access to the raw data stream from the process running within the terminal,
* including ANSI sequences.
* including VT sequences.
*/
onData: Event<string>;
}
/**
* Represents dimensions of a terminal.
* Represents the dimensions of a terminal.
*/
export interface TerminalDimensions {
/**
......@@ -367,11 +367,18 @@ declare module 'vscode' {
* Note that an instance of [Terminal](#Terminal) will be created when a TerminalRenderer is
* created with all its APIs available for use by extensions. When using the Terminal object
* of a TerminalRenderer it acts just like normal only the extension that created the
* TerminalRenderer essentially acts as a process of a normal terminal. For example when
* an [Terminal.onData](#Terminal.onData) listener is registered, that will fire when
* TerminalRenderer essentially acts as a process. For example when an
* [Terminal.onData](#Terminal.onData) listener is registered, that will fire when
* [TerminalRenderer.write](#TerminalRenderer.write) is called. Similarly when
* [Terminal.sendText](#Terminal.sendText) is triggered that will fire the
* [TerminalRenderer.onInput](#TerminalRenderer.onInput) event.
*
* **Example:** Create a terminal renderer, show it and write hello world in red
* ```typescript
* const renderer = window.createTerminalRenderer('foo');
* renderer.terminal.then(t => t.show());
* renderer.write('\x1b[31mHello world\x1b[0m');
* ```
*/
export interface TerminalRenderer {
/**
......@@ -383,6 +390,14 @@ declare module 'vscode' {
* The dimensions of the terminal, the rows and columns of the terminal can only be set to
* a value smaller than the maximum value, if this is undefined the terminal will auto fit
* to the maximum value [maximumDimensions](TerminalRenderer.maximumDimensions).
*
* **Example:** Override the dimensions of a TerminalRenderer to 20 columns and 10 rows
* ```typescript
* terminalRenderer.dimensions = {
* cols: 20,
* rows: 10
* };
* ```
*/
dimensions: TerminalDimensions;
......@@ -394,6 +409,11 @@ declare module 'vscode' {
*/
readonly maximumDimensions: TerminalDimensions;
/**
* The corressponding [Terminal](#Terminal) for this TerminalRenderer.
*/
readonly terminal: Thenable<Terminal>;
/**
* Write text to the terminal. Unlike [Terminal.sendText](#Terminal.sendText) which sends
* text to the underlying _process_, this will write the text to the terminal itself.
......@@ -416,6 +436,16 @@ declare module 'vscode' {
* An event which fires on keystrokes in the terminal or when an extension calls
* [Terminal.sendText](#Terminal.sendText). Keystrokes are converted into their
* corresponding VT sequence representation.
*
* **Example:** Simulate interaction with the terminal from an outside extension or a
* workbench command such as `workbench.action.terminal.runSelectedText`
* ```typescript
* const terminalRenderer = window.createTerminalRenderer('test');
* terminalRenderer.onInput(data => {
* cosole.log(data); // 'Hello world'
* });
* terminalRenderer.terminal.then(t => t.sendText('Hello world'));
* ```
*/
onInput: Event<string>;
......
......@@ -179,9 +179,15 @@ export class ExtHostTerminalRenderer extends BaseExtHostTerminal implements vsco
return this._onDidChangeMaximumDimensions && this._onDidChangeMaximumDimensions.event;
}
public get terminal(): Promise<ExtHostTerminal> {
console.log('terminal ' + this._id);
return this._fetchTerminal(this._id);
}
constructor(
proxy: MainThreadTerminalServiceShape,
private _name: string
private _name: string,
private _fetchTerminal: (id: number) => Promise<ExtHostTerminal>
) {
super(proxy);
this._proxy.$createTerminalRenderer(this._name).then(id => {
......@@ -240,7 +246,7 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
}
public createTerminalRenderer(name: string): vscode.TerminalRenderer {
const renderer = new ExtHostTerminalRenderer(this._proxy, name);
const renderer = new ExtHostTerminalRenderer(this._proxy, name, (id) => this._getTerminalByIdEventually(id));
this._terminalRenderers.push(renderer);
return renderer;
}
......@@ -395,6 +401,25 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
this._proxy.$sendProcessExit(id, exitCode);
}
private _getTerminalByIdEventually(id: number, retries: number = 3): Promise<ExtHostTerminal> {
return new Promise(c => {
if (retries === 0) {
c(undefined);
return;
}
const terminal = this._getTerminalById(id);
if (terminal) {
c(terminal);
} else {
// This should only be needed immediately after createTerminalRenderer is called
setTimeout(() => {
c(this._getTerminalByIdEventually(retries - 1));
}, 100);
}
});
}
private _getTerminalById(id: number): ExtHostTerminal {
return this._getTerminalObjectById(this._terminals, id);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册