Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix error when line - context < 0, fix highlighting errors -- replace…

… ansi-color with local color, disable color if output is not a tty
  • Loading branch information...
commit 20b52fde576f34fcfdc1f6a72b2f09a834fd20f0 1 parent 06449d0
Chris Dickinson authored June 21, 2011

Showing 1 changed file with 95 additions and 6 deletions. Show diff stats Hide diff stats

  1. 101  trace.js
101  trace.js
... ...
@@ -1,6 +1,69 @@
1 1
 var fs = require('fs'),
2  
-    natives = process.binding('natives'),
3  
-    color = require('ansi-color').set;
  2
+    tty = require('tty'),
  3
+    natives = process.binding('natives');
  4
+
  5
+var is_tty = function() {
  6
+  return tty.isatty(process.stdout.fd) && tty.isatty(process.stderr.fd);
  7
+};
  8
+
  9
+var Color = function(code) {
  10
+  this.code = code;
  11
+};
  12
+
  13
+Color.prototype.start = function() {
  14
+  Color.stack.push(this);
  15
+  return '\033['+this.code+'m';
  16
+};
  17
+
  18
+Color.prototype.end = function() {
  19
+  Color.stack.pop();
  20
+  return '\033['+Color.stack[Color.stack.length-1].code+'m';
  21
+};
  22
+
  23
+Color.prototype.wrap = function(text) {
  24
+  return this.start() + text + this.end();
  25
+};
  26
+
  27
+var color = is_tty() ?
  28
+  function(code) {
  29
+    return new Color(code);
  30
+  } :
  31
+  function(code) {
  32
+    return {
  33
+      start:function(){return ''},
  34
+      end:function(){return ''},
  35
+      wrap:function(text){ return text; }
  36
+    }
  37
+  };
  38
+
  39
+var colors = {
  40
+  off:color(0),
  41
+  bold:color(1),
  42
+  italic:color(3),
  43
+  underline:color(4),
  44
+  blink:color(5),
  45
+  inverse:color(7),
  46
+  hidden:color(8),
  47
+  black:color(30),
  48
+  red:color(31),
  49
+  green:color(32),
  50
+  yellow:color(33),
  51
+  blue:color(34),
  52
+  magenta:color(35),
  53
+  cyan:color(36),
  54
+  white:color(37),
  55
+  black_bg:color(40),
  56
+  red_bg:color(41),
  57
+  green_bg:color(42),
  58
+  yellow_bg:color(43),
  59
+  blue_bg:color(44),
  60
+  magenta_bg:color(45),
  61
+  cyan_bg:color(46),
  62
+  white_bg:color(47)
  63
+};
  64
+
  65
+Color.stack = [colors.off];
  66
+
4 67
 
5 68
 var err_re1 = /    at ([^\s]+) \(([\w\d\._\-\/]+):(\d+):(\d+)\)/,
6 69
     err_re2 = /    at ([^:]+):(\d+):(\d+)/;
@@ -15,7 +78,7 @@ Trace.defaults = [2, true, 'red'];
15 78
 
16 79
 Trace.prototype.toString = function(reversed) {
17 80
   reversed === undefined && (reversed = true);
18  
-  var args = [].slice.call(args, 1);
  81
+  var args = [].slice.call(arguments, 1);
19 82
   args.length === 0 && (args = Trace.defaults.slice());
20 83
 
21 84
   var frame_data = [this.first_line, '======\n'].concat(this.frames.map(function(frame) {
@@ -65,8 +128,27 @@ Frame.prototype.filedata = function() {
65 128
 
66 129
 Frame.prototype.toString = function() {
67 130
   var args = [].slice.call(arguments);
68  
-  return 'file '+color('"'+this.filename.replace(process.cwd(), '.')+'"', 'cyan')+' line '+color(this.line, 'red+bold')+', char '+color(this.character, 'red+bold')+', in '+color(this.named_location, 'cyan')+':\n'+
69  
-    color(this.get_lines.apply(this, args), 'yellow')+'\n';
  131
+  return [
  132
+    'file ',
  133
+    colors.cyan.wrap('"'+this.filename.replace(process.cwd(), '.')+'"'),
  134
+    ' line ',
  135
+    colors.red.wrap(
  136
+      colors.bold.wrap(
  137
+        this.line
  138
+      )
  139
+    ),
  140
+    ', char ',
  141
+    colors.red.wrap(
  142
+      colors.bold.wrap(
  143
+        this.character
  144
+      )
  145
+    ),
  146
+    ', in ',
  147
+    colors.cyan.wrap(this.named_location),
  148
+    ':\n',
  149
+    colors.yellow.wrap(this.get_lines.apply(this, args)),
  150
+    '\n'
  151
+  ].join('');
70 152
 };
71 153
 
72 154
 Frame.prototype.get_lines = function(context, ascii_cursor, highlight_error_start) {
@@ -77,9 +159,15 @@ Frame.prototype.get_lines = function(context, ascii_cursor, highlight_error_star
77 159
       end_line = this.line + context,
78 160
       character = this.character;
79 161
 
  162
+  start_line < 0 && (start_line = 0);
80 163
   var lines = filedata.slice(start_line, end_line);
81 164
 
  165
+  if(this.line - context < 0) {
  166
+    context += (this.line - context) - 1;
  167
+  }
  168
+
82 169
   if(highlight_error_start) {
  170
+    colors.yellow.start();
83 171
     lines = lines.map(function(line, idx) {
84 172
       if(idx === context) {
85 173
         line = line.split(/\b/g);
@@ -87,7 +175,7 @@ Frame.prototype.get_lines = function(context, ascii_cursor, highlight_error_star
87 175
         line = line.map(function(word) {
88 176
           var next = start + word.length;
89 177
           if(character <= next && character >= start) {
90  
-            word = color(word, highlight_error_start);
  178
+            word = colors[highlight_error_start].wrap(word);
91 179
           }
92 180
           start = next;
93 181
           return word;
@@ -95,6 +183,7 @@ Frame.prototype.get_lines = function(context, ascii_cursor, highlight_error_star
95 183
       }
96 184
       return line;
97 185
     });
  186
+    colors.yellow.end();
98 187
   }
99 188
   if(ascii_cursor) {
100 189
     lines = lines.map(function(line, idx) {

0 notes on commit 20b52fd

Please sign in to comment.
Something went wrong with that request. Please try again.