Added up button history mechanic to terminal..the basics work

This commit is contained in:
Daniel Xie 2017-05-01 12:23:20 -05:00
parent 9fa616b61a
commit 1fa0ef339a
2 changed files with 23 additions and 1 deletions

@ -112,7 +112,7 @@
<div id="terminal-container">
<table id="terminal">
<tr id="terminal-input">
<td id="terminal-input-td">$ <input type="text" class="terminal-input"/></td>
<td id="terminal-input-td">$ <input type="text" id="terminal-input-text-box" class="terminal-input"/></td>
</tr>
</table>

@ -17,6 +17,7 @@ var hackProgressPost = function(input) {
updateTerminalScroll();
}
//Scroll to the bottom of the terminal's 'text area'
function updateTerminalScroll() {
var element = document.getElementById("terminal-container");
element.scrollTop = element.scrollHeight;
@ -48,6 +49,18 @@ $(document).keyup(function(event) {
Engine._actionInProgress = false;
Terminal.finishAction(true);
}
//Up key to cycle through past commands
if (event.keyCode == 38) {
if (Terminal.commandHistory.length == 0) {return;}
if (Terminal.commandHistoryIndex < 0 ||
Terminal.commandHistoryIndex >= Terminal.commandHistory.length) {
Terminal.commandHistoryIndex = Terminal.commandHistory.length-1;
}
var prevCommand = Terminal.commandHistory[Terminal.commandHistoryIndex];
document.getElementById("terminal-input-text-box").value = prevCommand;
--Terminal.commandHistoryIndex;
}
}
});
@ -83,6 +96,9 @@ var Terminal = {
hackFlag: false,
analyzeFlag: false,
commandHistory: [],
commandHistoryIndex: 0,
finishAction: function(cancelled = false) {
if (Terminal.hackFlag) {
Terminal.finishHack(cancelled);
@ -181,6 +197,12 @@ var Terminal = {
},
executeCommand: function(command) {
Terminal.commandHistory.push(command);
if (Terminal.commandHistory.length > 50) {
Terminal.commandHistory.splice(0);
}
Terminal.commandHistoryIndex = Terminal.commandHistory.length - 1;
var commandArray = command.split(" ");
if (commandArray.length == 0) {