From f1a17e3cf5c181da82d96b0c34ba0548f09086d2 Mon Sep 17 00:00:00 2001 From: javacs3 Date: Sun, 25 Aug 2019 22:25:45 +0800 Subject: [PATCH] refactor CellColor constructor --- src/public/Terminal.ts | 40 +++++++++++----------------------------- typings/xterm.d.ts | 2 +- 2 files changed, 12 insertions(+), 30 deletions(-) diff --git a/src/public/Terminal.ts b/src/public/Terminal.ts index e1fb8d6d..28758642 100644 --- a/src/public/Terminal.ts +++ b/src/public/Terminal.ts @@ -227,9 +227,15 @@ export class CellColor implements ICellColorApi { readonly type: 'default' | 'rgb' | 'palette16' | 'palette256'; readonly value: number = 0; - constructor(type: 'default' | 'rgb' | 'palette16' | 'palette256', value: number) { - this.type = type; + constructor(value: number) { this.value = value; + switch (value & Attributes.CM_MASK) { + case Attributes.CM_P16: this.type = 'palette16'; break; + case Attributes.CM_P256: this.type = 'palette256'; break; + case Attributes.CM_RGB: this.type = 'rgb'; break; + case Attributes.CM_DEFAULT: this.type = 'default'; break; + default: throw new Error('Invalid CellColor value'); + } } public isDefault(): boolean { return this.value === 0; } public equals(c: ICellColorApi): boolean { return this.value === c.value; } @@ -248,7 +254,7 @@ export class CellColor implements ICellColorApi { return [-1, -1, -1]; } - public static getDefault(): ICellColorApi { return new CellColor('default', 0); } + public static getDefault(): ICellColorApi { return new CellColor(0); } } class BufferCellApiView implements IBufferCellApi { @@ -257,34 +263,10 @@ class BufferCellApiView implements IBufferCellApi { public get char(): string { return this._cell.getChars(); } public get width(): number { return this._cell.getWidth(); } public get foregroundColor(): ICellColorApi { - const cell = this._cell; - const value = cell.fg & COLOR_MASK; - if (cell.isFgDefault()) { - return new CellColor('default', 0); - } else if (cell.isFgPalette()) { - switch (cell.getFgColorMode()) { - case Attributes.CM_P16: return new CellColor('palette16', value); - case Attributes.CM_P256: return new CellColor('palette256', value); - } - } else if (cell.isFgRGB()) { - return new CellColor('rgb', value); - } - throw new Error('Invalid foregroundColor'); + return new CellColor(this._cell.fg & COLOR_MASK); } public get backgroundColor(): ICellColorApi { - const cell = this._cell; - const value = cell.bg & COLOR_MASK; - if (cell.isBgDefault()) { - return new CellColor('default', 0); - } else if (cell.isBgPalette()) { - switch (cell.getBgColorMode()) { - case Attributes.CM_P16: return new CellColor('palette16', value); - case Attributes.CM_P256: return new CellColor('palette256', value); - } - } else if (cell.isBgRGB()) { - return new CellColor('rgb', value); - } - throw new Error('Invalid backgroundColor'); + return new CellColor(this._cell.bg & COLOR_MASK); } public get style(): CellStyle { return ((this._cell.bg & BgFlags.FM_MASK) >>> 16) | ((this._cell.fg & FgFlags.FM_MASK) >>> 24); diff --git a/typings/xterm.d.ts b/typings/xterm.d.ts index afdfa3e4..ae11a8f1 100644 --- a/typings/xterm.d.ts +++ b/typings/xterm.d.ts @@ -974,7 +974,7 @@ declare module 'xterm' { readonly type: 'default' | 'rgb' | 'palette16' | 'palette256'; readonly value: number; - constructor(type: 'default' | 'rgb' | 'palette16' | 'palette256', value: number); + constructor(value: number); isDefault(): boolean; equals(c: CellColor): boolean;