From 1fa0ef339ab5ab0325c63cd5d3d45e50d4fbeafe Mon Sep 17 00:00:00 2001 From: Daniel Xie Date: Mon, 1 May 2017 12:23:20 -0500 Subject: [PATCH] Added up button history mechanic to terminal..the basics work --- index.html | 2 +- src/Terminal.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/index.html b/index.html index 804bc32eb..e3fe6f6ed 100644 --- a/index.html +++ b/index.html @@ -112,7 +112,7 @@
- +
$ $
diff --git a/src/Terminal.js b/src/Terminal.js index 1bbc36127..4a47f25b9 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -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) {