Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command History #15

Merged
merged 2 commits into from
Dec 10, 2012
Merged
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
56 changes: 49 additions & 7 deletions lib/better_errors/templates/main.erb
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@
var header = document.querySelector("header");
var headerHeight = header.offsetHeight;

var previousCommands = [];
var previousCommandOffset = 0;

apiCall = function(method, opts, cb) {
var req = new XMLHttpRequest();
req.open("POST", "/__better_errors/" + oid + "/" + method, true);
Expand Down Expand Up @@ -318,6 +321,35 @@
replInput.focus();
}
}

function populateCommandPrompt(input, newOffset, bound) {
var index = previousCommands.length + newOffset;
var previousCommand;

if(index < previousCommands.length && index >= 0) {
// the new index is within the bounds of the
// commands array, so grab the previously
// entered command.
previousCommandOffset = newOffset;
previousCommand = previousCommands[index];
}else{
// the new index isn't within the bounds
// of the array.
previousCommand = "";

if(index == bound) {
// set the offset just outside only
// if we're at the bounds of the array.
// this is so we only get one 'blank'
// command and don't continue moving
// the offset when we shouldn't.
previousCommandOffset = newOffset;
}
}
// now set the input's text to the previous command
// in the list.
input.value = previousCommand;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, this is awesome! After this line, could you please use something like this to set the cursor at the end of the text? (It's not happening for me in Chrome, otherwise.)

}

for(var i = 0; i < frames.length; i++) {
(function(index, el, frameInfo) {
Expand All @@ -335,7 +367,16 @@
if(replInput) {
replInput.onkeydown = function(ev) {
if(ev.keyCode == 13) {
// reset the command up/down arrow offset.
previousCommandOffset = 0;

var text = replInput.value;

if(text != "" && text !== undefined) {
// push this command on to the previous commands list.
previousCommands.push(text);
}

replInput.value = "";
apiCall("eval", { "index": index, source: text }, function(response) {
replPre.innerHTML += ">> " + response.highlighted_input + "\n";
Expand All @@ -346,12 +387,20 @@
}
replPre.scrollTop = replPre.scrollHeight;
});
}else if(ev.keyCode == 38) {
// the user pressed the up arrow.
populateCommandPrompt(replInput, previousCommandOffset - 1, -1);
}else if(ev.keyCode == 40) {
// the user pressed the down arrow.
populateCommandPrompt(replInput, previousCommandOffset + 1, previousCommands.length);
}
};
}
})(i, frames[i], frameInfos[i]);
}

document.querySelector(".frames li:first-child").click();

var applicationFrames = document.getElementById("application_frames");
var allFrames = document.getElementById("all_frames");

Expand All @@ -378,13 +427,6 @@
};

applicationFrames.click();

for(var i = 0; i < frames.length; i++) {
if(frames[i].attributes["data-context"].value == "application") {
frames[i].click();
break;
}
}
})();
</script>
</html>
Expand Down