Skip to content

Commit

Permalink
repl: REPLServer inherits from readline.Interface
Browse files Browse the repository at this point in the history
This exposes a setPrompt for and other readline features
  • Loading branch information
yorkie authored and tjfontaine committed Feb 18, 2014
1 parent 7589a00 commit 3ae0b17
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 39 deletions.
5 changes: 3 additions & 2 deletions doc/api/repl.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ For example, you could add this to your bashrc file:

## repl.start(options)

Returns and starts a `REPLServer` instance. Accepts an "options" Object that
takes the following values:
Returns and starts a `REPLServer` instance, that inherits from
[Readline Interface][]. Accepts an "options" Object that takes
the following values:

- `prompt` - the prompt and `stream` for all I/O. Defaults to `> `.

Expand Down
58 changes: 28 additions & 30 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ var path = require('path');
var fs = require('fs');
var rl = require('readline');
var Console = require('console').Console;
var EventEmitter = require('events').EventEmitter;
var domain = require('domain');
var debug = util.debuglog('repl');

Expand Down Expand Up @@ -82,8 +81,6 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
return new REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined);
}

EventEmitter.call(this);

var options, input, output, dom;
if (util.isObject(prompt)) {
// an options object was given
Expand All @@ -109,6 +106,9 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
self.useGlobal = !!useGlobal;
self.ignoreUndefined = !!ignoreUndefined;

// just for backwards compat, see github.com/joyent/node/pull/7127
self.rli = this;

eval_ = eval_ || defaultEval;

function defaultEval(code, context, file, cb) {
Expand Down Expand Up @@ -179,19 +179,18 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
self.bufferedCommand = '';
self.lines.level = [];

self.prompt = !util.isUndefined(prompt) ? prompt : '> ';

function complete(text, callback) {
self.complete(text, callback);
}

var rli = rl.createInterface({
input: self.inputStream,
output: self.outputStream,
completer: complete,
terminal: options.terminal
});
self.rli = rli;
rl.Interface.apply(this, [
self.inputStream,
self.outputStream,
complete,
options.terminal
])

self.setPrompt(!util.isUndefined(prompt) ? prompt : '> ');

this.commands = {};
defineDefaultCommands(this);
Expand All @@ -200,7 +199,7 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
self.writer = options.writer || exports.writer;

if (util.isUndefined(options.useColors)) {
options.useColors = rli.terminal;
options.useColors = self.terminal;
}
self.useColors = !!options.useColors;

Expand All @@ -211,24 +210,24 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
};
}

rli.setPrompt(self.prompt);
self.setPrompt(self._prompt);

