diff options
author | nea <romangraef@gmail.com> | 2022-08-26 14:56:23 +0200 |
---|---|---|
committer | nea <romangraef@gmail.com> | 2022-08-26 14:56:23 +0200 |
commit | 176e37844c17eaabb5c9da0f9c23237df9c0b5a8 (patch) | |
tree | 2c7d485355eed738ce1c30b03b7424e9f4a9db48 /src/jsMain/kotlin/moe/nea89/website/ShellExecutionContext.kt | |
parent | b87a65229ad9c7d09fd837b95e9e03624842669b (diff) | |
download | neamoe-176e37844c17eaabb5c9da0f9c23237df9c0b5a8.tar.gz neamoe-176e37844c17eaabb5c9da0f9c23237df9c0b5a8.tar.bz2 neamoe-176e37844c17eaabb5c9da0f9c23237df9c0b5a8.zip |
idk make it maveny
Diffstat (limited to 'src/jsMain/kotlin/moe/nea89/website/ShellExecutionContext.kt')
-rw-r--r-- | src/jsMain/kotlin/moe/nea89/website/ShellExecutionContext.kt | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/jsMain/kotlin/moe/nea89/website/ShellExecutionContext.kt b/src/jsMain/kotlin/moe/nea89/website/ShellExecutionContext.kt new file mode 100644 index 0000000..bd72421 --- /dev/null +++ b/src/jsMain/kotlin/moe/nea89/website/ShellExecutionContext.kt @@ -0,0 +1,51 @@ +package moe.nea89.website + +import kotlinx.browser.window +import kotlinx.coroutines.suspendCancellableCoroutine +import kotlin.coroutines.* +import kotlin.time.Duration +import kotlin.time.DurationUnit + +class ShellExecutionContext( + val console: KConsole, + val name: String, + val args: List<String>, +) { + + suspend fun wait(duration: Duration) { + suspendCancellableCoroutine<Unit> { + window.setTimeout({ + it.resume(Unit) + }, timeout = duration.toInt(DurationUnit.MILLISECONDS)) + } + } + + suspend fun exit(): Nothing { + suspendCancellableCoroutine<Unit> { + it.cancel() + console.state = KConsole.ConsoleState.SHELLPROMPT + console.rerender() + } + throw RuntimeException("THIs shOULDNT EXIST") + } + + companion object { + fun run( + console: KConsole, command: Command, name: String, args: List<String> + ) { + console.state = KConsole.ConsoleState.IN_PROGRAM + val se = ShellExecutionContext(console, name, args) + window.requestAnimationFrame { + command.runner.createCoroutine(se, object : Continuation<Unit> { + override val context: CoroutineContext + get() = EmptyCoroutineContext + + override fun resumeWith(result: Result<Unit>) { + console.state = KConsole.ConsoleState.SHELLPROMPT + console.rerender() + } + }).resume(Unit) + } + } + } +} |