Skip to content

Commit 1ac7472

Browse files
Kayla WashburnQix-
authored andcommitted
Fix ansi256 enumerations (#46)
Co-authored-by: Josh Junon <[email protected]>
1 parent 6498e13 commit 1ac7472

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

index.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,43 @@ function assembleStyles() {
102102
});
103103
}
104104

105+
const ansi2ansi = n => n;
105106
const rgb2rgb = (r, g, b) => [r, g, b];
106107

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

110-
styles.color.ansi = {};
111-
styles.color.ansi256 = {};
111+
styles.color.ansi = {
112+
ansi: wrapAnsi16(ansi2ansi, 0)
113+
};
114+
styles.color.ansi256 = {
115+
ansi256: wrapAnsi256(ansi2ansi, 0)
116+
};
112117
styles.color.ansi16m = {
113118
rgb: wrapAnsi16m(rgb2rgb, 0)
114119
};
115120

116-
styles.bgColor.ansi = {};
117-
styles.bgColor.ansi256 = {};
121+
styles.bgColor.ansi = {
122+
ansi: wrapAnsi16(ansi2ansi, 10)
123+
};
124+
styles.bgColor.ansi256 = {
125+
ansi256: wrapAnsi256(ansi2ansi, 10)
126+
};
118127
styles.bgColor.ansi16m = {
119128
rgb: wrapAnsi16m(rgb2rgb, 10)
120129
};
121130

122-
for (const key of Object.keys(colorConvert)) {
131+
for (let key of Object.keys(colorConvert)) {
123132
if (typeof colorConvert[key] !== 'object') {
124133
continue;
125134
}
126135

127136
const suite = colorConvert[key];
128137

138+
if (key === 'ansi16') {
139+
key = 'ansi';
140+
}
141+
129142
if ('ansi16' in suite) {
130143
styles.color.ansi[key] = wrapAnsi16(suite.ansi16, 0);
131144
styles.bgColor.ansi[key] = wrapAnsi16(suite.ansi16, 10);

test.js renamed to test/test.js

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import style from '.';
2+
import style from '..';
33

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

2222
test('don\'t pollute other objects', t => {
23-
const obj1 = require('.');
24-
const obj2 = require('.');
23+
const obj1 = require('..');
24+
const obj2 = require('..');
2525

2626
obj1.foo = true;
2727
t.not(obj1.foo, obj2.foo);
2828
});
2929

30+
test('all color types are always available', t => {
31+
const ansi = style.color.ansi;
32+
const ansi256 = style.color.ansi256;
33+
const ansi16m = style.color.ansi16m;
34+
35+
t.truthy(ansi);
36+
t.truthy(ansi.ansi);
37+
t.truthy(ansi.ansi256);
38+
39+
t.truthy(ansi256);
40+
t.truthy(ansi256.ansi);
41+
t.truthy(ansi256.ansi256);
42+
43+
t.truthy(ansi16m);
44+
t.truthy(ansi16m.ansi);
45+
t.truthy(ansi16m.ansi256);
46+
47+
// There are no such things as ansi16m source colors
48+
t.falsy(ansi.ansi16m);
49+
t.falsy(ansi256.ansi16m);
50+
t.falsy(ansi16m.ansi16m);
51+
});
52+
3053
test('support conversion to ansi (16 colors)', t => {
3154
t.is(style.color.ansi.rgb(255, 255, 255), '\u001B[97m');
3255
t.is(style.color.ansi.hsl(140, 100, 50), '\u001B[92m');
@@ -75,3 +98,7 @@ test('export raw ANSI escape codes', t => {
7598
t.is(style.codes.get(40), 49);
7699
t.is(style.codes.get(100), 49);
77100
});
101+
102+
test('rgb -> truecolor is stubbed', t => {
103+
t.is(style.color.ansi16m.rgb(123, 45, 67), '\u001B[38;2;123;45;67m');
104+
});

0 commit comments

Comments
 (0)