From 64d13f0a1956ca20d127fd35faed77a40ef7cce9 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Mon, 22 Jun 2020 12:49:02 +0800 Subject: [PATCH] Update command execute result --- .../net/mamoe/mirai/console/MiraiConsole.kt | 2 +- .../mirai/console/command/CommandManager.kt | 57 ++++++++++++++++--- frontend/mirai-console-pure/build.gradle.kts | 19 ++----- .../console/pure/MiraiConsolePureLoader.kt | 19 ++++--- 4 files changed, 66 insertions(+), 31 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt index 7cf716a36..aa40060db 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt @@ -39,7 +39,7 @@ internal object MiraiConsoleInitializer { internal object MiraiConsoleBuildConstants { // auto-filled on build (task :mirai-console:fillBuildConstants) @JvmStatic - val buildDate: Date = Date(1592723625351) // 2020-06-21 15:13:45 + val buildDate: Date = Date(1592799753404L) // 2020-06-22 12:22:33 const val version: String = "0.5.1" } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt index 2a5cca566..7cf78b6ae 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandManager.kt @@ -138,8 +138,10 @@ fun Command.unregister(): Boolean = InternalCommandManager.modifyLock.withLock { * * @see JCommandManager.executeCommand Java 方法 */ -suspend fun CommandSender.executeCommand(vararg messages: Any): Boolean { - if (messages.isEmpty()) return false +suspend fun CommandSender.executeCommand(vararg messages: Any): CommandExecuteResult { + if (messages.isEmpty()) return CommandExecuteResult( + status = CommandExecuteStatus.EMPTY_COMMAND + ) return executeCommandInternal( messages, messages[0].let { if (it is SingleMessage) it.toString() else it.toString().substringBefore(' ') }) @@ -154,8 +156,10 @@ internal inline fun List.dropToTypedArray(n: Int): Array = Arr * * @see JCommandManager.executeCommand Java 方法 */ -suspend fun CommandSender.executeCommand(message: MessageChain): Boolean { - if (message.isEmpty()) return false +suspend fun CommandSender.executeCommand(message: MessageChain): CommandExecuteResult { + if (message.isEmpty()) return CommandExecuteResult( + status = CommandExecuteStatus.EMPTY_COMMAND + ) return executeCommandInternal(message, message[0].toString()) } @@ -163,9 +167,44 @@ suspend fun CommandSender.executeCommand(message: MessageChain): Boolean { internal suspend inline fun CommandSender.executeCommandInternal( messages: Any, commandName: String -): Boolean { - val command = InternalCommandManager.matchCommand(commandName) ?: return false +): CommandExecuteResult { + val command = InternalCommandManager.matchCommand(commandName) ?: return CommandExecuteResult( + status = CommandExecuteStatus.COMMAND_NOT_FOUND, + commandName = commandName + ) val rawInput = messages.flattenCommandComponents() - command.onCommand(this, rawInput.dropToTypedArray(1)) - return true -} \ No newline at end of file + kotlin.runCatching { + command.onCommand(this, rawInput.dropToTypedArray(1)) + }.onFailure { + return CommandExecuteResult( + status = CommandExecuteStatus.FAILED, + commandName = commandName, + command = command, + exception = it + ) + } + return CommandExecuteResult( + status = CommandExecuteStatus.SUCCESSFUL, + commandName = commandName, + command = command + ) +} + +/** + * 命令的执行返回 + */ +class CommandExecuteResult( + val status: CommandExecuteStatus, + val exception: Throwable? = null, + val command: Command? = null, + val commandName: String? = null +) { + /** + * 命令的执行状态 + */ + enum class CommandExecuteStatus { + SUCCESSFUL, FAILED, COMMAND_NOT_FOUND, EMPTY_COMMAND + } + +} +typealias CommandExecuteStatus = CommandExecuteResult.CommandExecuteStatus diff --git a/frontend/mirai-console-pure/build.gradle.kts b/frontend/mirai-console-pure/build.gradle.kts index b26245fd1..ea7d4c798 100644 --- a/frontend/mirai-console-pure/build.gradle.kts +++ b/frontend/mirai-console-pure/build.gradle.kts @@ -22,22 +22,13 @@ kotlin { } } -var debugging = true - dependencies { - fun import0(dep: Any) { - if (debugging) { - implementation(dep) - } else { - compileOnly(dep) - } - } - import0("org.jline:jline:3.15.0") - import0("org.fusesource.jansi:jansi:1.18") + implementation("org.jline:jline:3.15.0") + implementation("org.fusesource.jansi:jansi:1.18") - import0(project(":mirai-console")) - import0("net.mamoe:mirai-core:${Versions.core}") - import0(kotlin("stdlib")) // embedded by core + compileAndRuntime(project(":mirai-console")) + compileAndRuntime("net.mamoe:mirai-core:${Versions.core}") + compileAndRuntime(kotlin("stdlib")) // embedded by core testApi("net.mamoe:mirai-core-qqandroid:${Versions.core}") testApi(project(":mirai-console")) diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePureLoader.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePureLoader.kt index 5d0e58f2d..ad142caa5 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePureLoader.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsolePureLoader.kt @@ -20,13 +20,12 @@ package net.mamoe.mirai.console.pure import net.mamoe.mirai.console.MiraiConsoleInitializer +import net.mamoe.mirai.console.command.CommandExecuteStatus import net.mamoe.mirai.console.command.ConsoleCommandSender import net.mamoe.mirai.console.command.executeCommand import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.PlainText import net.mamoe.mirai.utils.DefaultLogger -import net.mamoe.mirai.utils.PlatformLogger -import org.fusesource.jansi.Ansi import kotlin.concurrent.thread object MiraiConsolePureLoader { @@ -49,12 +48,18 @@ internal fun startConsoleThread() { while (true) { val next = MiraiConsoleFrontEndPure.requestInput("") consoleLogger.debug("INPUT> $next") - kotlin.runCatching { - if (!ConsoleCS.executeCommand(PlainText(next))) { // No such command - consoleLogger.warning("Unknown command: " + next.split(' ')[0]) + val result = ConsoleCS.executeCommand(PlainText(next)) + when (result.status) { + CommandExecuteStatus.SUCCESSFUL -> { + } + CommandExecuteStatus.EMPTY_COMMAND -> { + } + CommandExecuteStatus.FAILED -> { + consoleLogger.error("An error occurred while executing the command: $next", result.exception) + } + CommandExecuteStatus.COMMAND_NOT_FOUND -> { + consoleLogger.warning("Unknown command: ${result.commandName}") } - }.onFailure { - consoleLogger.error("Exception in executing command: $next", it) } } }