Skip to content

Commit

Permalink
Merge pull request #206 from milushov/persistent-history
Browse files Browse the repository at this point in the history
Persistent commands history
  • Loading branch information
Charlie Somerville committed Sep 25, 2013
2 parents f9c21af + 78d9f2e commit b598788
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/better_errors/templates/main.erb
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,13 @@
function REPL(index) {
this.index = index;

this.previousCommands = [];
this.previousCommandOffset = 0;
var previousCommands = JSON.parse(localStorage.getItem("better_errors_previous_commands"));
if(previousCommands === null) {
localStorage.setItem("better_errors_previous_commands", JSON.stringify([]));
previousCommands = [];
}

this.previousCommandOffset = previousCommands.length;
}

REPL.all = [];
Expand Down Expand Up @@ -865,28 +870,34 @@
REPL.prototype.onEnterKey = function() {
var text = this.getInput();
if(text != "" && text !== undefined) {
this.previousCommandOffset = this.previousCommands.push(text);
var previousCommands = JSON.parse(localStorage.getItem("better_errors_previous_commands"));
this.previousCommandOffset = previousCommands.push(text);
if(previousCommands.length > 100) {
previousCommands.splice(0, 1);
}
localStorage.setItem("better_errors_previous_commands", JSON.stringify(previousCommands));
}
this.setInput("");
this.sendInput(text);
};

REPL.prototype.onNavigateHistory = function(direction) {
this.previousCommandOffset += direction;
var previousCommands = JSON.parse(localStorage.getItem("better_errors_previous_commands"));

if(this.previousCommandOffset < 0) {
this.previousCommandOffset = -1;
this.setInput("");
return;
}

if(this.previousCommandOffset >= this.previousCommands.length) {
this.previousCommandOffset = this.previousCommands.length;
if(this.previousCommandOffset >= previousCommands.length) {
this.previousCommandOffset = previousCommands.length;
this.setInput("");
return;
}

this.setInput(this.previousCommands[this.previousCommandOffset]);
this.setInput(previousCommands[this.previousCommandOffset]);
};

REPL.prototype.onKeyDown = function(ev) {
Expand Down

0 comments on commit b598788

Please sign in to comment.