diff options
author | isaacs <i@izs.me> | 2010-05-14 11:35:50 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2010-05-14 11:35:50 +0400 |
commit | f6bae58db8c9d461ccf8f8c0caca56679c4b0067 (patch) | |
tree | 50ad0050885674e52b5b857ff9fc4df691a5710c /lib | |
parent | a991b294910881bd39f1f0667892f3ee3ded8e95 (diff) |
Use pretty colors, but not when being piped.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ls.js | 20 |
1 files changed, 17 insertions, 3 deletions
@@ -12,8 +12,7 @@ var npm = require("../npm") function ls (args, cb) { readInstalled([], function (er, installed) { registry.get(function (er, remote) { - var merged = merge(installed, remote) - , pretty = prettify(merged) + var pretty = prettify(merge(installed, remote), args) , stdout = process.stdout stdout.write(pretty) stdout.flush() @@ -21,7 +20,7 @@ function ls (args, cb) { }) } function strcmp (a, b) { return a > b ? 1 : -1 } -function prettify (data) { +function prettify (data, args) { var pkgs = Object.keys(data).sort(strcmp) , attrs = [] , names = [] @@ -48,6 +47,21 @@ function prettify (data) { names[n] += space.substr(0, maxNameLen - names[n].length) pretty.push(names[n] + " " + attrs[n]) } + // don't color if it's piping to some other process. + var doColor = !process.binding("stdio").isStdoutBlocking() + , colors = [36, 32, 33, 31, 35 ] + , c = 0 + , l = colors.length + if (args) args.forEach(function (arg) { + pretty = pretty.filter(function (line) { return line.match(arg) }) + if (doColor) { + pretty = pretty.map(function (line) { + return line.split(arg).join("\033["+colors[c]+"m" + arg + "\033[m") + }) + c = (c + 1) % l + } + }) + if (!pretty.length) pretty = ["Nothing found"] return pretty.join("\n") } function merge (installed, remote) { |