Skip to content

Commit 8f2a4eb

Browse files
stevenmhuntmedikoo
authored andcommitted
feat: Support NO_COLOR standard
https://no-color.org/ BREAKING CHANGE: ANSI color codes won't be generated for output, when NO_COLOR env var is set
1 parent c8c975a commit 8f2a4eb

File tree

7 files changed

+80
-12
lines changed

7 files changed

+80
-12
lines changed

bare.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var d = require("d")
1515
, max = Math.max
1616
, min = Math.min
1717
, variantModes = primitiveSet("_fg", "_bg")
18+
, supportsColor = require("./lib/supports-color")
1819
, xtermMatch
1920
, getFn;
2021

@@ -85,6 +86,9 @@ getFn = function () {
8586
null,
8687
true
8788
);
89+
if (!supportsColor.isColorSupported()) {
90+
return msg;
91+
}
8892
return start + msg + end;
8993
},
9094
proto

lib/supports-color.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
3+
// store whether supports-color mode is enabled or not.
4+
var state = null;
5+
6+
// force supports-color mode
7+
var enableColor = function () { state = true; };
8+
9+
// disable supports-color mode
10+
var disableColor = function () { state = false; };
11+
12+
// use the NO_COLOR environment variable (default)
13+
var autoDetectSupport = function () { state = null; };
14+
15+
// determine whether supports-color mode is enabled.
16+
var isColorSupported = function () {
17+
if (state === null) {
18+
return !process.env.NO_COLOR;
19+
}
20+
return state;
21+
};
22+
23+
module.exports = {
24+
enableColor: enableColor, disableColor: disableColor, autoDetectSupport: autoDetectSupport, isColorSupported: isColorSupported
25+
};

test/_lib/supports-color-wrapper.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
var supportsColor = require("../../lib/supports-color");
3+
4+
// a wrapper function which automatically forces color mode to enabled during the tests.
5+
module.exports = function (fn) {
6+
return function (t, a) {
7+
supportsColor.enableColor();
8+
try {
9+
return fn(t, a);
10+
}
11+
finally {
12+
supportsColor.autoDetectSupport();
13+
}
14+
};
15+
};

test/art.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"use strict";
22

33
var clc = require("../");
4+
var wrapper = require("./_lib/supports-color-wrapper");
45

5-
module.exports = function (t, a) {
6+
module.exports = wrapper(function (t, a) {
67
a(
78
t("ooo", { o: clc.yellow("x") }), "\x1b[33mx\x1b[39m\x1b[33mx\x1b[39m\x1b[33mx\x1b[39m",
89
"Basic art"
@@ -19,4 +20,4 @@ module.exports = function (t, a) {
1920
a(t("o\no", { o: clc.yellow("x") }), "\x1b[33mx\x1b[39m\n\x1b[33mx\x1b[39m", "Multiline art");
2021

2122
a(t("ooo", {}), "ooo", "Only text art");
22-
};
23+
});

test/bare.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"use strict";
2+
var supportsColor = require("../lib/supports-color");
3+
var wrapper = require("./_lib/supports-color-wrapper");
24

3-
module.exports = function (t, a) {
5+
module.exports = wrapper(function (t, a) {
46
var x, y;
57

68
a(t("test"), "test", "Plain");
@@ -397,4 +399,9 @@ module.exports = function (t, a) {
397399
"Xterm"
398400
);
399401
}
400-
};
402+
403+
supportsColor.disableColor();
404+
a(t.red("foo"), "foo", "Foreground with NO_COLOR");
405+
a(t.bgRed("foo", "bar"), "foo bar", "Background with NO_COLOR");
406+
a(t.blue.bgYellow("foo", "bar"), "foo bar", "Foreground & Background with NO_COLOR");
407+
});

test/lib/supports-color.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
var wrapper = require("../_lib/supports-color-wrapper");
3+
4+
module.exports = wrapper(function (t, a) {
5+
t.enableColor();
6+
a(t.isColorSupported(), true, "Enable color support NO_COLOR");
7+
t.disableColor();
8+
a(t.isColorSupported(), false, "Disable color support NO_COLOR");
9+
t.autoDetectSupport();
10+
a(t.isColorSupported(), !process.env.NO_COLOR, "use NO_COLOR env variable");
11+
});

test/throbber.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@ module.exports = {
2626
t.stderr.on("data", function (data) { err += data; });
2727
t.on("exit", function () {
2828
a.ok(out.length > 4, "Interval");
29-
a(
30-
startsWith.call(
31-
out.join(""),
32-
"START\x1b[31m-\x1b[39m\x1b[31m\b\\\x1b" +
33-
"[39m\x1b[31m\b|\x1b[39m\x1b[31m\b/\x1b[39m\x1b[31m\b-\x1b[39m"
34-
),
35-
true, "Output"
36-
);
29+
var isNoColor = Boolean(process.env.NO_COLOR);
30+
if (isNoColor) {
31+
a(startsWith.call(out.join(""), "START"), true, "Output");
32+
} else {
33+
a(
34+
startsWith.call(
35+
out.join(""),
36+
"START\x1b[31m-\x1b[39m\x1b[31m\b\\\x1b" +
37+
"[39m\x1b[31m\b|\x1b[39m\x1b[31m\b/\x1b[39m\x1b[31m\b-\x1b[39m"
38+
),
39+
true, "Output"
40+
);
41+
}
3742
a(err, "", "No stderr output");
3843
d();
3944
});

0 commit comments

Comments
 (0)