optimization and echo command
This commit is contained in:
parent
6625782d7b
commit
1f980e09b7
@ -55,4 +55,8 @@ function cmd_exec(input) {
|
|||||||
return "Invalid input!";
|
return "Invalid 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(' ');
|
||||||
}
|
}
|
14
js/main.js
14
js/main.js
@ -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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user