[debugger] color mark in _debugger, kill child on Ctrl+D
authorFedor Indutny <fedor.indutny@gmail.com>
Wed, 14 Sep 2011 06:59:56 +0000 (13:59 +0700)
committerFedor Indutny <fedor.indutny@gmail.com>
Wed, 14 Sep 2011 16:05:01 +0000 (23:05 +0700)
lib/_debugger.js

index 8c3d64b..8424848 100644 (file)
@@ -628,21 +628,19 @@ var helpMessage = 'Commands: ' + commands.join(', ');
 function SourceUnderline(sourceText, position) {
   if (!sourceText) return '';
 
-  // Create an underline with a caret pointing to the source position. If the
-  // source contains a tab character the underline will have a tab character in
-  // the same place otherwise the underline will have a space character.
-  var underline = '    ';
-  for (var i = 0; i < position; i++) {
-    if (sourceText[i] == '\t') {
-      underline += '\t';
-    } else {
-      underline += ' ';
-    }
+  var head = sourceText.slice(0, position),
+      tail = sourceText.slice(position);
+
+  // Colourize char if stdout supports colours
+  if (process.stdout.isTTY) {
+    tail = tail.replace(/(.{1,}?)([^\w]|$)/, '\033[32m$1\033[39m$2');
   }
-  underline += '^';
 
-  // Return the source line text with the underline beneath.
-  return underline;
+  // Return source line with coloured char at `position`
+  return [
+    head,
+    tail
+  ].join('');
 }
 
 
@@ -681,6 +679,11 @@ function Interface() {
   this.repl = new repl.REPLServer('debug> ', null,
                                   this.controlEval.bind(this));
 
+  this.repl.rli.addListener('close', function() {
+    self.killed = true;
+    self.killChild();
+  });
+
   // Lift all instance methods to repl context
   var proto = Interface.prototype,
       ignored = ['pause', 'resume', 'exitRepl', 'handleBreak',
@@ -713,6 +716,7 @@ function Interface() {
     }
   }
 
+  this.killed = false;
   this.waiting = null;
   this.paused = 0;
   this.context = this.repl.context;
@@ -723,13 +727,13 @@ function Interface() {
 
 
 Interface.prototype.pause = function() {
-  if (this.paused++ > 0) return false;
+  if (this.killed || this.paused++ > 0) return false;
   this.repl.rli.pause();
   process.stdin.pause();
 };
 
 Interface.prototype.resume = function(silent) {
-  if (this.paused === 0 || --this.paused !== 0) return false;
+  if (this.killed || this.paused === 0 || --this.paused !== 0) return false;
   this.repl.rli.resume();
   if (silent !== true) {
     this.repl.displayPrompt();
@@ -745,6 +749,7 @@ Interface.prototype.resume = function(silent) {
 
 // Output
 Interface.prototype.print = function(text) {
+  if (this.killed) return;
   if (process.stdout.isTTY) {
     process.stdout.cursorTo(0);
     process.stdout.clearLine(1);
@@ -776,28 +781,6 @@ Interface.prototype.handleBreak = function(r) {
   this.client.currentFrame = 0;
   this.client.currentScript = r.script.name;
 
-  if (process.stdout.isTTY) {
-    var step = {
-          'next': true,
-          'step': true,
-          'out': true,
-          'n': true,
-          's': true,
-          'o': true
-        },
-        history = this.repl.rli.history;
-
-    // If current cmd is 'step' and previous was too
-    // Clear previous lines and overwrite them
-    if (step[history[0]] && step[history[1]]) {
-      for (var i = 0; i < 8; i++) {
-        process.stdout.clearLine(0);
-        process.stdout.moveCursor(0, -1);
-      }
-      process.stdout.clearLine(0);
-    }
-  }
-
   this.print(SourceInfo(r));
   this.list(2);
   this.resume();
@@ -964,9 +947,8 @@ Interface.prototype.list = function() {
           pointer += '=';
         }
         pointer += '>';
-        self.print(pointer + ' ' + lines[i]);
-        self.print(SourceUnderline(client.currentSourceLineText,
-                                   client.currentSourceColumn));
+        self.print(pointer + ' ' + SourceUnderline(lines[i],
+                                                   client.currentSourceColumn));
       } else {
         self.print(leftPad(lineno) + ' ' + lines[i]);
       }