Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorisaacs <i@izs.me>2010-05-14 11:35:50 +0400
committerisaacs <i@izs.me>2010-05-14 11:35:50 +0400
commitf6bae58db8c9d461ccf8f8c0caca56679c4b0067 (patch)
tree50ad0050885674e52b5b857ff9fc4df691a5710c /lib
parenta991b294910881bd39f1f0667892f3ee3ded8e95 (diff)
Use pretty colors, but not when being piped.
Diffstat (limited to 'lib')
-rw-r--r--lib/ls.js20
1 files changed, 17 insertions, 3 deletions
diff --git a/lib/ls.js b/lib/ls.js
index 4c569e805..14fd60948 100644
--- a/lib/ls.js
+++ b/lib/ls.js
@@ -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) {