From 1f980e09b7e453bd788f2612dbd4a34f8971299a Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Mon, 16 Jan 2023 07:15:33 +0100 Subject: [PATCH] optimization and echo command --- js/commands.js | 4 ++++ js/main.js | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/js/commands.js b/js/commands.js index 96f70e1..b4325cd 100644 --- a/js/commands.js +++ b/js/commands.js @@ -55,4 +55,8 @@ function cmd_exec(input) { return "Invalid input!"; // Execute input and return output return (new Function(`return ${str}`))().toString(); +} + +function cmd_echo(input) { + return input.join(' '); } \ No newline at end of file diff --git a/js/main.js b/js/main.js index c301ac4..ac1356c 100644 --- a/js/main.js +++ b/js/main.js @@ -162,7 +162,12 @@ function createText(str, safe=true) { // Run input as command if possible function runCommand(input) { + // Return on no input + if (input === "") + return ""; + let output = ""; + const lowerIn = input.toLowerCase(); // Go through properties of window for (const func in window) { @@ -170,12 +175,13 @@ function runCommand(input) { // If property is prefixed with 'cmd_' (a 'command', so an executable function) if (splits.length === 2) { const name = splits[1]; + const lowerNm = name.toLowerCase(); // If command is called without parameters - if (input.toLowerCase() === name.toLowerCase()) { + if (lowerIn === lowerNm) { output = window[func](); break; // If command is called with parameters - } else if (input.startsWith(name + " ")) { + } else if (lowerIn.startsWith(lowerNm + " ")) { // Parameters always follow the command name after first space const params = input.split(" ").filter((e,i)=>i!==0); output = window[func](params); @@ -184,6 +190,10 @@ function runCommand(input) { } } + // Standard output: + if (output === "") + output = `${input.split(" ")[0]}: command not found`; + // Return command output return output; }