[debugger] requireConnection() returns bool, break UI
authorFedor Indutny <fedor.indutny@gmail.com>
Tue, 13 Sep 2011 14:09:18 +0000 (21:09 +0700)
committerRyan Dahl <ry@tinyclouds.org>
Wed, 14 Sep 2011 03:40:26 +0000 (20:40 -0700)
Stepping commands will overwrite output of previous step command

lib/_debugger.js

index 7c054cf..8c3d64b 100644 (file)
@@ -626,18 +626,12 @@ var helpMessage = 'Commands: ' + commands.join(', ');
 
 
 function SourceUnderline(sourceText, position) {
-  if (!sourceText) return;
-
-  var wrapper = require('module').wrapper[0];
-  if (sourceText.indexOf(wrapper) === 0) {
-    sourceText = sourceText.slice(wrapper.length);
-    position -= wrapper.length;
-  }
+  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 = '';
+  var underline = '    ';
   for (var i = 0; i < position; i++) {
     if (sourceText[i] == '\t') {
       underline += '\t';
@@ -648,7 +642,7 @@ function SourceUnderline(sourceText, position) {
   underline += '^';
 
   // Return the source line text with the underline beneath.
-  return sourceText + '\n' + underline;
+  return underline;
 }
 
 
@@ -751,8 +745,10 @@ Interface.prototype.resume = function(silent) {
 
 // Output
 Interface.prototype.print = function(text) {
-  process.stdout.cursorTo(0);
-  process.stdout.clearLine(1);
+  if (process.stdout.isTTY) {
+    process.stdout.cursorTo(0);
+    process.stdout.clearLine(1);
+  }
   process.stdout.write(typeof text === 'string' ? text : util.inspect(text));
   process.stdout.write('\n');
 };
@@ -775,17 +771,45 @@ Interface.prototype.handleBreak = function(r) {
   this.pause();
 
   this.client.currentSourceLine = r.sourceLine;
+  this.client.currentSourceLineText = r.sourceLineText;
+  this.client.currentSourceColumn = r.sourceColumn;
   this.client.currentFrame = 0;
   this.client.currentScript = r.script.name;
 
-  this.print(SourceInfo(r) + '\n' +
-             SourceUnderline(r.sourceLineText, r.sourceColumn));
+  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();
 };
 
 
 Interface.prototype.requireConnection = function() {
-  if (!this.client) this.error('App isn\'t running... Try `run` instead');
+  if (!this.client) {
+    this.error('App isn\'t running... Try `run` instead');
+    return false;
+  }
+  return true;
 };
 
 Interface.prototype.controlEval = function(code, context, filename, callback) {
@@ -896,7 +920,8 @@ Interface.prototype.restart = function() {
 
 // Print version
 Interface.prototype.version = function() {
-  this.requireConnection();
+  if (!this.requireConnection()) return;
+
   var self = this;
 
   this.pause();
@@ -908,12 +933,13 @@ Interface.prototype.version = function() {
 
 // List source code
 Interface.prototype.list = function() {
-  this.requireConnection();
+  if (!this.requireConnection()) return;
 
   var self = this,
       client = this.client,
-      from = client.currentSourceLine - 5,
-      to = client.currentSourceLine + 5;
+      delta = arguments[0] || 5,
+      from = client.currentSourceLine - delta + 1,
+      to = client.currentSourceLine + delta + 1;
 
   self.pause();
   client.reqSource(from, to, function(res) {
@@ -939,6 +965,8 @@ Interface.prototype.list = function() {
         }
         pointer += '>';
         self.print(pointer + ' ' + lines[i]);
+        self.print(SourceUnderline(client.currentSourceLineText,
+                                   client.currentSourceColumn));
       } else {
         self.print(leftPad(lineno) + ' ' + lines[i]);
       }
@@ -949,7 +977,7 @@ Interface.prototype.list = function() {
 
 // Print backtrace
 Interface.prototype.backtrace = function() {
-  this.requireConnection();
+  if (!this.requireConnection()) return;
 
   var self = this,
       client = this.client;
@@ -984,7 +1012,7 @@ Interface.prototype.backtrace = function() {
 
 // argument full tells if it should display internal node scripts or not
 Interface.prototype.scripts = function(displayNatives) {
-  this.requireConnection();
+  if (!this.requireConnection()) return;
 
   var client = this.client;
   var scripts = [];
@@ -1010,7 +1038,7 @@ Interface.prototype.scripts = function(displayNatives) {
 
 // Continue execution of script
 Interface.prototype.cont = function() {
-  this.requireConnection();
+  if (!this.requireConnection()) return;
   this.pause();
 
   var self = this;
@@ -1020,43 +1048,31 @@ Interface.prototype.cont = function() {
 };
 
 
-// Jump to next command
-Interface.prototype.next = function() {
-  this.requireConnection();
+// Step commands generator
+Interface.stepGenerator = function(type, count) {
+  return function() {
+    if (!this.requireConnection()) return;
 
-  this.pause();
+    var self = this;
 
-  var self = this;
-  this.client.step('next', 1, function(res) {
-    self.resume();
-  });
+    self.pause();
+    self.client.step(type, count, function(res) {
+      self.resume();
+    });
+  };
 };
 
 
-// Step in
-Interface.prototype.step = function() {
-  this.requireConnection();
+// Jump to next command
+Interface.prototype.next = Interface.stepGenerator('next', 1);
 
-  this.pause();
 
-  var self = this;
-  this.client.step('in', 1, function(res) {
-    self.resume();
-  });
-};
+// Step in
+Interface.prototype.step = Interface.stepGenerator('in', 1);
 
 
 // Step out
-Interface.prototype.out = function() {
-  this.requireConnection();
-
-  this.pause();
-
-  var self = this;
-  this.client.step('out', 1, function(res) {
-    self.resume();
-  });
-};
+Interface.prototype.out = Interface.stepGenerator('out', 1);
 
 
 // Add breakpoint
@@ -1083,7 +1099,8 @@ Interface.prototype.setBreakpoint = function(script, line, condition) {
 
 // Show breakpoints
 Interface.prototype.breakpoints = function() {
-  this.requireConnection();
+  if (!this.requireConnection()) return;
+
   this.pause();
   var self = this;
   this.client.listbreakpoints(function(res) {
@@ -1106,7 +1123,7 @@ Interface.prototype.kill = function() {
 
 // Activate debug repl
 Interface.prototype.repl = function() {
-  this.requireConnection();
+  if (!this.requireConnection()) return;
 
   var self = this;