optimization and echo command

This commit is contained in:
Baipyrus 2023-01-16 07:15:33 +01:00
parent 6625782d7b
commit 1f980e09b7
2 changed files with 16 additions and 2 deletions

View File

@ -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(' ');
}

View File

@ -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;
}