async functions test

This commit is contained in:
Baipyrus 2023-01-16 15:28:11 +01:00
parent 95fbc3cea4
commit cd4036172d

View File

@ -44,7 +44,7 @@ document.addEventListener("selectionchange", e => {
} }
}); });
document.addEventListener("keydown", e => { document.addEventListener("keydown", async e => {
const text = textCur.textContent; const text = textCur.textContent;
switch (e.key) { switch (e.key) {
case "Backspace": case "Backspace":
@ -100,7 +100,7 @@ document.addEventListener("keydown", e => {
tbDiv.innerHTML += `<br>`; tbDiv.innerHTML += `<br>`;
// Run command and return output // Run command and return output
let commandOutput = runCommand(text); let commandOutput = await runCommand(text);
if (commandOutput !== "") { if (commandOutput !== "") {
// If output is given, display it as text with formatting // If output is given, display it as text with formatting
tbDiv.append(createText(commandOutput, false)) tbDiv.append(createText(commandOutput, false))
@ -162,7 +162,7 @@ function createText(str, safe=true) {
} }
// Run input as command if possible // Run input as command if possible
function runCommand(input) { async function runCommand(input) {
// Return on no input // Return on no input
if (input === "") if (input === "")
return ""; return "";
@ -179,12 +179,18 @@ function runCommand(input) {
const lowerNm = name.toLowerCase(); const lowerNm = name.toLowerCase();
// If command is called without parameters // If command is called without parameters
if (lowerIn === lowerNm) { if (lowerIn === lowerNm) {
if (window[func].constructor.name === "AsnycFunction")
output = await window[func]();
else
output = window[func](); output = window[func]();
break; break;
// If command is called with parameters // If command is called with parameters
} else if (lowerIn.startsWith(lowerNm + " ")) { } 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);
if (window[func].constructor.name === "AsnycFunction")
output = await window[func](params);
else
output = window[func](params); output = window[func](params);
break; break;
} }