forked from twosigma/git-meta
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add color.status config support to address twosigma#713
- Loading branch information
Showing
6 changed files
with
239 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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) { | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const assert = require("chai").assert; | ||
const ColorHandler = require("../../lib/util/color_handler").ColorHandler; | ||
|
||
const realProcess = process; | ||
describe("ColorHandler", function() { | ||
describe("colors", function() { | ||
describe("enableColor = 'auto'", function() { | ||
afterEach(function() { | ||
// this test futzes around with the process global so | ||
// make sure it is properly reset after each test. | ||
global.process = realProcess; | ||
}); | ||
it("enables color when in a TTY", function() { | ||
global.process = {stdout: {isTTY: true}}; | ||
const colors = new ColorHandler("auto"); | ||
assert.isTrue(colors.enableColor); | ||
}); | ||
|
||
it("disables color when not in a TTY", function() { | ||
global.process = {stdout: {isTTY: false}}; | ||
const colors = new ColorHandler("auto"); | ||
assert.isFalse(colors.enableColor); | ||
assert.equal("test", colors.blue("test")); | ||
}); | ||
|
||
it("adds colors if enableColor", function() { | ||
const colors = new ColorHandler(); | ||
colors.enableColor = true; | ||
assert.equal("\u001b[34mtest\u001b[39m", colors.blue("test")); | ||
}); | ||
|
||
it("passes through string if !enableColor", function() { | ||
const colors = new ColorHandler(); | ||
colors.enableColor = false; | ||
assert.equal("test", colors.blue("test")); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters