8000 Fix readOrNull throwing on timeout by ajalt · Pull Request #258 · ajalt/mordant · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix readOrNull throwing on timeout #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Changelog

## Unreleased
## Changed
- `Terminal.rawPrint` now takes a `CharSequence` instead of a `String`
### Changed
- `Terminal.rawPrint` now takes a `CharSequence` instead of a `String` [(#255)](https://github.com/ajalt/mordant/issues/255)

### Fixed
- Fixed `Terminal.read*OrNull` throwing an exception on timeout on Windows. [(#256)](https://github.com/ajalt/mordant/issues/256)

## 3.0.1
### Fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.ajalt.mordant.terminal.terminalinterface.ffm

import com.github.ajalt.mordant.rendering.Size
import com.github.ajalt.mordant.terminal.TimeoutException
import com.github.ajalt.mordant.terminal.terminalinterface.TerminalInterfaceWindows
import java.lang.foreign.*

Expand Down Expand Up @@ -279,7 +280,7 @@ internal class TerminalInterfaceFfmWindows : TerminalInterfaceWindows() {
val stdin = stdinHandle
val waitResult = kernel.WaitForSingleObject(stdin, dwMilliseconds)
if (waitResult != 0) {
throw RuntimeException("Timeout reading from console input")
throw TimeoutException()
}
val inputEvents = arena.allocate(WinKernel32Library.INPUT_RECORD.Layout.layout, 1)
val eventsReadSeg = arena.allocateInt()
Expand Down
4 changes: 4 additions & 0 deletions mordant/api/mordant.api
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,10 @@ public final class com/github/ajalt/mordant/terminal/TerminalRecorder : com/gith
public final fun stdout ()Ljava/lang/String;
}

public final class com/github/ajalt/mordant/terminal/TimeoutException : java/lang/RuntimeException {
public fun <init> ()V
}

public final class com/github/ajalt/mordant/terminal/YesNoPrompt : com/github/ajalt/mordant/terminal/Prompt {
public fun <init> (Ljava/lang/String;Lcom/github/ajalt/mordant/terminal/Terminal;Ljava/lang/Boolean;ZZLjava/util/List;Ljava/lang/String;Ljava/lang/String;)V
public synthetic fun <init> (Ljava/lang/String;Lcom/github/ajalt/mordant/terminal/Terminal;Ljava/lang/Boolean;ZZLjava/util/List;Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.ajalt.mordant.input

import com.github.ajalt.mordant.terminal.Terminal
import com.github.ajalt.mordant.terminal.TimeoutException
import kotlin.time.Duration
import kotlin.time.TimeSource

Expand Down Expand Up @@ -102,11 +103,13 @@ class RawModeScope internal constructor(
timeout: Duration, skip: (InputEvent) -> Boolean,
): InputEvent? {
val t = TimeSource.Monotonic.markNow() + timeout
do {
val event = terminal.terminalInterface.readInputEvent(t, mouseTracking) ?: continue
if (event is MouseEvent && mouseTracking == MouseTracking.Off || skip(event)) continue
return event
} while (t.hasNotPassedNow())
try {
do {
val event = terminal.terminalInterface.readInputEvent(t, mouseTracking) ?: continue
if (event is MouseEvent && mouseTracking == MouseTracking.Off || skip(event)) continue
return event
} while (t.hasNotPassedNow())
} catch (_: TimeoutException) {}
return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,5 @@ data class PrintRequest(
*/
val stderr: Boolean,
)

class TimeoutException : RuntimeException("Timeout waiting for input")
0