Verified Commit 6556444e authored by Jakob Moser's avatar Jakob Moser
Browse files

Automatically try Ctrl+C to exit at least pagers

See #78
parent 754f232a
Loading
Loading
Loading
Loading
+18 −13
Original line number Diff line number Diff line
@@ -175,7 +175,9 @@ class ExerciseExecutionContext {
     */
    #prepareWith(command, keepVisible) {
        return new Promise((resolve, _) => {

            // First, we define two helper functions:
            // 1. prepareAndResolve, which runs the preparation command, optionally clears the terminal and resolves the promise
            // 2. tryPrepareAndResolve, which tries to ensure that we can actually run commands, and then calls prepareAndResolve 
            async function prepareAndResolve() {
                // Actually prepare and resolve the promise (we get the resolve() function via the closure)
                if(command !== null) {
@@ -187,23 +189,26 @@ class ExerciseExecutionContext {
                resolve()
            }

            if (getTerminalContents().length > 0) {
                // We have a prompt, so we can immediately run a command
                prepareAndResolve()
                return
            let intervalId
            function tryPrepareAndResolve() {
                if(getTerminalContents().length === 0 && !isVmLoading()) {
                    // We don't have a prompt, and the VM doesn't appear to be loading.
                    // So maybe the user has opened an editor or a pager. Let's try to get out of this.
                    abort()
                }

            // We don't have a prompt, so wait until we do (by repeatedly checking).           
            const intervalId = setInterval(() => {
                if (getTerminalContents().length > 0) {
                    // We have a prompt, so we can prepare and resolve.
                    clearInterval(intervalId)
                    prepareAndResolve()
                } else if(!isVmLoading()) {
                    // If the vm is not loading, and we don't have a prompt, maybe the user has opened an editor or a pager.
                    // Let's try Ctrl+C, maybe that will help us get out of this.
                    abort()
                    return
                }
            }, 500)
            }
            
            // We will repeatedly attempt to prepare and resolve in 0.5s intervals, starting 0.5s from now...
            intervalId = setInterval(tryPrepareAndResolve, 500)
            // ... and once now.
            tryPrepareAndResolve()
        })
    }