Skip to content

Commit

Permalink
Provide an ITerminalInstance.onLineData internal API
Browse files Browse the repository at this point in the history
Fixes #29840
  • Loading branch information
Tyriar committed Oct 26, 2017
1 parent 77ba628 commit 8bf87ab
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/vs/workbench/parts/terminal/common/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,22 @@ export interface ITerminalInstance {
*
* @param listener The listener function which takes the processes' data stream (including
* ANSI escape sequences).
*
* @deprecated onLineData will replace this.
*/
onData(listener: (data: string) => void): IDisposable;

/**
* 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
* the terminal (ie. the line is not wrapped), note that this means taht the line data will
* never fire for the last line, until the line is ended with a LF character. The lineData
* string will contain the fully wrapped line, not containing any LF/CR characters.
*/
onLineData(listener: (lineData: string) => void): IDisposable;

/**
* Attach a listener that fires when the terminal's pty process exits.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,32 @@ export class TerminalInstance implements ITerminalInstance {
};
}

public onLineData(listener: (lineData: string) => void): lifecycle.IDisposable {
if (!this._xterm) {
throw new Error('xterm must be initialized');
}
const lineFeedListener = () => {
const buffer = (<any>this._xterm.buffer);
const newLine = buffer.lines.get(buffer.ybase + buffer.y);
if (!newLine.isWrapped) {
let i = buffer.ybase + buffer.y - 1;
let lineData = buffer.translateBufferLineToString(i, true);
while (i >= 0 && buffer.lines.get(i--).isWrapped) {
lineData = buffer.translateBufferLineToString(i, true) + lineData;
}
listener(lineData);
}
};
this._xterm.on('lineFeed', lineFeedListener);
return {
dispose: () => {
if (this._xterm) {
this._xterm.off('lineFeed', lineFeedListener);
}
}
};
}

public onExit(listener: (exitCode: number) => void): lifecycle.IDisposable {
if (this._process) {
this._process.on('exit', listener);
Expand Down

0 comments on commit 8bf87ab

Please sign in to comment.