Skip to content

Commit

Permalink
Happy birthday to everyone.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Apr 4, 2014
1 parent 6823475 commit dbeb098
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ nbproject
############################
testApp
coverage


############################
# Other
############################
.node_history
46 changes: 46 additions & 0 deletions bin/sails-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = function () {
console.log();

var repl = REPL.start('sails> ');
history(repl, path.resolve(__dirname, '../.node_history'));
repl.on('exit', function(err) {
if (err) {
log.error(err);
Expand All @@ -62,3 +63,48 @@ module.exports = function () {

});
};

/**
* REPL History
* Pulled directly from https://github.com/tmpvar/repl.history
* with the slight tweak of setting historyIndex to -1 so that
* it works as expected.
*/
var fs = require('fs');
function history (repl, file) {

try {
var stat = fs.statSync(file);
repl.rli.history = fs.readFileSync(file, 'utf-8').split('\n').reverse();
repl.rli.history.shift();
repl.rli.historyIndex = -1;
} catch (e) {}

var fd = fs.openSync(file, 'a'), reval = repl.eval;

repl.rli.addListener('line', function(code) {
if (code && code !== '.history') {
fs.write(fd, code + '\n');
} else {
repl.rli.historyIndex++;
repl.rli.history.pop();
}
});

process.on('exit', function() {
fs.closeSync(fd);
});

repl.commands['.history'] = {
help : 'Show the history',
action : function() {
var out = [];
repl.rli.history.forEach(function(v, k) {
out.push(v);
});
repl.outputStream.write(out.reverse().join('\n') + '\n');
repl.displayPrompt();
}
};
};

0 comments on commit dbeb098

Please sign in to comment.