underline color support, test cases

This commit is contained in:
Jörg Breitbart
2020-03-08 22:56:58 +01:00
parent 058f33cd7b
commit 120527cb41
5 changed files with 228 additions and 32 deletions
+37 -5
View File
@@ -73,12 +73,45 @@ export class AttributeData implements IAttributeData {
public hasExtendedAttrs(): number {
return this.bg & BgFlags.HAS_EXTENDED;
}
public updateExtended(): void {
if (this.extended.isEmpty()) {
this.bg &= ~BgFlags.HAS_EXTENDED;
} else {
this.bg |= BgFlags.HAS_EXTENDED;
}
}
public getUnderlineColor(): number {
if ((this.bg & BgFlags.HAS_EXTENDED) && ~this.extended.underlineColor) {
return this.extended.underlineColor;
switch (this.extended.underlineColor & Attributes.CM_MASK) {
case Attributes.CM_P16:
case Attributes.CM_P256: return this.extended.underlineColor & Attributes.PCOLOR_MASK;
case Attributes.CM_RGB: return this.extended.underlineColor & Attributes.RGB_MASK;
default: return this.getFgColor();
}
}
return this.getFgColor();
}
public getUnderlineColorMode(): number {
return (this.bg & BgFlags.HAS_EXTENDED) && ~this.extended.underlineColor
? this.extended.underlineColor & Attributes.CM_MASK
: this.getFgColorMode();
}
public isUnderlineColorRGB(): boolean {
return (this.bg & BgFlags.HAS_EXTENDED) && ~this.extended.underlineColor
? (this.extended.underlineColor & Attributes.CM_MASK) === Attributes.CM_RGB
: this.isFgRGB();
}
public isUnderlineColorPalette(): boolean {
return (this.bg & BgFlags.HAS_EXTENDED) && ~this.extended.underlineColor
? (this.extended.underlineColor & Attributes.CM_MASK) === Attributes.CM_P16
|| (this.extended.underlineColor & Attributes.CM_MASK) === Attributes.CM_P256
: this.isFgPalette();
}
public isUnderlineColorDefault(): boolean {
return (this.bg & BgFlags.HAS_EXTENDED) && ~this.extended.underlineColor
? (this.extended.underlineColor & Attributes.CM_MASK) === 0
: this.isFgDefault();
}
public getUnderlineStyle(): UnderlineStyle {
return this.fg & FgFlags.UNDERLINE
? (this.bg & BgFlags.HAS_EXTENDED ? this.extended.underlineStyle : UnderlineStyle.SINGLE)
@@ -104,11 +137,10 @@ export class ExtendedAttrs implements IExtendedAttrs {
}
/**
* Convenient method to indicate whether the object holds no additional information
* and can be removed from the global attr object.
* Convenient method to indicate whether the object holds no additional information,
* that needs to be persistant in the buffer.
*/
public isEmpty(): boolean {
// needs to test for every single attribute stored
return !(this.underlineStyle !== UnderlineStyle.NONE || ~this.underlineColor);
return this.underlineStyle === UnderlineStyle.NONE;
}
}
+51 -2
View File
@@ -32,9 +32,27 @@ describe('AttributeData', () => {
attrs.bg |= BgFlags.HAS_EXTENDED;
assert.equal(!!attrs.hasExtendedAttrs(), true);
});
it('getUnderlineColor', () => {
it('getUnderlineColor - P256', () => {
const attrs = new AttributeData();
attrs.extended.underlineColor = (1 << 16) | (2 << 8) | 3;
// set a P256 color
attrs.extended.underlineColor = Attributes.CM_P256 | 45;
// should use FG color if BgFlags.HAS_EXTENDED is not set
assert.equal(attrs.getUnderlineColor(), -1);
// should use underlineColor if BgFlags.HAS_EXTENDED is set and underlineColor holds a value
attrs.bg |= BgFlags.HAS_EXTENDED;
assert.equal(attrs.getUnderlineColor(), 45);
// should use FG color if underlineColor holds no value
attrs.extended.underlineColor = -1;
attrs.fg |= Attributes.CM_P256 | 123;
assert.equal(attrs.getUnderlineColor(), 123);
});
it('getUnderlineColor - RGB', () => {
const attrs = new AttributeData();
// set a P256 color
attrs.extended.underlineColor = Attributes.CM_RGB | (1 << 16) | (2 << 8) | 3;
// should use FG color if BgFlags.HAS_EXTENDED is not set
assert.equal(attrs.getUnderlineColor(), -1);
@@ -48,6 +66,37 @@ describe('AttributeData', () => {
attrs.fg |= Attributes.CM_P256 | 123;
assert.equal(attrs.getUnderlineColor(), 123);
});
it('getUnderlineColorMode / isUnderlineColorRGB / isUnderlineColorPalette / isUnderlineColorDefault', () => {
const attrs = new AttributeData();
// should always return color mode of fg
for (const mode of [Attributes.CM_DEFAULT, Attributes.CM_P16, Attributes.CM_P256, Attributes.CM_RGB]) {
attrs.extended.underlineColor = mode;
assert.equal(attrs.getUnderlineColorMode(), attrs.getFgColorMode());
assert.equal(attrs.isUnderlineColorDefault(), true);
}
attrs.fg = Attributes.CM_RGB;
for (const mode of [Attributes.CM_DEFAULT, Attributes.CM_P16, Attributes.CM_P256, Attributes.CM_RGB]) {
attrs.extended.underlineColor = mode;
assert.equal(attrs.getUnderlineColorMode(), attrs.getFgColorMode());
assert.equal(attrs.isUnderlineColorDefault(), false);
assert.equal(attrs.isUnderlineColorRGB(), true);
}
// should return own mode
attrs.bg |= BgFlags.HAS_EXTENDED;
attrs.extended.underlineColor = Attributes.CM_DEFAULT;
assert.equal(attrs.getUnderlineColorMode(), Attributes.CM_DEFAULT);
attrs.extended.underlineColor = Attributes.CM_P16;
assert.equal(attrs.getUnderlineColorMode(), Attributes.CM_P16);
assert.equal(attrs.isUnderlineColorPalette(), true);
attrs.extended.underlineColor = Attributes.CM_P256;
assert.equal(attrs.getUnderlineColorMode(), Attributes.CM_P256);
assert.equal(attrs.isUnderlineColorPalette(), true);
attrs.extended.underlineColor = Attributes.CM_RGB;
assert.equal(attrs.getUnderlineColorMode(), Attributes.CM_RGB);
assert.equal(attrs.isUnderlineColorRGB(), true);
});
it('getUnderlineStyle', () => {
const attrs = new AttributeData();