[debugger] nicier output, clear line before writing
authorFedor Indutny <fedor.indutny@gmail.com>
Sat, 10 Sep 2011 10:33:07 +0000 (17:33 +0700)
committerFedor Indutny <fedor.indutny@gmail.com>
Sat, 10 Sep 2011 10:33:07 +0000 (17:33 +0700)
lib/_debugger.js

index 89b4392..90775ee 100644 (file)
@@ -655,7 +655,7 @@ function Interface() {
   var proto = Interface.prototype,
       ignored = ['pause', 'resume', 'exitRepl', 'handleBreak',
                  'requireConnection', 'killChild', 'trySpawn',
-                 'controlEval', 'debugEval'],
+                 'controlEval', 'debugEval', 'print'],
       synonym = {
         'run': 'r',
         'cont': 'c',
@@ -708,20 +708,23 @@ Interface.prototype.resume = function(silent) {
 };
 
 
-Interface.prototype.handleBreak = function(r) {
-  var expected = this.paused !== 0;
+// Output
+Interface.prototype.print = function(text) {
+  process.stdout.cursorTo(0);
+  process.stdout.clearLine(1);
+  process.stdout.write(text + '\n');
+};
+
 
+Interface.prototype.handleBreak = function(r) {
   this.pause();
 
   this.client.currentSourceLine = r.sourceLine;
   this.client.currentFrame = 0;
   this.client.currentScript = r.script.name;
 
-  if (!expected) {
-    console.log('');
-  }
-  console.log(SourceInfo(r));
-  console.log(SourceUnderline(r.sourceLineText, r.sourceColumn));
+  this.print(SourceInfo(r) + '\n' +
+             SourceUnderline(r.sourceLineText, r.sourceColumn));
   this.resume();
 };
 
@@ -801,7 +804,7 @@ function leftPad(n) {
 
 // Print help message
 Interface.prototype.help = function() {
-  console.log(helpMessage);
+  this.print(helpMessage);
 };
 
 
@@ -837,7 +840,7 @@ Interface.prototype.version = function() {
 
   this.pause();
   this.client.reqVersion(function(v) {
-    process.stdout.write(v);
+    self.print(v);
     self.resume();
   });
 };
@@ -872,9 +875,9 @@ Interface.prototype.list = function() {
           pointer += '=';
         }
         pointer += '>';
-        console.log(pointer + ' ' + lines[i]);
+        self.print(pointer + ' ' + lines[i]);
       } else {
-        console.log(leftPad(lineno) + ' ' + lines[i]);
+        self.print(leftPad(lineno) + ' ' + lines[i]);
       }
     }
     self.resume();
@@ -891,24 +894,25 @@ Interface.prototype.backtrace = function() {
   self.pause();
   client.fullTrace(function(bt) {
     if (bt.totalFrames == 0) {
-      console.log('(empty stack)');
+      self.print('(empty stack)');
     } else {
-      var text = '';
-      var firstFrameNative = bt.frames[0].script.isNative;
+      var trace = [],
+          firstFrameNative = bt.frames[0].script.isNative;
       for (var i = 0; i < bt.frames.length; i++) {
         var frame = bt.frames[i];
         if (!firstFrameNative && frame.script.isNative) break;
 
-        text += '#' + i + ' ';
+        var text = '#' + i + ' ';
         if (frame.func.inferredName && frame.func.inferredName.length > 0) {
           text += frame.func.inferredName + ' ';
         }
         text += require('path').basename(frame.script.name) + ':';
         text += (frame.line + 1) + ':' + (frame.column + 1);
-        text += '\n';
+
+        trace.push(text);
       }
 
-      console.log(text);
+      self.print(trace.join('\n'));
     }
     self.resume();
   });
@@ -920,7 +924,7 @@ Interface.prototype.scripts = function(displayNatives) {
   this.requireConnection();
 
   var client = this.client;
-  var text = '';
+  var scripts = [];
 
   this.pause();
   for (var id in client.scripts) {
@@ -929,12 +933,14 @@ Interface.prototype.scripts = function(displayNatives) {
       if (displayNatives ||
           script.name == client.currentScript ||
           !script.isNative) {
-        text += script.name == client.currentScript ? '* ' : '  ';
-        text += require('path').basename(script.name) + '\n';
+        scripts.push(
+          (script.name == client.currentScript ? '* ' : '  ') +
+          require('path').basename(script.name)
+        );
       }
     }
   }
-  console.log(text);
+  this.print(scripts.join('\n'));
   this.resume();
 };
 
@@ -1005,7 +1011,7 @@ Interface.prototype.breakpoints = function() {
   var self = this;
   this.client.listbreakpoints(function(res) {
     if (res.success) {
-      console.log(res.body);
+      self.print(res.body);
     } else {
       throw Error(res.message || 'Some error happened');
     }
@@ -1027,7 +1033,7 @@ Interface.prototype.repl = function() {
 
   var self = this;
 
-  console.log('Press Ctrl + C to leave debug repl');
+  self.print('Press Ctrl + C to leave debug repl');
 
   // Don't display any default messages
   var listeners = this.repl.rli.listeners('SIGINT');
@@ -1107,7 +1113,7 @@ Interface.prototype.trySpawn = function(cb) {
 
     client.on('close', function() {
       self.pause()
-      console.log('program terminated');
+      self.print('program terminated');
       self.resume();
       self.client = null;
       self.killChild();
@@ -1116,8 +1122,7 @@ Interface.prototype.trySpawn = function(cb) {
 
   client.on('unhandledResponse', function(res) {
     self.pause();
-    console.log('\r\nunhandled res:');
-    console.log(res);
+    self.print('\nunhandled res:' + JSON.stringify(res));
     self.resume();
   });