diff --git a/src/public/Terminal.ts b/src/public/Terminal.ts index 87fcfaef99..bc82d23529 100644 --- a/src/public/Terminal.ts +++ b/src/public/Terminal.ts @@ -3,8 +3,8 @@ * @license MIT */ -import { Terminal as ITerminalApi, ITerminalOptions, IMarker, IDisposable, ILinkMatcherOptions, ITheme, ILocalizableStrings } from 'xterm'; -import { ITerminal } from '../Types'; +import { Terminal as ITerminalApi, ITerminalOptions, IMarker, IDisposable, ILinkMatcherOptions, ITheme, ILocalizableStrings, IBuffer as IBufferApi, IBufferLine as IBufferLineApi, IBufferCell as IBufferCellApi } from 'xterm'; +import { ITerminal, IBufferLine, IBuffer } from '../Types'; import { Terminal as TerminalCore } from '../Terminal'; import * as Strings from '../Strings'; @@ -19,6 +19,7 @@ export class Terminal implements ITerminalApi { public get textarea(): HTMLTextAreaElement { return this._core.textarea; } public get rows(): number { return this._core.rows; } public get cols(): number { return this._core.cols; } + public get buffer(): IBufferApi { return new BufferApiView(this._core.buffer); } public get markers(): IMarker[] { return this._core.markers; } public blur(): void { this._core.blur(); @@ -158,3 +159,30 @@ export class Terminal implements ITerminalApi { return Strings; } } + +class BufferApiView implements IBufferApi { + constructor(private _buffer: IBuffer) {} + + public get cursorY(): number { return this._buffer.y; } + public get cursorX(): number { return this._buffer.x; } + public get viewportY(): number { return this._buffer.ydisp; } + public get baseY(): number { return this._buffer.ybase; } + public get length(): number { return this._buffer.lines.length; } + public getLine(y: number): IBufferLineApi { return new BufferLineApiView(this._buffer.lines.get(y)); } +} + +class BufferLineApiView implements IBufferLineApi { + constructor(private _line: IBufferLine) {} + + public get isWrapped(): boolean { return this._line.isWrapped; } + public getCell(x: number): IBufferCellApi { return new BufferCellApiView(this._line, x); } + public translateToString(trimRight: boolean, startColumn: number, endColumn: number): string { + return this._line.translateToString(trimRight, startColumn, endColumn); + } +} + +class BufferCellApiView implements IBufferCellApi { + constructor(private _line: IBufferLine, private _x: number) {} + public get char(): string { return this._line.getString(this._x); } + public get width(): number { return this._line.getWidth(this._x); } +} diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index ec647a10f5..429ad7e382 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -347,6 +347,13 @@ declare module 'xterm' { */ readonly cols: number; + /** + * (EXPERIMENTAL) The terminal's current buffer, note that this might be + * either the normal buffer or the alt buffer depending on what's running in + * the terminal. + */ + readonly buffer: IBuffer; + /** * (EXPERIMENTAL) Get all markers registered against the buffer. If the alt * buffer is active this will always return []. @@ -772,4 +779,77 @@ declare module 'xterm' { */ static applyAddon(addon: any): void; } + + interface IBuffer { + /** + * The y position of the cursor. This ranges between `0` (when the + * cursor is at baseY) and `Terminal.rows - 1` (when the cursor is on the + * last row). + */ + readonly cursorY: number; + + /** + * The x position of the cursor. This ranges between `0` (left side) and + * `Terminal.cols - 1` (right side). + */ + readonly cursorX: number; + + /** + * The line within the buffer where the top of the viewport is. + */ + readonly viewportY: number; + + /** + * The line within the buffer where the top of the bottom page is (when + * fully scrolled down); + */ + readonly baseY: number; + + /** + * The amount of lines in the buffer. + */ + readonly length: number; + + /** + * Gets a line from the buffer. + * + * @param y The line index to get. + */ + getLine(y: number): IBufferLine; + } + + interface IBufferLine { + /** + * Whether the line is wrapped from the previous line. + */ + readonly isWrapped: boolean; + + /** + * Gets a cell from the line. + * + * @param x The character index to get. + */ + getCell(x: number): IBufferCell; + + /** + * Gets the line + */ + translateToString(trimRight: boolean, startColumn: number, endColumn: number): string; + } + + interface IBufferCell { + /** + * The character within the cell. + */ + readonly char: string; + + /** + * The width of the character. Some examples: + * + * - This is `1` for most cells. + * - This is `2` for wide character like CJK glyphs. + * - This is `0` for cells immediately following cells with a width of `2`. + */ + readonly width: number; + } }