Commit 969ce409 authored by Jakob Moser's avatar Jakob Moser
Browse files

Make runCommand() await prompt

parent d1653c16
Loading
Loading
Loading
Loading
+24 −2
Original line number Diff line number Diff line
@@ -108,9 +108,10 @@ function sanitize(line) {
 *   input line), the returned results won't contain all the output or the input line
 * - Empty lines are ignored: Even if a command legitimately outputs an empty line, it will be ignored.
 * 
 * @param includeLastInteraction If the last interaction (which only consists of a prompt, with nothing executed yet) should be included
 * @returns List of objects of the form {"prompt": "...", "input": "...", "output": ["...", "..."]}
 */
export function getTerminalContents() {
export function getTerminalContents(includeLastInteraction) {
    const contents = []
    let currentInteraction = {}

@@ -128,9 +129,29 @@ export function getTerminalContents() {
            }
        })

    if(includeLastInteraction) {
        contents.push(currentInteraction)
    }

    return contents
}

/**
 * Returns the current prompt as soon as one is shown
 */
function prompt() {
    return new Promise((resolve)=>{
        const intervalId = setInterval(()=>{
            const lastInteraction = getTerminalContents(true).at(-1)

            if(lastInteraction.prompt && !lastInteraction.input) {
                clearInterval(intervalId)
                resolve(prompt)
            }
        }, 200)
    })
}

/**
 * Calls the given handler function without parameters whenever the user presses the key enter
 * @param {Function} handler A handler function
@@ -157,7 +178,8 @@ export async function runCommand(command) {
    // https://thewebdev.info/2021/05/02/how-to-programmatically-trigger-a-change-event-on-an-input-with-javascript/
    textarea.dispatchEvent(new Event("input"))

    // TODO Wait until a prompt is shown
    // And waid until a prompt is shown (-> until the command is executed)
    await prompt()
}

/**