[debugger] requireConnection() returns bool, break UI

Stepping commands will overwrite output of previous step command
This commit is contained in:
Fedor Indutny 2011-09-13 21:09:18 +07:00 committed by Ryan Dahl
parent 57388d8b2e
commit 9fb186892c

View File

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