From 8bf87ab3391800967917663ebe3d98c39718928d Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 26 Oct 2017 11:06:49 -0700 Subject: [PATCH] Provide an ITerminalInstance.onLineData internal API Fixes #29840 --- .../parts/terminal/common/terminal.ts | 13 ++++++++++ .../electron-browser/terminalInstance.ts | 26 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/vs/workbench/parts/terminal/common/terminal.ts b/src/vs/workbench/parts/terminal/common/terminal.ts index fe4841ac5ec..607df896b78 100644 --- a/src/vs/workbench/parts/terminal/common/terminal.ts +++ b/src/vs/workbench/parts/terminal/common/terminal.ts @@ -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. * diff --git a/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts b/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts index 07075759c0b..1dfbf753096 100644 --- a/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts +++ b/src/vs/workbench/parts/terminal/electron-browser/terminalInstance.ts @@ -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 = (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); -- GitLab