rli.on('close', function() {
self.on('close', function() {
self.emit('exit');
});

var sawSIGINT = false;
rli.on('SIGINT', function() {
var empty = rli.line.length === 0;
rli.clearLine();
self.on('SIGINT', function() {
var empty = self.line.length === 0;
self.clearLine();

if (!(self.bufferedCommand && self.bufferedCommand.length > 0) && empty) {
if (sawSIGINT) {
rli.close();
self.close();
sawSIGINT = false;
return;
}
rli.output.write('(^C again to quit)\n');
self.output.write('(^C again to quit)\n');
sawSIGINT = true;
} else {
sawSIGINT = false;
Expand All @@ -239,7 +238,7 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
self.displayPrompt();
});

rli.on('line', function(cmd) {
self.on('line', function(cmd) {
debug('line %j', cmd);
sawSIGINT = false;
var skipCatchall = false;
Expand Down Expand Up @@ -322,13 +321,13 @@ function REPLServer(prompt, stream, eval_, useGlobal, ignoreUndefined) {
};
});

rli.on('SIGCONT', function() {
self.on('SIGCONT', function() {
self.displayPrompt(true);
});

self.displayPrompt();
}
inherits(REPLServer, EventEmitter);
inherits(REPLServer, rl.Interface);
exports.REPLServer = REPLServer;


Expand All @@ -340,7 +339,6 @@ exports.start = function(prompt, source, eval_, useGlobal, ignoreUndefined) {
return repl;
};


REPLServer.prototype.createContext = function() {
var context;
if (this.useGlobal) {
Expand Down Expand Up @@ -388,17 +386,17 @@ REPLServer.prototype.resetContext = function() {
};

REPLServer.prototype.displayPrompt = function(preserveCursor) {
var prompt = this.prompt;
var prompt = this._prompt;
if (this.bufferedCommand.length) {
prompt = '...';
var levelInd = new Array(this.lines.level.length).join('..');
prompt += levelInd + ' ';
} else {
this.setPrompt(prompt);
}
this.rli.setPrompt(prompt);
this.rli.prompt(preserveCursor);
this.prompt(preserveCursor);
};


// A stream to push an array into a REPL
// used in REPLServer.complete
function ArrayStream() {
Expand Down Expand Up @@ -838,7 +836,7 @@ function defineDefaultCommands(repl) {
repl.defineCommand('exit', {
help: 'Exit the repl',
action: function() {
this.rli.close();
this.close();
}
});

Expand Down Expand Up @@ -879,7 +877,7 @@ function defineDefaultCommands(repl) {
this.displayPrompt();
lines.forEach(function(line) {
if (line) {
self.rli.write(line + '\n');
self.write(line + '\n');
}
});
}
Expand Down
29 changes: 23 additions & 6 deletions test/simple/test-repl-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,23 @@ var r1 = repl.start({
output: stream,
terminal: true
});

assert.equal(r1.input, stream);
assert.equal(r1.output, stream);
assert.equal(r1.input, r1.inputStream);
assert.equal(r1.output, r1.outputStream);
assert.equal(r1.terminal, true);
assert.equal(r1.useColors, r1.terminal);
assert.equal(r1.useGlobal, false);
assert.equal(r1.ignoreUndefined, false);

// test r1 for backwards compact
assert.equal(r1.rli.input, stream);
assert.equal(r1.rli.output, stream);
assert.equal(r1.rli.input, r1.inputStream);
assert.equal(r1.rli.output, r1.outputStream);
assert.equal(r1.rli.terminal, true);
assert.equal(r1.useColors, r1.rli.terminal);
assert.equal(r1.useGlobal, false);
assert.equal(r1.ignoreUndefined, false);

// 2
function writer() {}
Expand All @@ -59,12 +68,20 @@ var r2 = repl.start({
eval: evaler,
writer: writer
});
assert.equal(r2.input, stream);
assert.equal(r2.output, stream);
assert.equal(r2.input, r2.inputStream);
assert.equal(r2.output, r2.outputStream);
assert.equal(r2.terminal, false);
assert.equal(r2.useColors, true);
assert.equal(r2.useGlobal, true);
assert.equal(r2.ignoreUndefined, true);
assert.equal(r2.writer, writer);

// test r2 for backwards compact
assert.equal(r2.rli.input, stream);
assert.equal(r2.rli.output, stream);
assert.equal(r2.rli.input, r2.inputStream);
assert.equal(r2.rli.output, r2.outputStream);
assert.equal(r2.rli.terminal, false);
assert.equal(r2.useColors, true);
assert.equal(r2.useGlobal, true);
assert.equal(r2.ignoreUndefined, true);
assert.equal(r2.writer, writer);

2 changes: 1 addition & 1 deletion test/simple/test-repl-require-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ var common = require('../common'),
require.cache.something = 1;
assert.equal(require.cache.something, 1);

repl.start({ useGlobal: false }).rli.close();
repl.start({ useGlobal: false }).close();

assert.equal(require.cache.something, 1);
49 changes: 49 additions & 0 deletions test/simple/test-repl-setprompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var common = require('../common'),
assert = require('assert'),
spawn = require('child_process').spawn,
os = require('os'),
util = require('util');

var args = [
'-e',
'var e = new (require("repl")).REPLServer("foo.. "); e.context.e = e;',
];

var p = "bar.. ";

var child = spawn(process.execPath, args);

child.stdout.setEncoding('utf8');

var data = '';
child.stdout.on('data', function(d) { data += d });

child.stdin.end(util.format("e.setPrompt('%s');%s", p, os.EOL));

child.on('close', function(code, signal) {
assert.strictEqual(code, 0);
assert.ok(!signal);
var lines = data.split(/\n/);
assert.strictEqual(lines.pop(), p);
});

0 comments on commit 3ae0b17

Please sign in to comment.