-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathcolor_handler.js
More file actions
40 lines (35 loc) · 1.24 KB
/
color_handler.js
File metadata and controls
40 lines (35 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const colorsSafe = require("colors/safe");
/**
* This class is a wrapper around colors/safe that uses a color setting from
* the git meta config to determine if colors should be enabled.
*/
class ColorHandler {
/**
* @param {Bool|"auto"|null} enableColor
*
* NOTE: enableColor should probably be set based on a value in
* ConfigUtil.getConfigColorBool()
*/
constructor(enableColor) {
colorsSafe.enable();
if(enableColor === "auto" || enableColor === undefined) {
// Enable color if we're outputting to a terminal, otherwise disable
// since we're piping the output.
enableColor = process.stdout.isTTY === true;
}
this.enableColor = enableColor;
let self = this;
// add a passthrough function for each supported color
["blue", "cyan", "green", "grey", "magenta", "red", "yellow"].forEach(
function(color) {
self[color] = function(string) {
if(self.enableColor) {
return colorsSafe[color](string);
} else {
return string;
}
};
});
}
}
exports.ColorHandler = ColorHandler;