Skip to content

Commit

Permalink
Mostly implemented buffer API
Browse files Browse the repository at this point in the history
Part of xtermjs#1994
  • Loading branch information
Tyriar committed Apr 7, 2019
1 parent 2574577 commit efeca49
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/public/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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();
Expand Down Expand Up @@ -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); }
}
80 changes: 80 additions & 0 deletions typings/xterm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [].
Expand Down Expand Up @@ -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;
}
}

0 comments on commit efeca49

Please sign in to comment.