From d1e4ef77fdcdd55d21be958d38c30a47236384e3 Mon Sep 17 00:00:00 2001 From: Daniel Xie Date: Fri, 2 Jun 2017 20:26:17 -0500 Subject: [PATCH] Tab autocompletion now works on commands. Updated to v0.19.6 --- src/Constants.js | 12 ++++++-- src/Terminal.js | 76 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 65 insertions(+), 23 deletions(-) diff --git a/src/Constants.js b/src/Constants.js index 8bd311b7a..1de9a93c6 100644 --- a/src/Constants.js +++ b/src/Constants.js @@ -1,5 +1,5 @@ CONSTANTS = { - Version: "0.19.5", + Version: "0.19.6", //Max level for any skill, assuming no multipliers. Determined by max numerical value in javascript for experience //and the skill level formula in Player.js. Note that all this means it that when experience hits MAX_INT, then @@ -556,8 +556,14 @@ CONSTANTS = { "-Made the effects of many Augmentations slightly more powerful
" + "-Slightly increased company job wages across the board (~5-10% for each position)
" + "-Gyms and classes are now significantly more expensive
" + - "-Doubled the amount by which a server's security increases when it is hacked. Now, it will
" + - "increase by 0.002. Calling weaken() on a server will lower the security by 0.1.", + "-Doubled the amount by which a server's security increases when it is hacked. Now, it will " + + "increase by 0.002. Calling weaken() on a server will lower the security by 0.1.

" + + "v0.19.6
" + + "-Script editor now saves its state even when you change tabs
" + + "-scp() command in Terminal/script will now overwrite files at the destination
" + + "-Terminal commands are no longer case-sensitive (only the commands themselves such as 'run' or 'nano'. Filenames are " + + "still case sensitive
" + + "-Tab automcompletion will now work on commands", } \ No newline at end of file diff --git a/src/Terminal.js b/src/Terminal.js index 0cc8c97e7..09364bcf9 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -104,12 +104,10 @@ $(document).keydown(function(event) { var commandArray = input.split(" "); var index = commandArray.length - 2; - if (index < 0) {index = 0;} + if (index < -1) {index = 0;} var allPos = determineAllPossibilitiesForTabCompletion(input, index); if (allPos.length == 0) {return;} - - var arg = ""; var command = ""; if (commandArray.length == 0) {return;} @@ -170,41 +168,79 @@ function tabCompletion(command, arg, allPossibilities, index=0) { if (!(allPossibilities.constructor === Array)) {return;} if (!containsAllStrings(allPossibilities)) {return;} - for (var i = allPossibilities.length-1; i >= 0; --i) { - if (!allPossibilities[i].startsWith(arg)) { - allPossibilities.splice(i, 1); + command = command.toLowerCase(); + arg = arg.toLowerCase(); + + if (arg == "") { + for (var i = allPossibilities.length-1; i >= 0; --i) { + if (!allPossibilities[i].startsWith(command)) { + allPossibilities.splice(i, 1); + } + } + } else { + for (var i = allPossibilities.length-1; i >= 0; --i) { + if (!allPossibilities[i].startsWith(arg)) { + allPossibilities.splice(i, 1); + } } } + var val = ""; if (allPossibilities.length == 0) { return; - } else if (allPossibilities.length == 1) { - document.getElementById("terminal-input-text-box").value = command + " " + allPossibilities[0]; + } else if (allPossibilities.length == 1) { + if (arg == "") { + //Autocomplete command + val = allPossibilities[0]; + } else { + val = command + " " + allPossibilities[0]; + } + document.getElementById("terminal-input-text-box").value = val; document.getElementById("terminal-input-text-box").focus(); } else { var longestStartSubstr = longestCommonStart(allPossibilities); //If the longest common starting substring of remaining possibilities is the same //as whatevers already in terminal, just list all possible options. Otherwise, //change the input in the terminal to the longest common starting substr - if (longestStartSubstr == arg) { - //List all possible options - var allOptionsStr = ""; - for (var i = 0; i < allPossibilities.length; ++i) { - allOptionsStr += allPossibilities[i]; - allOptionsStr += " "; - } - post("> " + command + " " + arg); - post(allOptionsStr); - } else { - document.getElementById("terminal-input-text-box").value = command + " " + longestStartSubstr; - document.getElementById("terminal-input-text-box").focus(); + var allOptionsStr = ""; + for (var i = 0; i < allPossibilities.length; ++i) { + allOptionsStr += allPossibilities[i]; + allOptionsStr += " "; } + if (arg == "") { + if (longestStartSubstr == command) { + post("> " + command); + post(allOptionsStr); + } else { + document.getElementById("terminal-input-text-box").value = longestStartSubstr; + document.getElementById("terminal-input-text-box").focus(); + } + } else { + if (longestStartSubstr == arg) { + //List all possible options + post("> " + command + " " + arg); + post(allOptionsStr); + } else { + document.getElementById("terminal-input-text-box").value = command + " " + longestStartSubstr; + document.getElementById("terminal-input-text-box").focus(); + } + } + } } function determineAllPossibilitiesForTabCompletion(input, index=0) { var allPos = []; var currServ = Player.getCurrentServer(); + input = input.toLowerCase(); + + //Autocomplete the command + if (index == -1) { + return ["alias", "analyze", "cat", "clear", "cls", "connect", "free", + "hack", "help", "home", "hostname", "ifconfig", "kill", + "ls", "mem", "nano", "ps", "rm", "run", "scan", "scan-analyze", + "scp", "sudov", "tail", "top"]; + } if (input.startsWith("scp ") && index == 1) { for (var iphostname in AllServers) {