mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-26 07:20:09 +08:00
Add blocking bridge for ConsoleInput.requestInput
This commit is contained in:
parent
a4d5a4cbf2
commit
bc447fc103
@ -11,29 +11,23 @@
|
|||||||
|
|
||||||
package net.mamoe.mirai.console.util
|
package net.mamoe.mirai.console.util
|
||||||
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
|
import net.mamoe.kjbb.JvmBlockingBridge
|
||||||
import net.mamoe.mirai.console.MiraiConsole
|
import net.mamoe.mirai.console.MiraiConsole
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Console 输入. 由于 console 接管了 stdin, [readLine] 等操作需要在这里进行.
|
* Console 输入. 由于 console 接管了 [stdin][System. in], [readLine] 等操作需要在这里进行.
|
||||||
*/
|
*/
|
||||||
public interface ConsoleInput {
|
public interface ConsoleInput {
|
||||||
/**
|
/**
|
||||||
* 以 [提示][hint] 向用户索要一个输入
|
* 挂起当前协程, 以 [提示][hint] 向用户索要一个输入, 在用户完成输入时返回输入结果.
|
||||||
*/
|
*/
|
||||||
@JvmSynthetic
|
@JvmBlockingBridge
|
||||||
public suspend fun requestInput(hint: String): String
|
public suspend fun requestInput(hint: String): String
|
||||||
|
|
||||||
/**
|
|
||||||
* 以 [提示][hint] 向用户索要一个输入. 仅供 Java 调用者使用
|
|
||||||
*/
|
|
||||||
@JvmName("requestInput")
|
|
||||||
@JavaFriendlyAPI
|
|
||||||
public fun requestInputBlocking(hint: String): String
|
|
||||||
|
|
||||||
public companion object INSTANCE : ConsoleInput by ConsoleInputImpl {
|
public companion object INSTANCE : ConsoleInput by ConsoleInputImpl {
|
||||||
|
@JvmSynthetic
|
||||||
public suspend inline fun MiraiConsole.requestInput(hint: String): String = ConsoleInput.requestInput(hint)
|
public suspend inline fun MiraiConsole.requestInput(hint: String): String = ConsoleInput.requestInput(hint)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,10 +36,6 @@ public interface ConsoleInput {
|
|||||||
internal object ConsoleInputImpl : ConsoleInput {
|
internal object ConsoleInputImpl : ConsoleInput {
|
||||||
private val inputLock = Mutex()
|
private val inputLock = Mutex()
|
||||||
|
|
||||||
override suspend fun requestInput(
|
override suspend fun requestInput(hint: String): String =
|
||||||
hint: String
|
inputLock.withLock { MiraiConsole.frontEnd.requestInput(hint) }
|
||||||
): String = inputLock.withLock { MiraiConsole.frontEnd.requestInput(hint) }
|
|
||||||
|
|
||||||
@JavaFriendlyAPI
|
|
||||||
override fun requestInputBlocking(hint: String): String = runBlocking { requestInput(hint) }
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user