[debugger] color mark in _debugger, kill child on Ctrl+D

pull/22966/head
Fedor Indutny 2011-09-14 13:59:56 +07:00
parent 0e0fbf7e6a
commit f2ec46a7a7
1 changed files with 23 additions and 41 deletions

View 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 += ' ';
}
}
underline += '^';
var head = sourceText.slice(0, position),
tail = sourceText.slice(position);
// Return the source line text with the underline beneath.
return underline;
// Colourize char if stdout supports colours
if (process.stdout.isTTY) {
tail = tail.replace(/(.{1,}?)([^\w]|$)/, '\033[32m$1\033[39m$2');
}
// 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]);
}