index.js
function proxy(options, format) {
  var tty = options.tty
    , method = options.method
    , re = /(%[sdj])+/g
    , start
    , end;
  if(arguments.length === 1) {
    return method.apply(console, []);
  }
  var arg, i, replacing, replacements, matches, tag;
  replacing = (typeof format === 'string')
    && re.test(format) && arguments.length > 2;
  replacements = [].slice.call(arguments, 2);
  if(format instanceof AnsiColor) {
    replacing = true;
    if(!replacements.length) {
      replacements.unshift(format); format = '%s';
    }
  }
  if(!replacing) {
    replacements.unshift(format);
    return method.apply(console, replacements);
  }
  matches = (format && (typeof format.match === 'function')) ?
    format.match(re) : [];
  if(format instanceof AnsiColor) {
    if(!tty) {
      format = format.v;
    }else{
      tag = format.start(tty);
      format = format.valueOf(tty);
    }
  }

  //console.dir('is tty: ' + tty);
  if(tty) {
    re = /(%[sdj])/g;
    var fmt, result, j = 0;
    while((result = re.exec(format))) {
      if(j === replacements.length) {
        break;
      }
      arg = replacements[j];
      //console.dir('processing ansi replacement: ' + typeof(arg));
      fmt = result[1];
      start = format.substr(0, result.index);
      end = format.substr(result.index + result[0].length);
      //console.dir('re format: ' + fmt);
      //console.dir('re start: ' + start);
      //console.dir('re end: ' + end);
      if((arg instanceof AnsiColor)) {
        //console.dir('update arg value: ' + typeof(arg.v));
        if(fmt === '%j') {
          arg.v = JSON.stringify(arg.v, circular());
        }
        format = start + '%s' + end;
      }
      j++;
    }
  }

  for(i = 0;i < replacements.length;i++) {
    arg = replacements[i];
    if(arg instanceof AnsiColor) {
      replacements[i] = arg.valueOf(tty, tag);
    }
  }
  replacements.unshift(format);
  return method.apply(options.scope ? options.scope : console, replacements);
}
function proxy(options, format) {
  var tty = options.tty
    , method = options.method
    , re = /(%[sdj])+/g
    , start
    , end;
  if(arguments.length === 1) {
    return method.apply(console, []);
  }
  var arg, i, replacing, replacements, matches, tag;
  replacing = (typeof format === 'string')
    && re.test(format) && arguments.length > 2;
  replacements = [].slice.call(arguments, 2);
  if(format instanceof AnsiColor) {
    replacing = true;
    if(!replacements.length) {
      replacements.unshift(format); format = '%s';
    }
  }
  if(!replacing) {
    replacements.unshift(format);
    return method.apply(console, replacements);
  }
  matches = (format && (typeof format.match === 'function')) ?
    format.match(re) : [];
  if(format instanceof AnsiColor) {
    if(!tty) {
      format = format.v;
    }else{
      tag = format.start(tty);
      format = format.valueOf(tty);
    }
  }

  //console.dir('is tty: ' + tty);
  if(tty) {
    re = /(%[sdj])/g;
    var fmt, result, j = 0;
    while((result = re.exec(format))) {
      if(j === replacements.length) {
        break;
      }
      arg = replacements[j];
      //console.dir('processing ansi replacement: ' + typeof(arg));
      fmt = result[1];
      start = format.substr(0, result.index);
      end = format.substr(result.index + result[0].length);
      //console.dir('re format: ' + fmt);
      //console.dir('re start: ' + start);
      //console.dir('re end: ' + end);
      if((arg instanceof AnsiColor)) {
        //console.dir('update arg value: ' + typeof(arg.v));
        if(fmt === '%j') {
          arg.v = JSON.stringify(arg.v, circular());
        }
        format = start + '%s' + end;
      }
      j++;
    }
  }

  for(i = 0;i < replacements.length;i++) {
    arg = replacements[i];
    if(arg instanceof AnsiColor) {
      replacements[i] = arg.valueOf(tty, tag);
    }
  }
  replacements.unshift(format);
  return method.apply(options.scope ? options.scope : console, replacements);
}
function proxy ( options , format ) { var tty = options . tty , method = options . method , re = / (%[sdj])+ / g , start , end ; if ( arguments . length === 1 ) { return method . apply ( console , [ ] ) ; } var arg , i , replacing , replacements , matches , tag ; replacing = ( typeof format === 'string' ) && re . test ( format ) && arguments . length > 2 ; replacements = [ ] . slice . call ( arguments , 2 ) ; if ( format instanceof AnsiColor ) { replacing = true ; if ( ! replacements . length ) { replacements . unshift ( format ) ; format = '%s' ; } } if ( ! replacing ) { replacements . unshift ( format ) ; return method . apply ( console , replacements ) ; } matches = ( format && ( typeof format . match === 'function' ) ) ? format . match ( re ) : [ ] ; if ( format instanceof AnsiColor ) { if ( ! tty ) { format = format . v ; } else { tag = format . start ( tty ) ; format = format . valueOf ( tty ) ; } } //console.dir('is tty: ' + tty); if ( tty ) { re = / (%[sdj]) / g ; var fmt , result , j = 0 ; while ( ( result = re . exec ( format ) ) ) { if ( j === replacements . length ) { break ; } arg = replacements [ j ] ; //console.dir('processing ansi replacement: ' + typeof(arg)); fmt = result [ 1 ] ; start = format . substr ( 0 , result . index ) ; end = format . substr ( result . index + result [ 0 ] . length ) ; //console.dir('re format: ' + fmt); //console.dir('re start: ' + start); //console.dir('re end: ' + end); if ( ( arg instanceof AnsiColor ) ) { //console.dir('update arg value: ' + typeof(arg.v)); if ( fmt === '%j' ) { arg . v = JSON . stringify ( arg . v , circular ( ) ) ; } format = start + '%s' + end ; } j ++ ; } } for ( i = 0 ; i < replacements . length ; i ++ ) { arg = replacements [ i ] ; if ( arg instanceof AnsiColor ) { replacements [ i ] = arg . valueOf ( tty , tag ) ; } } replacements . unshift ( format ) ; return method . apply ( options . scope ? options . scope : console , replacements ) ; }
Escapes replacement values.

@param options Proxy configuration options.
@param options.tty A boolean indicating whether the output is a tty.
@param options.method A method to proxy to.
@param options.stream A writable stream to write to.

@param format The format string.
@param ... The format string arguments.
Escapes replacement values .