Skip to content

rawMode.readEventOrNull(...) throws an Exception instead of returning null on Windows #276

@Stephcraft

Description

@Stephcraft

I'm trying to listen for terminal inputs in a coroutine, to do so I think we need to use rawMode.readEventOrNull(...) so that cancelling cooperates, unless there's a better way?

runBlocking {
    val inputCoroutine = launch(Dispatchers.IO) {
        terminal.enterRawMode(MouseTracking.Off).use { rawMode ->
            while (isActive) {
                val event = rawMode.readEventOrNull(50.milliseconds) // <- throws here
                if(event is KeyboardEvent && event.isCtrlC) {
                    println("EXIT")
                    exitProcess(0)
                }
            }
        }
    }

    delay(5000)

    inputCoroutine.cancelAndJoin()

    println("DONE")
}

But then a RuntimeException is thrown on Windows instead of returning null:

Exception in thread "main" java.lang.RuntimeException: Timeout reading from console input
        at com.github.ajalt.mordant.terminal.terminalinterface.jna.TerminalInterfaceJnaWindows.readRawEvent(TerminalInterface.jna.windows.kt:278)
        at com.github.ajalt.mordant.terminal.terminalinterface.TerminalInterfaceWindows.readInputEvent(TerminalInterface.windows.kt:85)
        at com.github.ajalt.mordant.input.RawModeScope.readEventOrNull-LRDsOJo(RawMode.kt:140)
        at net.stephcraft.MainKt$main$1$2$inputCoroutine$1.invokeSuspend(Main.kt:329)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
        at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:113)
        at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:89)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:586)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:820)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions