Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Added a result history to REPL #1120

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/repl.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module.paths = require('module')._nodeModulePaths(module.filename);
// Can overridden with custom print functions, such as `probe` or `eyes.js` // Can overridden with custom print functions, such as `probe` or `eyes.js`
exports.writer = util.inspect; exports.writer = util.inspect;


var resultCache;


function REPLServer(prompt, stream) { function REPLServer(prompt, stream) {
var self = this; var self = this;
Expand Down Expand Up @@ -150,7 +151,7 @@ function REPLServer(prompt, stream) {
// '{ a : 1 }' // '{ a : 1 }'
// and statements e.g. // and statements e.g.
// 'for (var i = 0; i < 10; i++) console.log(i);' // 'for (var i = 0; i < 10; i++) console.log(i);'

self.context.$ = resultCache;
var ret, success = false; var ret, success = false;
try { try {
// First we attempt to eval as expression with parens. // First we attempt to eval as expression with parens.
Expand All @@ -169,6 +170,9 @@ function REPLServer(prompt, stream) {
} }


if (ret !== undefined) { if (ret !== undefined) {
if (resultCache) {
resultCache.unshift(ret);
}
self.context._ = ret; self.context._ = ret;
self.outputStream.write(exports.writer(ret) + '\n'); self.outputStream.write(exports.writer(ret) + '\n');
} }
Expand Down Expand Up @@ -546,6 +550,20 @@ function defineDefaultCommands(repl) {
this.displayPrompt(); this.displayPrompt();
} }
}); });

repl.defineCommand('$', {
help:'Toggle the results array in $',
action:function() {
if (resultCache) {
resultCache = undefined;
this.outputStream.write('$ result array off\n');
} else {
resultCache = [];
this.outputStream.write('$ result array on\n');
}
this.displayPrompt();
}
});
} }




Expand Down