Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed output redirection bug + custom colors

  • Loading branch information...
commit fafddbae0855ab9e836f1c7500efe78b841defdf 1 parent 7ba7f97
@kilianc kilianc authored
Showing with 31 additions and 6 deletions.
  1. +18 −1 README.md
  2. +13 −5 console-trace.js
View
19 README.md
@@ -22,7 +22,7 @@ require('console-trace')([options])
* __always__ - (`Boolean`: defaults to false) always print the callsite info even without accessing methods from the `t` or `traced` getters.
* __cwd__ - (`String`: defaults to `process.cwd()`) the path that will be stripped from the callsite info
-* __color__ - (`Boolean`: defaults to true) terminal colors support flag
+* __colors__ - (`Boolean|Object`: defaults to true) terminal colors support flag or a custom color object
* __right__ - (`Boolean`: defaults to false) callsite alignment flag, when true prints infos on the right
### Examples:
@@ -65,6 +65,23 @@ console.log('a'); // tracing right
console.error('a'); // tracing right
```
+You can change defaults colors too
+
+```javascript
+require('./console-trace')({
+ always: true,
+ colors: {
+ warn: '35',
+ info: '32'
+ }
+})
+
+...
+
+console.warn('a'); // magenta
+console.info('a'); // green
+```
+
To customize the string that's prefixed to the calls, override the
`console.traceFormat` function.
View
18 console-trace.js
@@ -4,7 +4,9 @@
*/
var callsite = require('callsite')
- , isatty = require('tty').isatty()
+ , tty = require('tty')
+ , isatty = Boolean(tty.isatty() && process.stdout.getWindowSize)
+ , defaultColors = { log: '90', error: '91', warn: '93', info: '96' }
console.traceOptions = Object.create(null);
console.traceOptions.cwd = process.cwd() + '/';
@@ -32,7 +34,7 @@ module.exports = function (options) {
var fn = console[name];
console[name] = function () {
if (console._trace || console.traceOptions.always) {
- var pad = (arguments[0] && !console.traceOptions.right || !console.traceOptions.colors || !isatty ? ' ' : '');
+ var pad = (arguments[0] && !console.traceOptions.right || !isatty ? ' ' : '');
arguments[0] = console.traceFormat(__stack[1], name) + pad + arguments[0];
}
console._trace = false;
@@ -51,19 +53,25 @@ module.exports = function (options) {
console.traceFormat = function (call, method) {
var basename = call.getFileName().replace(console.traceOptions.cwd, '')
, str = '[' + basename + ':' + call.getLineNumber() + ']'
+ , color = '99'
- if (!console.traceOptions.colors || !isatty) {
+ if (!isatty) {
return str;
}
+
+ if (console.traceOptions.colors !== false) {
+ color = console.traceOptions.colors === true ? defaultColors[method] : console.traceOptions.colors[method];
+ }
+
if (console.traceOptions.right) {
var rowWidth = process.stdout.getWindowSize()[0];
return '\033[s' + // save current position
'\033[' + rowWidth + 'D' + // move to the start of the line
'\033[' + (rowWidth - str.length) + 'C' + // align right
- '\033[' + ('error' == method ? '91' : '90') + 'm' + str + '\033[39m' +
+ '\033[' + color + 'm' + str + '\033[39m' +
'\033[u'; // restore current position
} else {
- return '\033[' + ('error' == method ? '91' : '90') + 'm' + str + '\033[39m';
+ return '\033[' + color + 'm' + str + '\033[39m';
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.