Permalink
Browse files

Disable color if tty doesn't like it

  • Loading branch information...
davglass committed Sep 19, 2012
1 parent cf6c5f1 commit 7abe14c15a08ca11028cf9d2877f1aa520e71deb
Showing with 15 additions and 4 deletions.
  1. +15 −4 lib/log.js
View
@@ -4,17 +4,28 @@ Code licensed under the BSD License:
http://yuilibrary.com/license/
*/
var color = require('ansi-color').set;
+var hasColor = false;
+var stdio;
+try {
+ stdio = require("stdio");
+ hasColor = stdio.isStderrATTY();
+} catch (ex) {
+ hasColor = true;
+}
-var prefix = color('shifter', 'magenta');
-
exports.color = function (str, code) {
+ if (!hasColor) {
+ return str;
+ }
if (code === 'gray' || code === 'grey') {
code = 'white';
}
return color(str, code);
};
+var prefix = exports.color('shifter', 'magenta');
+
exports.info = function (str) {
console.log(prefix, color('[info]', 'white'), str);
};
@@ -28,10 +39,10 @@ exports.warn = function (str) {
};
exports.error = function (str) {
- console.log(prefix, color('[error]', 'red'), str);
+ console.error(prefix, color('[error]', 'red'), str);
process.exit(1);
};
exports.err = function (str) {
- console.log(prefix, color('[err]', 'red'), str);
+ console.error(prefix, color('[err]', 'red'), str);
};

0 comments on commit 7abe14c

Please sign in to comment.