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

@ -56,3 +56,7 @@ function cmd_exec(input) {
// Execute input and return output // Execute input and return output
return (new Function(`return ${str}`))().toString(); 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 // Run input as command if possible
function runCommand(input) { function runCommand(input) {
// Return on no input
if (input === "")
return "";
let output = ""; let output = "";
const lowerIn = input.toLowerCase();
// Go through properties of window // Go through properties of window
for (const func in 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 property is prefixed with 'cmd_' (a 'command', so an executable function)
if (splits.length === 2) { if (splits.length === 2) {
const name = splits[1]; const name = splits[1];
const lowerNm = name.toLowerCase();
// If command is called without parameters // If command is called without parameters
if (input.toLowerCase() === name.toLowerCase()) { if (lowerIn === lowerNm) {
output = window[func](); output = window[func]();
break; break;
// If command is called with parameters // 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 // Parameters always follow the command name after first space
const params = input.split(" ").filter((e,i)=>i!==0); const params = input.split(" ").filter((e,i)=>i!==0);
output = window[func](params); 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 command output
return output; return output;
} }