Skip to content

Commit

Permalink
Fix ansi256 enumerations (#46)
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Junon <junon@zeit.co>
  • Loading branch information
2 people authored and sindresorhus committed Mar 2, 2018
1 parent 6498e13 commit 1ac7472
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
23 changes: 18 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,43 @@ function assembleStyles() {
});
}

const ansi2ansi = n => n;
const rgb2rgb = (r, g, b) => [r, g, b];

styles.color.close = '\u001B[39m';
styles.bgColor.close = '\u001B[49m';

styles.color.ansi = {};
styles.color.ansi256 = {};
styles.color.ansi = {
ansi: wrapAnsi16(ansi2ansi, 0)
};
styles.color.ansi256 = {
ansi256: wrapAnsi256(ansi2ansi, 0)
};
styles.color.ansi16m = {
rgb: wrapAnsi16m(rgb2rgb, 0)
};

styles.bgColor.ansi = {};
styles.bgColor.ansi256 = {};
styles.bgColor.ansi = {
ansi: wrapAnsi16(ansi2ansi, 10)
};
styles.bgColor.ansi256 = {
ansi256: wrapAnsi256(ansi2ansi, 10)
};
styles.bgColor.ansi16m = {
rgb: wrapAnsi16m(rgb2rgb, 10)
};

for (const key of Object.keys(colorConvert)) {
for (let key of Object.keys(colorConvert)) {
if (typeof colorConvert[key] !== 'object') {
continue;
}

const suite = colorConvert[key];

if (key === 'ansi16') {
key = 'ansi';
}

if ('ansi16' in suite) {
styles.color.ansi[key] = wrapAnsi16(suite.ansi16, 0);
styles.bgColor.ansi[key] = wrapAnsi16(suite.ansi16, 10);
Expand Down
33 changes: 30 additions & 3 deletions test.js → test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import style from '.';
import style from '..';

test('return ANSI escape codes', t => {
t.is(style.green.open, '\u001B[32m');
Expand All @@ -20,13 +20,36 @@ test('groups should not be enumerable', t => {
});

test('don\'t pollute other objects', t => {
const obj1 = require('.');
const obj2 = require('.');
const obj1 = require('..');
const obj2 = require('..');

obj1.foo = true;
t.not(obj1.foo, obj2.foo);
});

test('all color types are always available', t => {
const ansi = style.color.ansi;
const ansi256 = style.color.ansi256;
const ansi16m = style.color.ansi16m;

t.truthy(ansi);
t.truthy(ansi.ansi);
t.truthy(ansi.ansi256);

t.truthy(ansi256);
t.truthy(ansi256.ansi);
t.truthy(ansi256.ansi256);

t.truthy(ansi16m);
t.truthy(ansi16m.ansi);
t.truthy(ansi16m.ansi256);

// There are no such things as ansi16m source colors
t.falsy(ansi.ansi16m);
t.falsy(ansi256.ansi16m);
t.falsy(ansi16m.ansi16m);
});

test('support conversion to ansi (16 colors)', t => {
t.is(style.color.ansi.rgb(255, 255, 255), '\u001B[97m');
t.is(style.color.ansi.hsl(140, 100, 50), '\u001B[92m');
Expand Down Expand Up @@ -75,3 +98,7 @@ test('export raw ANSI escape codes', t => {
t.is(style.codes.get(40), 49);
t.is(style.codes.get(100), 49);
});

test('rgb -> truecolor is stubbed', t => {
t.is(style.color.ansi16m.rgb(123, 45, 67), '\u001B[38;2;123;45;67m');
});

0 comments on commit 1ac7472

Please sign in to comment.