From 4e046c71f88b52b3f888648c55e3d31db26c4850 Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Sat, 15 Feb 2020 21:45:57 +0800 Subject: [PATCH 1/3] console frame complete --- .../kotlin/net/mamoe/mirai/MiraiConsole.kt | 6 - .../kotlin/net/mamoe/mirai/MiraiConsoleUI.kt | 108 ++++++++++++------ 2 files changed, 75 insertions(+), 39 deletions(-) diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsole.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsole.kt index c4837f415..67ad448e2 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsole.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsole.kt @@ -48,16 +48,10 @@ object MiraiConsole { fun start() { logger("Mirai-console v$version $build | core version v$coreVersion is still in testing stage, majority feature is available") - logger("Mirai-console v$version $build | 核心版本 v${coreVersion}还处于测试阶段, 大部分功能可用") - logger() logger("Mirai-console now running under " + System.getProperty("user.dir")) - logger("Mirai-console 正在 " + System.getProperty("user.dir") + "下运行") - logger() logger("Get news in github: https://github.com/mamoe/mirai") - logger("在Github中获取项目最新进展: https://github.com/mamoe/mirai") logger("Mirai为开源项目,请自觉遵守开源项目协议") logger("Powered by Mamoe Technologies and contributors") - logger() runBlocking { DefaultCommands() diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsoleUI.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsoleUI.kt index 7d08f6b81..8bbf1b4e0 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsoleUI.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsoleUI.kt @@ -11,6 +11,8 @@ import com.googlecode.lanterna.terminal.Terminal import com.googlecode.lanterna.terminal.TerminalResizeListener import com.googlecode.lanterna.terminal.swing.SwingTerminal import com.googlecode.lanterna.terminal.swing.SwingTerminalFrame +import kotlinx.coroutines.* +import net.mamoe.mirai.MiraiConsoleUI.LoggerDrawer.cleanPage import net.mamoe.mirai.MiraiConsoleUI.LoggerDrawer.drawLog import net.mamoe.mirai.MiraiConsoleUI.LoggerDrawer.redrawLogs import net.mamoe.mirai.utils.currentTimeSeconds @@ -24,10 +26,13 @@ import kotlin.math.ceil object MiraiConsoleUI { - val log = mutableMapOf>().also { it[0L] = LimitLinkedQueue(50) } + val log = mutableMapOf>().also { + it[0L] = LimitLinkedQueue(50) + it[2821869985L] = LimitLinkedQueue(50) + } - private val screens = mutableListOf(0L) + private val screens = mutableListOf(0L, 2821869985L) private var currentScreenId = 0 fun addBotScreen(uin: Long) { @@ -68,12 +73,46 @@ object MiraiConsoleUI { } textGraphics = terminal.newTextGraphics() + /* + var lastRedrawTime = 0L + var lastNewWidth = 0 + var lastNewHeight = 0 + terminal.addResizeListener(TerminalResizeListener { terminal1: Terminal, newSize: TerminalSize -> - terminal.clearScreen() - inited = false - update() - redrawCommand() - redrawLogs(log[screens[currentScreenId]]!!) + try { + if (lastNewHeight == newSize.rows + && + lastNewWidth == newSize.columns + ) { + return@TerminalResizeListener + } + lastNewHeight = newSize.rows + lastNewWidth = newSize.columns + terminal.clearScreen() + if(terminal !is SwingTerminalFrame) { + Thread.sleep(300) + } + update() + redrawCommand() + redrawLogs(log[screens[currentScreenId]]!!) + }catch (ignored:Exception){ + + } + }) + + */ + var lastJob: Job? = null + terminal.addResizeListener(TerminalResizeListener { terminal1: Terminal, newSize: TerminalSize -> + lastJob = GlobalScope.launch { + delay(300) + if (lastJob == coroutineContext[Job]) { + terminal.clearScreen() + //inited = false + update() + redrawCommand() + redrawLogs(log[screens[currentScreenId]]!!) + } + } }) if (terminal !is SwingTerminalFrame) { @@ -104,10 +143,14 @@ object MiraiConsoleUI { when (keyStroke.keyType) { KeyType.ArrowLeft -> { currentScreenId = getLeftScreenId() + clearRows(2) + cleanPage() update() } KeyType.ArrowRight -> { currentScreenId = getRightScreenId() + clearRows(2) + cleanPage() update() } KeyType.Enter -> { @@ -156,7 +199,6 @@ object MiraiConsoleUI { } - var inited = false fun clearRows(row: Int) { textGraphics.putString(0, row, " ".repeat(terminal.terminalSize.columns)) } @@ -167,25 +209,24 @@ object MiraiConsoleUI { val width = terminal.terminalSize.columns val height = terminal.terminalSize.rows terminal.setBackgroundColor(TextColor.ANSI.DEFAULT) - if (!inited) { - val mainTitle = "Mirai Console v0.01 Core v0.15" - textGraphics.foregroundColor = TextColor.ANSI.WHITE - textGraphics.backgroundColor = TextColor.ANSI.GREEN - textGraphics.putString((width - mainTitle.length) / 2, 1, mainTitle, SGR.BOLD) - textGraphics.foregroundColor = TextColor.ANSI.DEFAULT - textGraphics.backgroundColor = TextColor.ANSI.DEFAULT - textGraphics.putString(2, 3, "-".repeat(width - 4)) - textGraphics.putString(2, 5, "-".repeat(width - 4)) - textGraphics.putString(2, height - 4, "-".repeat(width - 4)) - textGraphics.putString(2, height - 3, "|>>>") - textGraphics.putString(width - 3, height - 3, "|") - textGraphics.putString(2, height - 2, "-".repeat(width - 4)) - inited = true - } + + val mainTitle = "Mirai Console v0.01 Core v0.15" + textGraphics.foregroundColor = TextColor.ANSI.WHITE + textGraphics.backgroundColor = TextColor.ANSI.GREEN + textGraphics.putString((width - mainTitle.length) / 2, 1, mainTitle, SGR.BOLD) + textGraphics.foregroundColor = TextColor.ANSI.DEFAULT + textGraphics.backgroundColor = TextColor.ANSI.DEFAULT + textGraphics.putString(2, 3, "-".repeat(width - 4)) + textGraphics.putString(2, 5, "-".repeat(width - 4)) + textGraphics.putString(2, height - 4, "-".repeat(width - 4)) + textGraphics.putString(2, height - 3, "|>>>") + textGraphics.putString(width - 3, height - 3, "|") + textGraphics.putString(2, height - 2, "-".repeat(width - 4)) + textGraphics.foregroundColor = TextColor.ANSI.DEFAULT textGraphics.backgroundColor = TextColor.ANSI.DEFAULT val leftName = getScreenName(getLeftScreenId()) - clearRows(2) + // clearRows(2) textGraphics.putString((width - title.length) / 2 - "$leftName << ".length, 2, "$leftName << ") textGraphics.foregroundColor = TextColor.ANSI.WHITE textGraphics.backgroundColor = TextColor.ANSI.YELLOW @@ -230,14 +271,14 @@ object MiraiConsoleUI { var currentHeight = 6 fun drawLog(string: String, flush: Boolean = true) { - val maxHeight = terminal.terminalSize.rows - 6 + val maxHeight = terminal.terminalSize.rows - 4 val heightNeed = (string.length / (terminal.terminalSize.columns - 6)) + 1 if (currentHeight + heightNeed > maxHeight) { cleanPage() } textGraphics.foregroundColor = TextColor.ANSI.GREEN textGraphics.backgroundColor = TextColor.ANSI.DEFAULT - val width = terminal.terminalSize.columns - 6 + val width = terminal.terminalSize.columns - 7 var x = string while (true) { if (x == "") break @@ -264,7 +305,7 @@ object MiraiConsoleUI { fun cleanPage() { - for (index in 6 until terminal.terminalSize.rows - 6) { + for (index in 6 until terminal.terminalSize.rows - 4) { clearRows(index) } currentHeight = 6 @@ -272,20 +313,21 @@ object MiraiConsoleUI { fun redrawLogs(toDraw: List) { - this.cleanPage() + //this.cleanPage() + currentHeight = 6 var logsToDraw = 0 var vara = 0 - toDraw.reversed().forEach { + toDraw.forEach { val heightNeed = (it.length / (terminal.terminalSize.columns - 6)) + 1 vara += heightNeed - if (currentHeight + vara < terminal.terminalSize.rows - 6) { + if (currentHeight + vara < terminal.terminalSize.rows - 4) { logsToDraw++ } else { - return + return@forEach } } - for (index in (toDraw.size - logsToDraw) until toDraw.size - 1) { - drawLog(toDraw[index], false) + for (index in 0 until logsToDraw) { + drawLog(toDraw[logsToDraw - index - 1], false) } if (terminal is SwingTerminalFrame) { terminal.flush() From b6e77fd86b5a9ec7a08ff05e3ccafadaf08a09db Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Sat, 15 Feb 2020 21:48:55 +0800 Subject: [PATCH 2/3] ConsoleUI warning --- .../src/main/kotlin/net/mamoe/mirai/MiraiConsoleUI.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsoleUI.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsoleUI.kt index 8bbf1b4e0..e2cdf1c17 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsoleUI.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsoleUI.kt @@ -23,7 +23,18 @@ import java.util.* import kotlin.concurrent.thread import kotlin.math.ceil +/** + * 此文件不推荐任何人看 + * 可能导致 + * 1:心肌梗死 + * 2:呼吸困难 + * 3:想要重写但是发现改任何一个看似不合理的地方都会崩 + * + * @author NaturalHG + * + */ +@SuppressWarnings("UNCHECKED") object MiraiConsoleUI { val log = mutableMapOf>().also { From 0e4c991648ed32fd30d0b4e3731494feab8287ec Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Sat, 15 Feb 2020 22:26:59 +0800 Subject: [PATCH 3/3] ConsoleUI warning --- .../mirai/api/http/MiraiHttpAPIServer.kt | 5 +++-- .../kotlin/net/mamoe/mirai/MiraiConsole.kt | 22 +++++++++++++------ .../kotlin/net/mamoe/mirai/MiraiConsoleUI.kt | 21 ++++++++++-------- .../net/mamoe/mirai/plugins/PluginBase.kt | 6 ++++- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/MiraiHttpAPIServer.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/MiraiHttpAPIServer.kt index 6ceafc843..1075f81cc 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/MiraiHttpAPIServer.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/MiraiHttpAPIServer.kt @@ -25,8 +25,9 @@ import kotlin.coroutines.CoroutineContext object MiraiHttpAPIServer : CoroutineScope { - private val logger = DefaultLogger("Mirai HTTP API") - override val coroutineContext: CoroutineContext = CoroutineExceptionHandler { _, throwable -> logger.error(throwable) } + var logger = DefaultLogger("Mirai HTTP API") + override val coroutineContext: CoroutineContext = + CoroutineExceptionHandler { _, throwable -> logger.error(throwable) } init { SessionManager.authKey = generateSessionKey()//用于验证的key, 使用和SessionKey相同的方法生成, 但意义不同 diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsole.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsole.kt index 67ad448e2..16466e74a 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsole.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsole.kt @@ -18,6 +18,7 @@ import net.mamoe.mirai.api.http.MiraiHttpAPIServer import net.mamoe.mirai.api.http.generateSessionKey import net.mamoe.mirai.contact.sendMessage import net.mamoe.mirai.utils.MiraiLogger +import net.mamoe.mirai.utils.SimpleLogger import java.io.File import java.io.PrintStream import kotlin.concurrent.thread @@ -38,16 +39,17 @@ object MiraiConsole { val pluginManager: PluginManager get() = PluginManager - var logger = UIPushLogger(0) + var logger = UIPushLogger var path: String = System.getProperty("user.dir") val version = "0.01" - var coreVersion = "0.13" + var coreVersion = "0.15" val build = "Beta" fun start() { - logger("Mirai-console v$version $build | core version v$coreVersion is still in testing stage, majority feature is available") + MiraiConsoleUI.start() + logger("Mirai-console [v$version $build | core version v$coreVersion] is still in testing stage, majority feature is available") logger("Mirai-console now running under " + System.getProperty("user.dir")) logger("Get news in github: https://github.com/mamoe/mirai") logger("Mirai为开源项目,请自觉遵守开源项目协议") @@ -77,11 +79,14 @@ object MiraiConsole { logger("请尽快更改初始生成的HTTP API AUTHKEY") } logger("正在启动HTTPAPI; 端口=" + MiraiProperties.HTTP_API_PORT) + MiraiHttpAPIServer.logger = SimpleLogger("HTTP API") { _, message, e -> + logger("[Mirai HTTP API]", 0, message) + } MiraiHttpAPIServer.start( MiraiProperties.HTTP_API_PORT, MiraiProperties.HTTP_API_AUTH_KEY ) - logger("HTTPAPI启动完成; 端口=" + MiraiProperties.HTTP_API_PORT) + logger("HTTPAPI启动完成; 端口= " + MiraiProperties.HTTP_API_PORT) } } @@ -249,11 +254,14 @@ object MiraiConsole { } } - class UIPushLogger(val identity: Long) { + object UIPushLogger { operator fun invoke(any: Any? = null) { - MiraiConsoleUI.start() + invoke("[Mirai$version $build]", 0L, any) + } + + operator fun invoke(identityStr: String, identity: Long, any: Any? = null) { if (any != null) { - MiraiConsoleUI.pushLog(identity, "[Mirai$version $build]: $any") + MiraiConsoleUI.pushLog(identity, "$identityStr: $any") } } } diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsoleUI.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsoleUI.kt index e2cdf1c17..ead768705 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsoleUI.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/MiraiConsoleUI.kt @@ -36,19 +36,22 @@ import kotlin.math.ceil @SuppressWarnings("UNCHECKED") object MiraiConsoleUI { + val cacheLogSize = 50 val log = mutableMapOf>().also { - it[0L] = LimitLinkedQueue(50) - it[2821869985L] = LimitLinkedQueue(50) + it[0L] = LimitLinkedQueue(cacheLogSize) } + val botAdminCount = mutableMapOf() private val screens = mutableListOf(0L, 2821869985L) private var currentScreenId = 0 + fun addBotScreen(uin: Long) { screens.add(uin) - log[uin] = LimitLinkedQueue() + log[uin] = LimitLinkedQueue(cacheLogSize) + botAdminCount[uin] = 0 } @@ -182,7 +185,7 @@ object MiraiConsoleUI { } } - fun getLeftScreenId(): Int { + private fun getLeftScreenId(): Int { var newId = currentScreenId - 1 if (newId < 0) { newId = screens.size - 1 @@ -190,7 +193,7 @@ object MiraiConsoleUI { return newId } - fun getRightScreenId(): Int { + private fun getRightScreenId(): Int { var newId = 1 + currentScreenId if (newId >= screens.size) { newId = 0 @@ -198,7 +201,7 @@ object MiraiConsoleUI { return newId } - fun getScreenName(id: Int): String { + private fun getScreenName(id: Int): String { return when (screens[id]) { 0L -> { "Console Screen" @@ -369,7 +372,7 @@ object MiraiConsoleUI { } } - fun addCommandChar( + private fun addCommandChar( c: Char ) { if (commandBuilder.isEmpty() && c != '/') { @@ -385,7 +388,7 @@ object MiraiConsoleUI { } } - fun deleteCommandChar() { + private fun deleteCommandChar() { if (!commandBuilder.isEmpty()) { commandBuilder = StringBuilder(commandBuilder.toString().substring(0, commandBuilder.length - 1)) } @@ -398,7 +401,7 @@ object MiraiConsoleUI { } - fun emptyCommand() { + private fun emptyCommand() { commandBuilder = StringBuilder() redrawCommand() if (terminal is SwingTerminal) { diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/plugins/PluginBase.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/plugins/PluginBase.kt index e1c8b0a20..ceb797100 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/plugins/PluginBase.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/plugins/PluginBase.kt @@ -11,8 +11,10 @@ package net.mamoe.mirai.plugins import net.mamoe.mirai.ICommand import kotlinx.coroutines.* +import net.mamoe.mirai.MiraiConsole import net.mamoe.mirai.utils.DefaultLogger import net.mamoe.mirai.utils.MiraiLogger +import net.mamoe.mirai.utils.SimpleLogger import net.mamoe.mirai.utils.io.encodeToString import java.io.File import java.net.URL @@ -156,7 +158,9 @@ object PluginManager { File(it).mkdirs() } - val logger = DefaultLogger("Mirai Plugin Manager") + val logger = SimpleLogger("Plugin Manager") { _, message, e -> + MiraiConsole.logger("[Plugin Manager]", 0, message) + } //已完成加载的 private val nameToPluginBaseMap: MutableMap = mutableMapOf()