Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

repl history now saved to ~/.node_history

When the REPL starts it will check for ~/.node_history and load it if it
exists and is json parseable.

Upon exit, it stringifies the history and saves it.
  • Loading branch information...
commit 8ffe3561dc644e76e131ec31743c30d8577a7a21 1 parent c2ad828
@regality authored
Showing with 58 additions and 3 deletions.
  1. +18 −2 lib/readline.js
  2. +7 −0 lib/repl.js
  3. +33 −1 src/node.js
View
20 lib/readline.js
@@ -25,7 +25,7 @@
// * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
// * http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html
-var kHistorySize = 30;
+var kHistorySize = 100;
var kBufSize = 10 * 1024;
var util = require('util');
@@ -183,7 +183,11 @@ Interface.prototype._onLine = function(line) {
Interface.prototype._addHistory = function() {
if (this.line.length === 0) return '';
- this.history.unshift(this.line);
+ // Only add it if it is not identical to the previous
+ if (this.history.length === 0 || this.line !== this.history[0]) {
+ this.history.unshift(this.line);
+ }
+
this.historyIndex = -1;
// Only store so many
@@ -500,6 +504,18 @@ Interface.prototype._line = function() {
};
+Interface.prototype.getHistory = function() {
+ return this.history;
+};
+
+Interface.prototype.setHistory = function(history) {
+ this.history = history;
+ this.historyIndex = -1;
+ if (this.history.length > kHistorySize) {
+ this.history.splice(kHistorySize, Infinity);
+ }
+};
+
Interface.prototype._historyNext = function() {
if (this.historyIndex > 0) {
this.historyIndex--;
View
7 lib/repl.js
@@ -363,6 +363,13 @@ REPLServer.prototype.displayPrompt = function(preserveCursor) {
this.rli.prompt(preserveCursor);
};
+REPLServer.prototype.setHistory = function(history) {
+ this.rli.setHistory(history);
+};
+
+REPLServer.prototype.getHistory = function() {
+ return this.rli.getHistory();
+};
// A stream to push an array into a REPL
// used in REPLServer.complete
View
34 src/node.js
@@ -137,8 +137,11 @@
}
var repl = Module.requireRepl().start(opts);
+ loadReplHistory(repl);
repl.on('exit', function() {
- process.exit();
+ saveReplHistory(repl, function() {
+ process.exit();
+ });
});
for (var i in homeContext) {
@@ -184,6 +187,35 @@
return context;
}
+ var nodeHistory;
+
+ function loadReplHistory(repl) {
+ var fs = NativeModule.require('fs');
+ var path = NativeModule.require('path');
+ var home = process.env.HOME;
+
+ if (!home) return;
+ nodeHistory = path.join(home, ".node_history");
+
+ fs.readFile(nodeHistory, function(err, data) {
+ if (err) return;
+ try {
+ var history = JSON.parse(data);
+ repl.setHistory(history);
+ } catch (e) { }
+ });
+ }
+
+ function saveReplHistory(repl, callback) {
+ var fs = NativeModule.require('fs');
+ var history = JSON.stringify(repl.getHistory(), false, 1);
+ if (!nodeHistory) return callback();
+
+ fs.writeFile(nodeHistory, history, function(err) {
+ callback();
+ });
+ }
+
startup.globalVariables = function() {
global.process = process;
global.global = global;
Please sign in to comment.
Something went wrong with that request. Please try again.