Browse Source

Allow for two streams in REPL

Towards windows compatibility.
v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
b6dafc1a47
  1. 29
      lib/repl.js

29
lib/repl.js

@ -52,10 +52,19 @@ function REPLServer(prompt, stream) {
self.context = context; self.context = context;
self.bufferedCommand = ''; self.bufferedCommand = '';
self.stream = stream || process.openStdin(); if (stream) {
// We're given a duplex socket
self.outputStream = stream;
self.inputStream = stream;
} else {
self.outputStream = process.stdout;
self.inputStream = process.stdin;
process.stdin.resume();
}
self.prompt = prompt || '> '; self.prompt = prompt || '> ';
var rli = self.rli = rl.createInterface(self.stream, function(text) { var rli = self.rli = rl.createInterface(self.outputStream, function(text) {
return self.complete(text); return self.complete(text);
}); });
@ -81,7 +90,7 @@ function REPLServer(prompt, stream) {
} }
}); });
self.stream.addListener('data', function(chunk) { self.inputStream.addListener('data', function(chunk) {
rli.write(chunk); rli.write(chunk);
}); });
@ -98,7 +107,7 @@ function REPLServer(prompt, stream) {
if (self.parseREPLKeyword(keyword, rest) === true) { if (self.parseREPLKeyword(keyword, rest) === true) {
return; return;
} else { } else {
self.stream.write('Invalid REPL keyword\n'); self.outputStream.write('Invalid REPL keyword\n');
skipCatchall = true; skipCatchall = true;
} }
} }
@ -132,7 +141,7 @@ function REPLServer(prompt, stream) {
if (ret !== undefined) { if (ret !== undefined) {
context._ = ret; context._ = ret;
self.stream.write(exports.writer(ret) + '\n'); self.outputStream.write(exports.writer(ret) + '\n');
} }
self.bufferedCommand = ''; self.bufferedCommand = '';
@ -151,9 +160,9 @@ function REPLServer(prompt, stream) {
} catch (e) { } catch (e) {
// On error: Print the error and clear the buffer // On error: Print the error and clear the buffer
if (e.stack) { if (e.stack) {
self.stream.write(e.stack + '\n'); self.outputStream.write(e.stack + '\n');
} else { } else {
self.stream.write(e.toString() + '\n'); self.outputStream.write(e.toString() + '\n');
} }
self.bufferedCommand = ''; self.bufferedCommand = '';
} }
@ -163,7 +172,7 @@ function REPLServer(prompt, stream) {
}); });
rli.addListener('close', function() { rli.addListener('close', function() {
self.stream.destroy(); self.inputStream.destroy();
}); });
self.displayPrompt(); self.displayPrompt();
@ -465,7 +474,7 @@ function defineDefaultCommands(repl) {
repl.defineCommand('clear', { repl.defineCommand('clear', {
help: 'Break, and also clear the local context', help: 'Break, and also clear the local context',
action: function() { action: function() {
this.stream.write('Clearing context...\n'); this.outputStream.write('Clearing context...\n');
this.bufferedCommand = ''; this.bufferedCommand = '';
resetContext(); resetContext();
this.displayPrompt(); this.displayPrompt();
@ -485,7 +494,7 @@ function defineDefaultCommands(repl) {
var self = this; var self = this;
Object.keys(this.commands).sort().forEach(function(name) { Object.keys(this.commands).sort().forEach(function(name) {
var cmd = self.commands[name]; var cmd = self.commands[name];
self.stream.write(name + '\t' + (cmd.help || '') + '\n'); self.outputStream.write(name + '\t' + (cmd.help || '') + '\n');
}); });
this.displayPrompt(); this.displayPrompt();
} }

Loading…
Cancel
Save