Skip to content

Commit

Permalink
Simplify color config setting
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Jul 16, 2012
1 parent a7b1011 commit 7b4bb45
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
3 changes: 3 additions & 0 deletions doc/cli/config.md
Expand Up @@ -501,6 +501,9 @@ It cannot be set from the command line, but if you are using npm
programmatically, you may wish to send logs to somewhere other than
stderr.

If the `color` config is set to true, then this stream will receive
colored output if it is a TTY.

### long

* Default: false
Expand Down
2 changes: 1 addition & 1 deletion lib/help-search.js
Expand Up @@ -163,7 +163,7 @@ function helpSearch (args, silent, cb) {
})
out = newOut.join("")
}
if (npm.config.get("color")) {
if (npm.color) {
var color = "\033[31;40m"
, reset = "\033[0m"
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/ls.js
Expand Up @@ -205,7 +205,7 @@ function makeArchy (data, long, dir) {
}

function makeArchy_ (data, long, dir, depth, parent, d) {
var color = npm.config.get("color")
var color = npm.color
if (typeof data === "string") {
if (depth < npm.config.get("depth")) {
// just missing
Expand Down
21 changes: 20 additions & 1 deletion lib/npm.js
Expand Up @@ -262,17 +262,36 @@ function load (npm, conf, cb) {
//console.error("about to look up configs")

ini.resolveConfigs(conf, function (er) {
var color = npm.config.get("color")

log.level = npm.config.get("loglevel")
log.heading = "npm"
log.stream = npm.config.get("logstream")
switch (npm.config.get("color")) {
switch (color) {
case "always": log.enableColor(); break
case false: log.disableColor(); break
}
log.resume()

if (er) return cb(er)

// see if we need to color normal output
switch (color) {
case "always":
npm.color = true
break
case false:
npm.color = false
break
default:
var tty = require("tty")
if (process.stdout.isTTY) npm.color = true
else if (!tty.isatty) npm.color = true
else if (tty.isatty(1)) npm.color = true
else npm.color = false
break
}

// at this point the configs are all set.
// go ahead and spin up the registry client.
var token
Expand Down
4 changes: 2 additions & 2 deletions lib/search.js
Expand Up @@ -253,9 +253,9 @@ function addColorMarker (str, arg, i) {
function colorize (line) {
for (var i = 0; i < cl; i ++) {
var m = i + 1
var color = npm.config.get("color") ? "\033["+colors[i]+"m" : ""
var color = npm.color ? "\033["+colors[i]+"m" : ""
line = line.split(String.fromCharCode(m)).join(color)
}
var uncolor = npm.config.get("color") ? "\033[0m" : ""
var uncolor = npm.color ? "\033[0m" : ""
return line.split("\u0000").join(uncolor)
}

0 comments on commit 7b4bb45

Please sign in to comment.