From b93b774971a9be93093d6eb553bd2b6c152610da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Fri, 4 Jan 2019 19:14:56 +0100 Subject: [PATCH] direct cell attrs getter --- src/BufferLine.ts | 41 +++++++++++++++++++++++++++++++++++++++++ src/Types.ts | 10 ++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/BufferLine.ts b/src/BufferLine.ts index 2792d710..b3a94672 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -176,6 +176,47 @@ export class BufferLine implements IBufferLine { } } + /** + * primitive getters + * use these when only one value is needed, otherwise use `loadCell` + */ + public getWidth(index: number): number { + return this._data[index * CELL_SIZE + Cell.CONTENT] >> Content.WIDTH_SHIFT; + } + public hasWidth(index: number): number { + return this._data[index * CELL_SIZE + Cell.CONTENT] & Content.WIDTH_MASK; + } + public getFG(index: number): number { + return this._data[index * CELL_SIZE + Cell.FG]; + } + public getBG(index: number): number { + return this._data[index * CELL_SIZE + Cell.BG]; + } + public hasContent(index: number): number { + return this._data[index * CELL_SIZE + Cell.CONTENT] & Content.HAS_CONTENT; + } + public getCodePoint(index: number): number { + // returns either the single codepoint or the last charCode in combined + const content = this._data[index * CELL_SIZE + Cell.CONTENT]; + if (content & Content.IS_COMBINED) { + return this._combined[index].charCodeAt(this._combined[index].length - 1); + } + return content & Content.CODEPOINT_MASK; + } + public isCombined(index: number): number { + return this._data[index * CELL_SIZE + Cell.CONTENT] & Content.IS_COMBINED; + } + public getString(index: number): string { + const content = this._data[index * CELL_SIZE + Cell.CONTENT]; + if (content & Content.IS_COMBINED) { + return this._combined[index]; + } + if (content & Content.CODEPOINT_MASK) { + return stringFromCodePoint(content & Content.CODEPOINT_MASK); + } + return ''; // return empty string for empty cells + } + public loadCell(index: number, cell: ICellData): ICellData { cell.content = this._data[index * CELL_SIZE + Cell.CONTENT]; cell.fg = this._data[index * CELL_SIZE + Cell.FG]; diff --git a/src/Types.ts b/src/Types.ts index 180fe000..c1a7788a 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -545,4 +545,14 @@ export interface IBufferLine { clone(): IBufferLine; getTrimmedLength(): number; translateToString(trimRight?: boolean, startCol?: number, endCol?: number): string; + + /* direct access to cell attrs */ + getWidth(index: number): number; + hasWidth(index: number): number; + getFG(index: number): number; + getBG(index: number): number; + hasContent(index: number): number; + getCodePoint(index: number): number; + isCombined(index: number): number; + getString(index: number): string; }