From ae216c3088b194e3471ac83d45ed9cb089467373 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 14 Jun 2019 23:00:33 -0700 Subject: [PATCH] Move AttributeData into separate file --- src/InputHandler.test.ts | 4 +- src/InputHandler.ts | 5 +- src/Terminal.ts | 3 +- src/TestUtils.test.ts | 2 +- src/common/buffer/AttributeData.ts | 68 ++++++++++ src/common/buffer/BufferLine.ts | 121 +----------------- src/common/buffer/CellData.ts | 2 +- src/common/buffer/Constants.ts | 55 ++++++++ src/renderer/BaseRenderLayer.ts | 2 +- src/renderer/CharacterJoinerRegistry.ts | 2 +- src/renderer/TextRenderLayer.ts | 2 +- .../dom/DomRendererRowFactory.test.ts | 4 +- src/renderer/dom/DomRendererRowFactory.ts | 2 +- 13 files changed, 142 insertions(+), 130 deletions(-) create mode 100644 src/common/buffer/AttributeData.ts diff --git a/src/InputHandler.test.ts b/src/InputHandler.test.ts index e42216af..8170796f 100644 --- a/src/InputHandler.test.ts +++ b/src/InputHandler.test.ts @@ -8,8 +8,10 @@ import { InputHandler } from './InputHandler'; import { MockInputHandlingTerminal, TestTerminal } from './TestUtils.test'; import { Terminal } from './Terminal'; import { IBufferLine } from 'common/Types'; -import { Attributes, AttributeData, DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; +import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; import { CellData } from 'common/buffer/CellData'; +import { Attributes } from 'common/buffer/Constants'; +import { AttributeData } from 'common/buffer/AttributeData'; describe('InputHandler', () => { describe('save and restore cursor', () => { diff --git a/src/InputHandler.ts b/src/InputHandler.ts index 1c6b1cce..e2784aa0 100644 --- a/src/InputHandler.ts +++ b/src/InputHandler.ts @@ -13,11 +13,12 @@ import { IDisposable } from 'xterm'; import { Disposable } from 'common/Lifecycle'; import { concat } from 'common/TypedArrayUtils'; import { StringToUtf32, stringFromCodePoint, utf32ToString, Utf8ToUtf32 } from 'common/input/TextDecoder'; -import { Attributes, FgFlags, BgFlags, AttributeData, DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; +import {DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; import { EventEmitter, IEvent } from 'common/EventEmitter'; import { IParsingState, IDcsHandler, IEscapeSequenceParser } from 'common/parser/Types'; -import { NULL_CELL_CODE, NULL_CELL_WIDTH } from 'common/buffer/Constants'; +import { NULL_CELL_CODE, NULL_CELL_WIDTH, Attributes, FgFlags, BgFlags } from 'common/buffer/Constants'; import { CellData } from 'common/buffer/CellData'; +import { AttributeData } from 'common/buffer/AttributeData'; /** * Map collect to glevel. Used in `selectCharset`. diff --git a/src/Terminal.ts b/src/Terminal.ts index eaf2a41d..42cacf6f 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -44,7 +44,7 @@ import { DomRenderer } from './renderer/dom/DomRenderer'; import { IKeyboardEvent, KeyboardResultType, ICharset, IBufferLine, IAttributeData } from 'common/Types'; import { evaluateKeyboardEvent } from 'common/input/Keyboard'; import { EventEmitter, IEvent } from 'common/EventEmitter'; -import { Attributes, DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; +import { DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; import { applyWindowsMode } from './WindowsMode'; import { ColorManager } from 'browser/ColorManager'; import { RenderService } from 'browser/services/RenderService'; @@ -55,6 +55,7 @@ import { CharSizeService } from 'browser/services/CharSizeService'; import { BufferService, MINIMUM_COLS, MINIMUM_ROWS } from 'common/services/BufferService'; import { Disposable } from 'common/Lifecycle'; import { IBufferSet, IBuffer } from 'common/buffer/Types'; +import { Attributes } from 'common/buffer/Constants'; // Let it work inside Node.js for automated testing purposes. const document = (typeof window !== 'undefined') ? window.document : null; diff --git a/src/TestUtils.test.ts b/src/TestUtils.test.ts index 6b92bf0b..257babe8 100644 --- a/src/TestUtils.test.ts +++ b/src/TestUtils.test.ts @@ -11,7 +11,7 @@ import { Buffer } from 'common/buffer/Buffer'; import * as Browser from 'common/Platform'; import { IDisposable, IMarker, IEvent, ISelectionPosition } from 'xterm'; import { Terminal } from './Terminal'; -import { AttributeData } from 'common/buffer/BufferLine'; +import { AttributeData } from 'common/buffer/AttributeData'; import { IColorManager, IColorSet, IMouseHelper } from 'browser/Types'; import { IOptionsService } from 'common/services/Services'; import { EventEmitter } from 'common/EventEmitter'; diff --git a/src/common/buffer/AttributeData.ts b/src/common/buffer/AttributeData.ts new file mode 100644 index 00000000..0e7e2705 --- /dev/null +++ b/src/common/buffer/AttributeData.ts @@ -0,0 +1,68 @@ +/** + * Copyright (c) 2018 The xterm.js authors. All rights reserved. + * @license MIT + */ + +import { IAttributeData, IColorRGB } from 'common/Types'; +import { Attributes, FgFlags, BgFlags } from 'common/buffer/Constants'; + +export class AttributeData implements IAttributeData { + static toColorRGB(value: number): IColorRGB { + return [ + value >>> Attributes.RED_SHIFT & 255, + value >>> Attributes.GREEN_SHIFT & 255, + value & 255 + ]; + } + static fromColorRGB(value: IColorRGB): number { + return (value[0] & 255) << Attributes.RED_SHIFT | (value[1] & 255) << Attributes.GREEN_SHIFT | value[2] & 255; + } + + public clone(): IAttributeData { + const newObj = new AttributeData(); + newObj.fg = this.fg; + newObj.bg = this.bg; + return newObj; + } + + // data + public fg: number = 0; + public bg: number = 0; + + // flags + public isInverse(): number { return this.fg & FgFlags.INVERSE; } + public isBold(): number { return this.fg & FgFlags.BOLD; } + public isUnderline(): number { return this.fg & FgFlags.UNDERLINE; } + public isBlink(): number { return this.fg & FgFlags.BLINK; } + public isInvisible(): number { return this.fg & FgFlags.INVISIBLE; } + public isItalic(): number { return this.bg & BgFlags.ITALIC; } + public isDim(): number { return this.bg & BgFlags.DIM; } + + // color modes + public getFgColorMode(): number { return this.fg & Attributes.CM_MASK; } + public getBgColorMode(): number { return this.bg & Attributes.CM_MASK; } + public isFgRGB(): boolean { return (this.fg & Attributes.CM_MASK) === Attributes.CM_RGB; } + public isBgRGB(): boolean { return (this.bg & Attributes.CM_MASK) === Attributes.CM_RGB; } + public isFgPalette(): boolean { return (this.fg & Attributes.CM_MASK) === Attributes.CM_P16 || (this.fg & Attributes.CM_MASK) === Attributes.CM_P256; } + public isBgPalette(): boolean { return (this.bg & Attributes.CM_MASK) === Attributes.CM_P16 || (this.bg & Attributes.CM_MASK) === Attributes.CM_P256; } + public isFgDefault(): boolean { return (this.fg & Attributes.CM_MASK) === 0; } + public isBgDefault(): boolean { return (this.bg & Attributes.CM_MASK) === 0; } + + // colors + public getFgColor(): number { + switch (this.fg & Attributes.CM_MASK) { + case Attributes.CM_P16: + case Attributes.CM_P256: return this.fg & Attributes.PCOLOR_MASK; + case Attributes.CM_RGB: return this.fg & Attributes.RGB_MASK; + default: return -1; // CM_DEFAULT defaults to -1 + } + } + public getBgColor(): number { + switch (this.bg & Attributes.CM_MASK) { + case Attributes.CM_P16: + case Attributes.CM_P256: return this.bg & Attributes.PCOLOR_MASK; + case Attributes.CM_RGB: return this.bg & Attributes.RGB_MASK; + default: return -1; // CM_DEFAULT defaults to -1 + } + } +} diff --git a/src/common/buffer/BufferLine.ts b/src/common/buffer/BufferLine.ts index 95ddb91a..8e742be3 100644 --- a/src/common/buffer/BufferLine.ts +++ b/src/common/buffer/BufferLine.ts @@ -3,10 +3,11 @@ * @license MIT */ -import { CharData, IBufferLine, ICellData, IColorRGB, IAttributeData } from 'common/Types'; +import { CharData, IBufferLine, ICellData } from 'common/Types'; import { stringFromCodePoint } from 'common/input/TextDecoder'; import { DEFAULT_COLOR, CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX, CHAR_DATA_ATTR_INDEX, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE, WHITESPACE_CELL_CHAR, Content } from 'common/buffer/Constants'; -import { CellData } from './CellData'; +import { CellData } from 'common/buffer/CellData'; +import { AttributeData } from 'common/buffer/AttributeData'; /** * buffer memory layout: @@ -34,122 +35,6 @@ const enum Cell { BG = 2 // currently unused } -export const enum Attributes { - /** - * bit 1..8 blue in RGB, color in P256 and P16 - */ - BLUE_MASK = 0xFF, - BLUE_SHIFT = 0, - PCOLOR_MASK = 0xFF, - PCOLOR_SHIFT = 0, - - /** - * bit 9..16 green in RGB - */ - GREEN_MASK = 0xFF00, - GREEN_SHIFT = 8, - - /** - * bit 17..24 red in RGB - */ - RED_MASK = 0xFF0000, - RED_SHIFT = 16, - - /** - * bit 25..26 color mode: DEFAULT (0) | P16 (1) | P256 (2) | RGB (3) - */ - CM_MASK = 0x3000000, - CM_DEFAULT = 0, - CM_P16 = 0x1000000, - CM_P256 = 0x2000000, - CM_RGB = 0x3000000, - - /** - * bit 1..24 RGB room - */ - RGB_MASK = 0xFFFFFF -} - -export const enum FgFlags { - /** - * bit 27..31 (32th bit unused) - */ - INVERSE = 0x4000000, - BOLD = 0x8000000, - UNDERLINE = 0x10000000, - BLINK = 0x20000000, - INVISIBLE = 0x40000000 -} - -export const enum BgFlags { - /** - * bit 27..32 (upper 4 unused) - */ - ITALIC = 0x4000000, - DIM = 0x8000000 -} - -export class AttributeData implements IAttributeData { - static toColorRGB(value: number): IColorRGB { - return [ - value >>> Attributes.RED_SHIFT & 255, - value >>> Attributes.GREEN_SHIFT & 255, - value & 255 - ]; - } - static fromColorRGB(value: IColorRGB): number { - return (value[0] & 255) << Attributes.RED_SHIFT | (value[1] & 255) << Attributes.GREEN_SHIFT | value[2] & 255; - } - - public clone(): IAttributeData { - const newObj = new AttributeData(); - newObj.fg = this.fg; - newObj.bg = this.bg; - return newObj; - } - - // data - public fg: number = 0; - public bg: number = 0; - - // flags - public isInverse(): number { return this.fg & FgFlags.INVERSE; } - public isBold(): number { return this.fg & FgFlags.BOLD; } - public isUnderline(): number { return this.fg & FgFlags.UNDERLINE; } - public isBlink(): number { return this.fg & FgFlags.BLINK; } - public isInvisible(): number { return this.fg & FgFlags.INVISIBLE; } - public isItalic(): number { return this.bg & BgFlags.ITALIC; } - public isDim(): number { return this.bg & BgFlags.DIM; } - - // color modes - public getFgColorMode(): number { return this.fg & Attributes.CM_MASK; } - public getBgColorMode(): number { return this.bg & Attributes.CM_MASK; } - public isFgRGB(): boolean { return (this.fg & Attributes.CM_MASK) === Attributes.CM_RGB; } - public isBgRGB(): boolean { return (this.bg & Attributes.CM_MASK) === Attributes.CM_RGB; } - public isFgPalette(): boolean { return (this.fg & Attributes.CM_MASK) === Attributes.CM_P16 || (this.fg & Attributes.CM_MASK) === Attributes.CM_P256; } - public isBgPalette(): boolean { return (this.bg & Attributes.CM_MASK) === Attributes.CM_P16 || (this.bg & Attributes.CM_MASK) === Attributes.CM_P256; } - public isFgDefault(): boolean { return (this.fg & Attributes.CM_MASK) === 0; } - public isBgDefault(): boolean { return (this.bg & Attributes.CM_MASK) === 0; } - - // colors - public getFgColor(): number { - switch (this.fg & Attributes.CM_MASK) { - case Attributes.CM_P16: - case Attributes.CM_P256: return this.fg & Attributes.PCOLOR_MASK; - case Attributes.CM_RGB: return this.fg & Attributes.RGB_MASK; - default: return -1; // CM_DEFAULT defaults to -1 - } - } - public getBgColor(): number { - switch (this.bg & Attributes.CM_MASK) { - case Attributes.CM_P16: - case Attributes.CM_P256: return this.bg & Attributes.PCOLOR_MASK; - case Attributes.CM_RGB: return this.bg & Attributes.RGB_MASK; - default: return -1; // CM_DEFAULT defaults to -1 - } - } -} - export const DEFAULT_ATTR_DATA = Object.freeze(new AttributeData()); /** diff --git a/src/common/buffer/CellData.ts b/src/common/buffer/CellData.ts index 39fe0d50..21ad2ee5 100644 --- a/src/common/buffer/CellData.ts +++ b/src/common/buffer/CellData.ts @@ -6,7 +6,7 @@ import { CharData, ICellData } from 'common/Types'; import { stringFromCodePoint } from 'common/input/TextDecoder'; import { CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX, CHAR_DATA_ATTR_INDEX, Content } from 'common/buffer/Constants'; -import { AttributeData } from './BufferLine'; +import { AttributeData } from 'common/buffer/AttributeData'; /** * CellData - represents a single Cell in the terminal buffer. diff --git a/src/common/buffer/Constants.ts b/src/common/buffer/Constants.ts index 35c9096f..276a5c54 100644 --- a/src/common/buffer/Constants.ts +++ b/src/common/buffer/Constants.ts @@ -71,3 +71,58 @@ export const enum Content { WIDTH_MASK = 0xC00000, // 3 << 22 WIDTH_SHIFT = 22 } + +export const enum Attributes { + /** + * bit 1..8 blue in RGB, color in P256 and P16 + */ + BLUE_MASK = 0xFF, + BLUE_SHIFT = 0, + PCOLOR_MASK = 0xFF, + PCOLOR_SHIFT = 0, + + /** + * bit 9..16 green in RGB + */ + GREEN_MASK = 0xFF00, + GREEN_SHIFT = 8, + + /** + * bit 17..24 red in RGB + */ + RED_MASK = 0xFF0000, + RED_SHIFT = 16, + + /** + * bit 25..26 color mode: DEFAULT (0) | P16 (1) | P256 (2) | RGB (3) + */ + CM_MASK = 0x3000000, + CM_DEFAULT = 0, + CM_P16 = 0x1000000, + CM_P256 = 0x2000000, + CM_RGB = 0x3000000, + + /** + * bit 1..24 RGB room + */ + RGB_MASK = 0xFFFFFF +} + +export const enum FgFlags { + /** + * bit 27..31 (32th bit unused) + */ + INVERSE = 0x4000000, + BOLD = 0x8000000, + UNDERLINE = 0x10000000, + BLINK = 0x20000000, + INVISIBLE = 0x40000000 +} + +export const enum BgFlags { + /** + * bit 27..32 (upper 4 unused) + */ + ITALIC = 0x4000000, + DIM = 0x8000000 +} diff --git a/src/renderer/BaseRenderLayer.ts b/src/renderer/BaseRenderLayer.ts index 021c4fba..5101fdd5 100644 --- a/src/renderer/BaseRenderLayer.ts +++ b/src/renderer/BaseRenderLayer.ts @@ -12,7 +12,7 @@ import { IGlyphIdentifier } from './atlas/Types'; import { DIM_OPACITY, INVERTED_DEFAULT_COLOR } from './atlas/Constants'; import { BaseCharAtlas } from './atlas/BaseCharAtlas'; import { acquireCharAtlas } from './atlas/CharAtlasCache'; -import { AttributeData } from 'common/buffer/BufferLine'; +import { AttributeData } from 'common/buffer/AttributeData'; import { IColorSet } from 'browser/Types'; import { CellData } from 'common/buffer/CellData'; diff --git a/src/renderer/CharacterJoinerRegistry.ts b/src/renderer/CharacterJoinerRegistry.ts index 475def00..80fff2b1 100644 --- a/src/renderer/CharacterJoinerRegistry.ts +++ b/src/renderer/CharacterJoinerRegistry.ts @@ -6,7 +6,7 @@ import { ITerminal } from '../Types'; import { IBufferLine, ICellData, CharData } from 'common/Types'; import { ICharacterJoinerRegistry, ICharacterJoiner } from './Types'; -import { AttributeData } from 'common/buffer/BufferLine'; +import { AttributeData } from 'common/buffer/AttributeData'; import { WHITESPACE_CELL_CHAR, Content } from 'common/buffer/Constants'; import { CellData } from 'common/buffer/CellData'; diff --git a/src/renderer/TextRenderLayer.ts b/src/renderer/TextRenderLayer.ts index a0aac5ad..db3bec19 100644 --- a/src/renderer/TextRenderLayer.ts +++ b/src/renderer/TextRenderLayer.ts @@ -9,7 +9,7 @@ import { ITerminal } from '../Types'; import { CharData, ICellData } from 'common/Types'; import { GridCache } from './GridCache'; import { BaseRenderLayer } from './BaseRenderLayer'; -import { AttributeData } from 'common/buffer/BufferLine'; +import { AttributeData } from 'common/buffer/AttributeData'; import { NULL_CELL_CODE, Content } from 'common/buffer/Constants'; import { JoinedCellData } from './CharacterJoinerRegistry'; import { IColorSet } from 'browser/Types'; diff --git a/src/renderer/dom/DomRendererRowFactory.test.ts b/src/renderer/dom/DomRendererRowFactory.test.ts index ad4b96cd..6ad9641c 100644 --- a/src/renderer/dom/DomRendererRowFactory.test.ts +++ b/src/renderer/dom/DomRendererRowFactory.test.ts @@ -6,8 +6,8 @@ import jsdom = require('jsdom'); import { assert } from 'chai'; import { DomRendererRowFactory } from './DomRendererRowFactory'; -import { NULL_CELL_CODE, NULL_CELL_WIDTH, NULL_CELL_CHAR, DEFAULT_ATTR } from 'common/buffer/Constants'; -import { BufferLine, FgFlags, BgFlags, Attributes, DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; +import { NULL_CELL_CODE, NULL_CELL_WIDTH, NULL_CELL_CHAR, DEFAULT_ATTR, FgFlags, BgFlags, Attributes } from 'common/buffer/Constants'; +import { BufferLine, DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine'; import { ITerminalOptions } from '../../Types'; import { IBufferLine } from 'common/Types'; import { CellData } from 'common/buffer/CellData'; diff --git a/src/renderer/dom/DomRendererRowFactory.ts b/src/renderer/dom/DomRendererRowFactory.ts index 4dc67ed4..687d207d 100644 --- a/src/renderer/dom/DomRendererRowFactory.ts +++ b/src/renderer/dom/DomRendererRowFactory.ts @@ -6,7 +6,7 @@ import { ITerminalOptions } from '../../Types'; import { IBufferLine } from 'common/Types'; import { INVERTED_DEFAULT_COLOR } from '../atlas/Constants'; -import { AttributeData } from 'common/buffer/BufferLine'; +import { AttributeData } from 'common/buffer/AttributeData'; import { NULL_CELL_CODE, WHITESPACE_CELL_CHAR } from 'common/buffer/Constants'; import { CellData } from 'common/buffer/CellData';