optimization and echo command
This commit is contained in:
parent
6625782d7b
commit
1f980e09b7
@ -56,3 +56,7 @@ function cmd_exec(input) {
|
||||
// Execute input and return output
|
||||
return (new Function(`return ${str}`))().toString();
|
||||
}
|
||||
|
||||
function cmd_echo(input) {
|
||||
return input.join(' ');
|
||||
}
|
14
js/main.js
14
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user