From f1015915ba38025831d5833a9ff2bc71985d7012 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 11 Sep 2020 19:22:55 +0800 Subject: [PATCH 01/11] Working without Terminal --- .../mirai/console/pure/ConsolePureSettings.kt | 45 ++++ .../mamoe/mirai/console/pure/ConsoleThread.kt | 4 +- .../pure/MiraiConsoleImplementationPure.kt | 8 +- .../console/pure/MiraiConsolePureLoader.kt | 74 +++++- .../mirai/console/pure/noconsole/NoConsole.kt | 240 ++++++++++++++++++ 5 files changed, 368 insertions(+), 3 deletions(-) create mode 100644 frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt create mode 100644 frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt new file mode 100644 index 000000000..cf34ee767 --- /dev/null +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2018-2020 Karlatemp. All rights reserved. + * @author Karlatemp + * + * LuckPerms-Mirai/mirai-console.mirai-console-pure.main/ConsolePureSettings.kt + * + * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link. + * + * https://github.com/Karlatemp/LuckPerms-Mirai/blob/master/LICENSE + */ + +/* + * @author Karlatemp + */ + +package net.mamoe.mirai.console.pure + +@Retention(AnnotationRetention.BINARY) +@RequiresOptIn(level = RequiresOptIn.Level.WARNING) +@Target( + AnnotationTarget.CLASS, + AnnotationTarget.TYPEALIAS, + AnnotationTarget.FUNCTION, + AnnotationTarget.PROPERTY, + AnnotationTarget.FIELD, + AnnotationTarget.CONSTRUCTOR +) +@MustBeDocumented +annotation class ConsolePureExperimentalAPI + +@ConsolePureExperimentalAPI +public object ConsolePureSettings { + @JvmField + var setupAnsi: Boolean = System.getProperty("os.name") + .toLowerCase() + .contains("windows")// Just for Windows + + @JvmField + var noConsole: Boolean = false + + @JvmField + var dropAnsi = false + @JvmField + var noConsoleSafeReading=false +} \ No newline at end of file diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsoleThread.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsoleThread.kt index 80751f6cd..ec05564da 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsoleThread.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsoleThread.kt @@ -27,8 +27,10 @@ import org.jline.reader.UserInterruptException val consoleLogger by lazy { DefaultLogger("console") } -@OptIn(ConsoleInternalAPI::class) +@OptIn(ConsoleInternalAPI::class, ConsolePureExperimentalAPI::class) internal fun startupConsoleThread() { + if (ConsolePureSettings.noConsole) return + MiraiConsole.launch(CoroutineName("Input")) { while (true) { try { diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleImplementationPure.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleImplementationPure.kt index 9bdf2d2ce..1ca9243e6 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleImplementationPure.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleImplementationPure.kt @@ -17,7 +17,7 @@ "INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER_WARNING", "EXPOSED_SUPER_CLASS" ) -@file:OptIn(ConsoleInternalAPI::class, ConsoleFrontEndImplementation::class) +@file:OptIn(ConsoleInternalAPI::class, ConsoleFrontEndImplementation::class, ConsolePureExperimentalAPI::class) package net.mamoe.mirai.console.pure @@ -34,6 +34,8 @@ import net.mamoe.mirai.console.plugin.DeferredPluginLoader import net.mamoe.mirai.console.plugin.PluginLoader import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader import net.mamoe.mirai.console.pure.ConsoleInputImpl.requestInput +import net.mamoe.mirai.console.pure.noconsole.AllEmptyLineReader +import net.mamoe.mirai.console.pure.noconsole.NoConsole import net.mamoe.mirai.console.util.ConsoleExperimentalAPI import net.mamoe.mirai.console.util.ConsoleInput import net.mamoe.mirai.console.util.ConsoleInternalAPI @@ -119,6 +121,8 @@ private object ConsoleInputImpl : ConsoleInput { } val lineReader: LineReader by lazy { + if (ConsolePureSettings.noConsole) return@lazy AllEmptyLineReader + LineReaderBuilder.builder() .terminal(terminal) .completer(NullCompleter()) @@ -126,6 +130,8 @@ val lineReader: LineReader by lazy { } val terminal: Terminal = run { + if (ConsolePureSettings.noConsole) return@run NoConsole + val dumb = System.getProperty("java.class.path") .contains("idea_rt.jar") || System.getProperty("mirai.idea") !== null || System.getenv("mirai.idea") !== null 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 e52a0ef7e..121a0af84 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 @@ -15,7 +15,7 @@ "INVISIBLE_GETTER", "INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER", ) -@file:OptIn(ConsoleInternalAPI::class) +@file:OptIn(ConsoleInternalAPI::class, ConsolePureExperimentalAPI::class) package net.mamoe.mirai.console.pure @@ -26,6 +26,7 @@ import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.MiraiConsoleImplementation import net.mamoe.mirai.console.MiraiConsoleImplementation.Companion.start import net.mamoe.mirai.console.data.AutoSavePluginDataHolder +import net.mamoe.mirai.console.pure.noconsole.SystemOutputPrintStream import net.mamoe.mirai.console.util.ConsoleExperimentalAPI import net.mamoe.mirai.console.util.ConsoleInternalAPI import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope @@ -33,6 +34,7 @@ import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.utils.DefaultLogger import net.mamoe.mirai.utils.minutesToMillis import java.io.PrintStream +import kotlin.system.exitProcess /** * mirai-console-pure CLI 入口点 @@ -40,6 +42,7 @@ import java.io.PrintStream object MiraiConsolePureLoader { @JvmStatic fun main(args: Array) { + parse(args, exitProcess = true) startAsDaemon() try { runBlocking { @@ -50,6 +53,75 @@ object MiraiConsolePureLoader { } } + @ConsolePureExperimentalAPI + fun printlnHelpMessage() { + val help = mapOf( + "--help" to "显示此帮助", + "--no-console" to "使用无终端操作环境", + "--dont-setup-terminal-ansi" to + "[NoConsole] [Windows Only]\n" + + "不进行ansi console初始化工作", + "--drop-ansi" to "[NoConsole] 禁用 ansi", + "--safe-reading" to + "[NoConsole] 如果启动此选项, console在获取用户输入的时候会获得一个安全的空字符串\n" + + " 如果不启动, 将会直接 error", + ) + val prefixPlaceholder = String(CharArray( + help.keys.maxOfOrNull { it.length }!! + 3 + ) { ' ' }) + + fun printOption(optionName: String, value: String) { + print(optionName) + print(prefixPlaceholder.substring(optionName.length)) + val lines = value.split('\n').iterator() + if (lines.hasNext()) println(lines.next()) + lines.forEach { line -> + print(prefixPlaceholder) + println(line) + } + } + help.entries.forEach { (optionName, value) -> + printOption(optionName, value) + } + } + + @ConsolePureExperimentalAPI + fun parse(args: Array, exitProcess: Boolean = false) { + val iterator = args.iterator() + while (iterator.hasNext()) { + when (val option = iterator.next()) { + "--help" -> { + printlnHelpMessage() + if (exitProcess) exitProcess(0) + return + } + "--no-console" -> { + ConsolePureSettings.noConsole = true + } + "--dont-setup-terminal-ansi" -> { + ConsolePureSettings.setupAnsi = false + } + "--drop-ansi" -> { + ConsolePureSettings.dropAnsi = true + ConsolePureSettings.setupAnsi = false + } + "--safe-reading" -> { + ConsolePureSettings.noConsoleSafeReading = true + } + else -> { + println("Unknown option `$option`") + printlnHelpMessage() + if (exitProcess) + @Suppress("UNREACHABLE_CODE") + exitProcess(1) + return + } + } + } + if (ConsolePureSettings.noConsole) + SystemOutputPrintStream // Setup Output Channel + } + @Suppress("MemberVisibilityCanBePrivate") @ConsoleExperimentalAPI fun startAsDaemon(instance: MiraiConsoleImplementationPure = MiraiConsoleImplementationPure()) { diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt new file mode 100644 index 000000000..eee8339f0 --- /dev/null +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt @@ -0,0 +1,240 @@ +/* + * Copyright (c) 2018-2020 Karlatemp. All rights reserved. + * @author Karlatemp + * + * LuckPerms-Mirai/mirai-console.mirai-console-pure.main/NoConsole.kt + * + * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link. + * + * https://github.com/Karlatemp/LuckPerms-Mirai/blob/master/LICENSE + */ + +/* + * @author Karlatemp + */ + +package net.mamoe.mirai.console.pure.noconsole + +import net.mamoe.mirai.console.pure.ConsolePureExperimentalAPI +import net.mamoe.mirai.console.pure.ConsolePureSettings +import org.jline.keymap.KeyMap +import org.jline.reader.* +import org.jline.terminal.Attributes +import org.jline.terminal.MouseEvent +import org.jline.terminal.Size +import org.jline.terminal.Terminal +import org.jline.terminal.impl.AbstractTerminal +import org.jline.utils.AttributedString +import org.jline.utils.NonBlockingReader +import java.io.File +import java.io.InputStream +import java.io.OutputStream +import java.io.PrintWriter + +private const val SPACE_INT = ' '.toInt() +private const val SPACE_BYTE = ' '.toByte() + +internal object NoNonBlockingReader : NonBlockingReader() { + override fun read(timeout: Long, isPeek: Boolean): Int { + return SPACE_INT + } + + override fun close() { + } + + override fun readBuffered(b: CharArray?): Int { + return 0 + } +} + +internal object AllSpaceInputStream : InputStream() { + override fun read(): Int { + return SPACE_INT + } + + override fun available(): Int { + return 1 + } + + override fun read(b: ByteArray, off: Int, len: Int): Int { + for (i in off until (off + len)) { + b[i] = SPACE_BYTE + } + return len + } + + override fun close() { + } +} + +internal object AllIgnoredOutputStream : OutputStream() { + override fun close() { + } + + override fun write(b: ByteArray, off: Int, len: Int) { + } + + override fun write(b: ByteArray) { + } + + override fun write(b: Int) { + } + + override fun flush() { + } +} + +@OptIn(ConsolePureExperimentalAPI::class) +internal val SystemOutputPrintStream by lazy { + if (ConsolePureSettings.setupAnsi) { + org.fusesource.jansi.AnsiConsole.systemInstall() + } + System.out +} + +internal object AllEmptyLineReader : LineReader { + private fun ignored(): T = error("Ignored") + override fun defaultKeyMaps(): MutableMap> = ignored() + + override fun printAbove(str: String?) { + SystemOutputPrintStream.println(str) + } + + @OptIn(ConsolePureExperimentalAPI::class) + override fun readLine(): String = + if (ConsolePureSettings.noConsoleSafeReading) "" + else error("Unsupported Reading line when console front-end closed.") + + override fun readLine(mask: Char?): String = readLine() + + override fun readLine(prompt: String?): String = readLine() + + override fun readLine(prompt: String?, mask: Char?): String = readLine() + + override fun readLine(prompt: String?, mask: Char?, buffer: String?): String = readLine() + + override fun readLine(prompt: String?, rightPrompt: String?, mask: Char?, buffer: String?): String = "" + + override fun readLine( + prompt: String?, + rightPrompt: String?, + maskingCallback: MaskingCallback?, + buffer: String? + ): String = "" + + + override fun printAbove(str: AttributedString?) { + str?.let { printAbove(it.toAnsi()) } + } + + override fun isReading(): Boolean = false + + override fun variable(name: String?, value: Any?) = this + + override fun option(option: LineReader.Option?, value: Boolean) = this + + override fun callWidget(name: String?) {} + + override fun getVariables(): MutableMap = ignored() + + override fun getVariable(name: String?): Any = ignored() + + override fun setVariable(name: String?, value: Any?) {} + + override fun isSet(option: LineReader.Option?): Boolean = ignored() + + override fun setOpt(option: LineReader.Option?) {} + + override fun unsetOpt(option: LineReader.Option?) {} + + override fun getTerminal(): Terminal = NoConsole + + override fun getWidgets(): MutableMap = ignored() + + override fun getBuiltinWidgets(): MutableMap = ignored() + + override fun getBuffer(): Buffer = ignored() + + override fun getAppName(): String = "Mirai Console" + + override fun runMacro(macro: String?) {} + + override fun readMouseEvent(): MouseEvent = ignored() + + override fun getHistory(): History = ignored() + + override fun getParser(): Parser = ignored() + + override fun getHighlighter(): Highlighter = ignored() + + override fun getExpander(): Expander = ignored() + + override fun getKeyMaps(): MutableMap> = ignored() + + override fun getKeyMap(): String = ignored() + + override fun setKeyMap(name: String?): Boolean = ignored() + + override fun getKeys(): KeyMap = ignored() + + override fun getParsedLine(): ParsedLine = ignored() + + override fun getSearchTerm(): String = ignored() + + override fun getRegionActive(): LineReader.RegionType = ignored() + + override fun getRegionMark(): Int = ignored() + + override fun addCommandsInBuffer(commands: MutableCollection?) {} + + override fun editAndAddInBuffer(file: File?) {} + + override fun getLastBinding(): String = ignored() + + override fun getTailTip(): String = ignored() + + override fun setTailTip(tailTip: String?) {} + + override fun setAutosuggestion(type: LineReader.SuggestionType?) {} + + override fun getAutosuggestion(): LineReader.SuggestionType = ignored() + +} + +internal object NoConsole : AbstractTerminal( + "No Console", "No Console" +) { + override fun reader(): NonBlockingReader = NoNonBlockingReader + + private val AllIgnoredPrintWriter = object : PrintWriter(AllIgnoredOutputStream) { + override fun close() { + } + + override fun flush() { + } + } + + // We don't need it. Mirai-Console using LineReader to print messages. + override fun writer(): PrintWriter = AllIgnoredPrintWriter + + override fun input(): InputStream = AllSpaceInputStream + + override fun output(): OutputStream = AllIgnoredOutputStream + private val attributes0 = Attributes() + override fun getAttributes(): Attributes { + return Attributes(attributes0) + } + + override fun setAttributes(attr: Attributes?) { + attr?.let { attributes0.copy(it) } + } + + private val size0 = Size(189, 53) + override fun getSize(): Size { + return Size().also { it.copy(size0) } + } + + override fun setSize(size: Size?) { + size?.let { size0.copy(it) } + } +} \ No newline at end of file From 12b942a27bf7fa1d9dc55dbac9f6d6d94163a8bb Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 11 Sep 2020 19:28:12 +0800 Subject: [PATCH 02/11] Fix Copyright --- .../net/mamoe/mirai/console/pure/ConsolePureSettings.kt | 9 +++------ .../net/mamoe/mirai/console/pure/noconsole/NoConsole.kt | 8 +++----- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt index cf34ee767..5e0fb1d2d 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt @@ -1,14 +1,11 @@ /* - * Copyright (c) 2018-2020 Karlatemp. All rights reserved. - * @author Karlatemp - * - * LuckPerms-Mirai/mirai-console.mirai-console-pure.main/ConsolePureSettings.kt + * Copyright 2019-2020 Mamoe Technologies and contributors. * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link. * - * https://github.com/Karlatemp/LuckPerms-Mirai/blob/master/LICENSE + * https://github.com/mamoe/mirai/blob/master/LICENSE */ - /* * @author Karlatemp */ diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt index eee8339f0..9939e3346 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt @@ -1,12 +1,10 @@ /* - * Copyright (c) 2018-2020 Karlatemp. All rights reserved. - * @author Karlatemp - * - * LuckPerms-Mirai/mirai-console.mirai-console-pure.main/NoConsole.kt + * Copyright 2019-2020 Mamoe Technologies and contributors. * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link. * - * https://github.com/Karlatemp/LuckPerms-Mirai/blob/master/LICENSE + * https://github.com/mamoe/mirai/blob/master/LICENSE */ /* From 6f38654f8703558776174ad3d0a6c4e08eb21bd4 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 11 Sep 2020 19:45:26 +0800 Subject: [PATCH 03/11] Drop ansi --- .../net/mamoe/mirai/console/pure/ConsolePureSettings.kt | 3 ++- .../net/mamoe/mirai/console/pure/noconsole/NoConsole.kt | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt index 5e0fb1d2d..6f6462c72 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt @@ -37,6 +37,7 @@ public object ConsolePureSettings { @JvmField var dropAnsi = false + @JvmField - var noConsoleSafeReading=false + var noConsoleSafeReading = false } \ No newline at end of file diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt index 9939e3346..c42aef863 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt @@ -10,6 +10,7 @@ /* * @author Karlatemp */ +@file:OptIn(ConsolePureExperimentalAPI::class) package net.mamoe.mirai.console.pure.noconsole @@ -82,20 +83,23 @@ internal object AllIgnoredOutputStream : OutputStream() { } } -@OptIn(ConsolePureExperimentalAPI::class) internal val SystemOutputPrintStream by lazy { if (ConsolePureSettings.setupAnsi) { org.fusesource.jansi.AnsiConsole.systemInstall() } System.out } +private val ANSI_REGEX = """\u001b\[[0-9a-zA-Z;]*?m""".toRegex() internal object AllEmptyLineReader : LineReader { private fun ignored(): T = error("Ignored") override fun defaultKeyMaps(): MutableMap> = ignored() override fun printAbove(str: String?) { - SystemOutputPrintStream.println(str) + if (str == null) return + if (ConsolePureSettings.dropAnsi) { + SystemOutputPrintStream.println(ANSI_REGEX.replace(str, "")) + } else SystemOutputPrintStream.println(str) } @OptIn(ConsolePureExperimentalAPI::class) From f677516a70ad3ff64f0c2ee2faa3e79a4865cd09 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 11 Sep 2020 20:02:51 +0800 Subject: [PATCH 04/11] Review: Incoming stream should only `\n` coming. --- .../mirai/console/pure/noconsole/NoConsole.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt index c42aef863..8d3350fd0 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt @@ -30,12 +30,12 @@ import java.io.InputStream import java.io.OutputStream import java.io.PrintWriter -private const val SPACE_INT = ' '.toInt() -private const val SPACE_BYTE = ' '.toByte() +private const val LN_INT = '\n'.toInt() +private const val LN_BYTE = '\n'.toByte() internal object NoNonBlockingReader : NonBlockingReader() { override fun read(timeout: Long, isPeek: Boolean): Int { - return SPACE_INT + return LN_INT } override fun close() { @@ -46,9 +46,9 @@ internal object NoNonBlockingReader : NonBlockingReader() { } } -internal object AllSpaceInputStream : InputStream() { +internal object AllNextLineInputStream : InputStream() { override fun read(): Int { - return SPACE_INT + return LN_INT } override fun available(): Int { @@ -57,7 +57,7 @@ internal object AllSpaceInputStream : InputStream() { override fun read(b: ByteArray, off: Int, len: Int): Int { for (i in off until (off + len)) { - b[i] = SPACE_BYTE + b[i] = LN_BYTE } return len } @@ -219,7 +219,7 @@ internal object NoConsole : AbstractTerminal( // We don't need it. Mirai-Console using LineReader to print messages. override fun writer(): PrintWriter = AllIgnoredPrintWriter - override fun input(): InputStream = AllSpaceInputStream + override fun input(): InputStream = AllNextLineInputStream override fun output(): OutputStream = AllIgnoredOutputStream private val attributes0 = Attributes() From 284f6a28e1222cc77ec497b61eedf3c123b5a489 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 11 Sep 2020 20:03:45 +0800 Subject: [PATCH 05/11] Rename NoNonBlockingReader to NoConsoleNonBlockingReader --- .../net/mamoe/mirai/console/pure/noconsole/NoConsole.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt index 8d3350fd0..8792c11c6 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt @@ -33,7 +33,7 @@ import java.io.PrintWriter private const val LN_INT = '\n'.toInt() private const val LN_BYTE = '\n'.toByte() -internal object NoNonBlockingReader : NonBlockingReader() { +internal object NoConsoleNonBlockingReader : NonBlockingReader() { override fun read(timeout: Long, isPeek: Boolean): Int { return LN_INT } @@ -206,7 +206,7 @@ internal object AllEmptyLineReader : LineReader { internal object NoConsole : AbstractTerminal( "No Console", "No Console" ) { - override fun reader(): NonBlockingReader = NoNonBlockingReader + override fun reader(): NonBlockingReader = NoConsoleNonBlockingReader private val AllIgnoredPrintWriter = object : PrintWriter(AllIgnoredOutputStream) { override fun close() { From afed2e1de3b8ac21ecedc5abee32ed7584f2ee64 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 11 Sep 2020 20:21:41 +0800 Subject: [PATCH 06/11] Review: Using list to save help for better editing. --- .../console/pure/MiraiConsolePureLoader.kt | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) 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 121a0af84..ba16ae344 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 @@ -55,22 +55,30 @@ object MiraiConsolePureLoader { @ConsolePureExperimentalAPI fun printlnHelpMessage() { - val help = mapOf( + val help = listOf( + "" to "Mirai-Console[Pure FrontEnd] v" + kotlin.runCatching { + net.mamoe.mirai.console.internal.MiraiConsoleBuildConstants.version + }.getOrElse { "" }, + "" to "", "--help" to "显示此帮助", + "" to "", "--no-console" to "使用无终端操作环境", "--dont-setup-terminal-ansi" to - "[NoConsole] [Windows Only]\n" + - "不进行ansi console初始化工作", + "[NoConsole] [Windows Only] 不进行ansi console初始化工作", "--drop-ansi" to "[NoConsole] 禁用 ansi", "--safe-reading" to "[NoConsole] 如果启动此选项, console在获取用户输入的时候会获得一个安全的空字符串\n" + " 如果不启动, 将会直接 error", ) val prefixPlaceholder = String(CharArray( - help.keys.maxOfOrNull { it.length }!! + 3 + help.maxOfOrNull { it.first.length }!! + 3 ) { ' ' }) fun printOption(optionName: String, value: String) { + if (optionName == "") { + println(value) + return + } print(optionName) print(prefixPlaceholder.substring(optionName.length)) val lines = value.split('\n').iterator() @@ -80,7 +88,7 @@ object MiraiConsolePureLoader { println(line) } } - help.entries.forEach { (optionName, value) -> + help.forEach { (optionName, value) -> printOption(optionName, value) } } From 9e9c848596ae77a2d8d9413aafd8b21fa86d0217 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 11 Sep 2020 20:35:36 +0800 Subject: [PATCH 07/11] Rename `--drop-ansi` to `--no-ansi` --- .../net/mamoe/mirai/console/pure/ConsolePureSettings.kt | 2 +- .../net/mamoe/mirai/console/pure/MiraiConsolePureLoader.kt | 6 +++--- .../net/mamoe/mirai/console/pure/noconsole/NoConsole.kt | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt index 6f6462c72..dab1a70a8 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt @@ -36,7 +36,7 @@ public object ConsolePureSettings { var noConsole: Boolean = false @JvmField - var dropAnsi = false + var noAnsi = false @JvmField var noConsoleSafeReading = false 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 ba16ae344..687417268 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 @@ -65,7 +65,7 @@ object MiraiConsolePureLoader { "--no-console" to "使用无终端操作环境", "--dont-setup-terminal-ansi" to "[NoConsole] [Windows Only] 不进行ansi console初始化工作", - "--drop-ansi" to "[NoConsole] 禁用 ansi", + "--no-ansi" to "[NoConsole] 禁用 ansi", "--safe-reading" to "[NoConsole] 如果启动此选项, console在获取用户输入的时候会获得一个安全的空字符串\n" + " 如果不启动, 将会直接 error", @@ -109,8 +109,8 @@ object MiraiConsolePureLoader { "--dont-setup-terminal-ansi" -> { ConsolePureSettings.setupAnsi = false } - "--drop-ansi" -> { - ConsolePureSettings.dropAnsi = true + "--no-ansi" -> { + ConsolePureSettings.noAnsi = true ConsolePureSettings.setupAnsi = false } "--safe-reading" -> { diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt index 8792c11c6..0694751a0 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt @@ -97,7 +97,7 @@ internal object AllEmptyLineReader : LineReader { override fun printAbove(str: String?) { if (str == null) return - if (ConsolePureSettings.dropAnsi) { + if (ConsolePureSettings.noAnsi) { SystemOutputPrintStream.println(ANSI_REGEX.replace(str, "")) } else SystemOutputPrintStream.println(str) } From a72fe42aaa297bb81b8bc6906daa0a614ee9d65a Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 11 Sep 2020 20:42:00 +0800 Subject: [PATCH 08/11] Add option `--reading-replacement` --- .../mirai/console/pure/ConsolePureSettings.kt | 3 +++ .../mirai/console/pure/MiraiConsolePureLoader.kt | 16 +++++++++++++++- .../mirai/console/pure/noconsole/NoConsole.kt | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt index dab1a70a8..e24447a91 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt @@ -40,4 +40,7 @@ public object ConsolePureSettings { @JvmField var noConsoleSafeReading = false + + @JvmField + var noConsoleReadingReplacement: String = "" } \ No newline at end of file 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 687417268..30b431fc6 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 @@ -69,6 +69,9 @@ object MiraiConsolePureLoader { "--safe-reading" to "[NoConsole] 如果启动此选项, console在获取用户输入的时候会获得一个安全的空字符串\n" + " 如果不启动, 将会直接 error", + "--reading-replacement " to + "[NoConsole] Console尝试读取命令的替换符, 默认是空字符串\n" + + " 使用此选项会自动开启 --safe-reading", ) val prefixPlaceholder = String(CharArray( help.maxOfOrNull { it.first.length }!! + 3 @@ -113,6 +116,18 @@ object MiraiConsolePureLoader { ConsolePureSettings.noAnsi = true ConsolePureSettings.setupAnsi = false } + "--reading-replacement" -> { + ConsolePureSettings.noConsoleSafeReading = true + if (iterator.hasNext()){ + ConsolePureSettings.noConsoleReadingReplacement = iterator.next() + } else { + println("Bad option `--reading-replacement`") + println("Usage: --reading-replacement ") + if (exitProcess) + exitProcess(1) + return + } + } "--safe-reading" -> { ConsolePureSettings.noConsoleSafeReading = true } @@ -120,7 +135,6 @@ object MiraiConsolePureLoader { println("Unknown option `$option`") printlnHelpMessage() if (exitProcess) - @Suppress("UNREACHABLE_CODE") exitProcess(1) return } diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt index 0694751a0..6be4975fa 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt @@ -104,7 +104,7 @@ internal object AllEmptyLineReader : LineReader { @OptIn(ConsolePureExperimentalAPI::class) override fun readLine(): String = - if (ConsolePureSettings.noConsoleSafeReading) "" + if (ConsolePureSettings.noConsoleSafeReading) ConsolePureSettings.noConsoleReadingReplacement else error("Unsupported Reading line when console front-end closed.") override fun readLine(mask: Char?): String = readLine() From a666dc54481a1c8547fee9e608215ac543aac670 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 11 Sep 2020 20:52:50 +0800 Subject: [PATCH 09/11] Unified code style --- .../mirai/console/pure/ConsolePureSettings.kt | 6 +- .../console/pure/MiraiConsolePureLoader.kt | 2 +- .../mirai/console/pure/noconsole/NoConsole.kt | 56 +++---------------- 3 files changed, 11 insertions(+), 53 deletions(-) diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt index e24447a91..68ea3c074 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt @@ -30,16 +30,16 @@ public object ConsolePureSettings { @JvmField var setupAnsi: Boolean = System.getProperty("os.name") .toLowerCase() - .contains("windows")// Just for Windows + .contains("windows") // Just for Windows @JvmField var noConsole: Boolean = false @JvmField - var noAnsi = false + var noAnsi: Boolean = false @JvmField - var noConsoleSafeReading = false + var noConsoleSafeReading: Boolean = false @JvmField var noConsoleReadingReplacement: String = "" 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 30b431fc6..1c5794700 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 @@ -118,7 +118,7 @@ object MiraiConsolePureLoader { } "--reading-replacement" -> { ConsolePureSettings.noConsoleSafeReading = true - if (iterator.hasNext()){ + if (iterator.hasNext()) { ConsolePureSettings.noConsoleReadingReplacement = iterator.next() } else { println("Bad option `--reading-replacement`") diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt index 6be4975fa..d77667538 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt @@ -92,8 +92,6 @@ internal val SystemOutputPrintStream by lazy { private val ANSI_REGEX = """\u001b\[[0-9a-zA-Z;]*?m""".toRegex() internal object AllEmptyLineReader : LineReader { - private fun ignored(): T = error("Ignored") - override fun defaultKeyMaps(): MutableMap> = ignored() override fun printAbove(str: String?) { if (str == null) return @@ -107,16 +105,13 @@ internal object AllEmptyLineReader : LineReader { if (ConsolePureSettings.noConsoleSafeReading) ConsolePureSettings.noConsoleReadingReplacement else error("Unsupported Reading line when console front-end closed.") + // region + private fun ignored(): T = error("Ignored") override fun readLine(mask: Char?): String = readLine() - override fun readLine(prompt: String?): String = readLine() - override fun readLine(prompt: String?, mask: Char?): String = readLine() - override fun readLine(prompt: String?, mask: Char?, buffer: String?): String = readLine() - override fun readLine(prompt: String?, rightPrompt: String?, mask: Char?, buffer: String?): String = "" - override fun readLine( prompt: String?, rightPrompt: String?, @@ -124,83 +119,48 @@ internal object AllEmptyLineReader : LineReader { buffer: String? ): String = "" - override fun printAbove(str: AttributedString?) { str?.let { printAbove(it.toAnsi()) } } + override fun defaultKeyMaps(): MutableMap> = ignored() override fun isReading(): Boolean = false - override fun variable(name: String?, value: Any?) = this - override fun option(option: LineReader.Option?, value: Boolean) = this - override fun callWidget(name: String?) {} - override fun getVariables(): MutableMap = ignored() - override fun getVariable(name: String?): Any = ignored() - override fun setVariable(name: String?, value: Any?) {} - override fun isSet(option: LineReader.Option?): Boolean = ignored() - override fun setOpt(option: LineReader.Option?) {} - override fun unsetOpt(option: LineReader.Option?) {} - override fun getTerminal(): Terminal = NoConsole - override fun getWidgets(): MutableMap = ignored() - override fun getBuiltinWidgets(): MutableMap = ignored() - override fun getBuffer(): Buffer = ignored() - override fun getAppName(): String = "Mirai Console" - override fun runMacro(macro: String?) {} - override fun readMouseEvent(): MouseEvent = ignored() - override fun getHistory(): History = ignored() - override fun getParser(): Parser = ignored() - override fun getHighlighter(): Highlighter = ignored() - override fun getExpander(): Expander = ignored() - override fun getKeyMaps(): MutableMap> = ignored() - override fun getKeyMap(): String = ignored() - override fun setKeyMap(name: String?): Boolean = ignored() - override fun getKeys(): KeyMap = ignored() - override fun getParsedLine(): ParsedLine = ignored() - override fun getSearchTerm(): String = ignored() - override fun getRegionActive(): LineReader.RegionType = ignored() - override fun getRegionMark(): Int = ignored() - override fun addCommandsInBuffer(commands: MutableCollection?) {} - override fun editAndAddInBuffer(file: File?) {} - override fun getLastBinding(): String = ignored() - override fun getTailTip(): String = ignored() - override fun setTailTip(tailTip: String?) {} - override fun setAutosuggestion(type: LineReader.SuggestionType?) {} - override fun getAutosuggestion(): LineReader.SuggestionType = ignored() - + // endregion } internal object NoConsole : AbstractTerminal( @@ -209,11 +169,8 @@ internal object NoConsole : AbstractTerminal( override fun reader(): NonBlockingReader = NoConsoleNonBlockingReader private val AllIgnoredPrintWriter = object : PrintWriter(AllIgnoredOutputStream) { - override fun close() { - } - - override fun flush() { - } + override fun close() {} + override fun flush() {} } // We don't need it. Mirai-Console using LineReader to print messages. @@ -222,6 +179,7 @@ internal object NoConsole : AbstractTerminal( override fun input(): InputStream = AllNextLineInputStream override fun output(): OutputStream = AllIgnoredOutputStream + private val attributes0 = Attributes() override fun getAttributes(): Attributes { return Attributes(attributes0) From 081b679134cdfcdabe0b844faaa6006742bfd8af Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 11 Sep 2020 21:08:12 +0800 Subject: [PATCH 10/11] Rename printlnHelpMessage to printHelpMessage --- .../mamoe/mirai/console/pure/MiraiConsolePureLoader.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 1c5794700..c97a60137 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 @@ -54,7 +54,7 @@ object MiraiConsolePureLoader { } @ConsolePureExperimentalAPI - fun printlnHelpMessage() { + fun printHelpMessage() { val help = listOf( "" to "Mirai-Console[Pure FrontEnd] v" + kotlin.runCatching { net.mamoe.mirai.console.internal.MiraiConsoleBuildConstants.version @@ -67,7 +67,7 @@ object MiraiConsolePureLoader { "[NoConsole] [Windows Only] 不进行ansi console初始化工作", "--no-ansi" to "[NoConsole] 禁用 ansi", "--safe-reading" to - "[NoConsole] 如果启动此选项, console在获取用户输入的时候会获得一个安全的空字符串\n" + + "[NoConsole] 如果启动此选项, console在获取用户输入的时候会获得一个安全的替换符\n" + " 如果不启动, 将会直接 error", "--reading-replacement " to "[NoConsole] Console尝试读取命令的替换符, 默认是空字符串\n" + @@ -102,7 +102,7 @@ object MiraiConsolePureLoader { while (iterator.hasNext()) { when (val option = iterator.next()) { "--help" -> { - printlnHelpMessage() + printHelpMessage() if (exitProcess) exitProcess(0) return } @@ -133,7 +133,7 @@ object MiraiConsolePureLoader { } else -> { println("Unknown option `$option`") - printlnHelpMessage() + printHelpMessage() if (exitProcess) exitProcess(1) return From 11688b1decb07382d8bdec5c6e0eff83c031e41c Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Sat, 12 Sep 2020 19:40:34 +0800 Subject: [PATCH 11/11] Correct class naming following Kotlin formal style --- .../mirai/console/pure/ConsolePureSettings.kt | 4 +- .../mamoe/mirai/console/pure/ConsoleThread.kt | 2 +- .../pure/MiraiConsoleImplementationPure.kt | 2 +- .../console/pure/MiraiConsolePureLoader.kt | 6 +-- .../mirai/console/pure/noconsole/NoConsole.kt | 45 +++++++------------ 5 files changed, 23 insertions(+), 36 deletions(-) diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt index 68ea3c074..5a1fdec47 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsolePureSettings.kt @@ -23,9 +23,9 @@ package net.mamoe.mirai.console.pure AnnotationTarget.CONSTRUCTOR ) @MustBeDocumented -annotation class ConsolePureExperimentalAPI +annotation class ConsolePureExperimentalApi -@ConsolePureExperimentalAPI +@ConsolePureExperimentalApi public object ConsolePureSettings { @JvmField var setupAnsi: Boolean = System.getProperty("os.name") diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsoleThread.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsoleThread.kt index 22684d20c..276c6ed18 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsoleThread.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/ConsoleThread.kt @@ -27,7 +27,7 @@ import org.jline.reader.UserInterruptException val consoleLogger by lazy { DefaultLogger("console") } -@OptIn(ConsoleInternalApi::class, ConsolePureExperimentalAPI::class) +@OptIn(ConsoleInternalApi::class, ConsolePureExperimentalApi::class) internal fun startupConsoleThread() { if (ConsolePureSettings.noConsole) return diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleImplementationPure.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleImplementationPure.kt index ed17d6916..f55466259 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleImplementationPure.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/MiraiConsoleImplementationPure.kt @@ -17,7 +17,7 @@ "INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER_WARNING", "EXPOSED_SUPER_CLASS" ) -@file:OptIn(ConsoleInternalApi::class, ConsoleFrontEndImplementation::class, ConsolePureExperimentalAPI::class) +@file:OptIn(ConsoleInternalApi::class, ConsoleFrontEndImplementation::class, ConsolePureExperimentalApi::class) package net.mamoe.mirai.console.pure 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 946d3afdf..0fa6abe1f 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 @@ -15,7 +15,7 @@ "INVISIBLE_GETTER", "INVISIBLE_ABSTRACT_MEMBER_FROM_SUPER", ) -@file:OptIn(ConsoleInternalApi::class, ConsolePureExperimentalAPI::class) +@file:OptIn(ConsoleInternalApi::class, ConsolePureExperimentalApi::class) package net.mamoe.mirai.console.pure @@ -53,7 +53,7 @@ object MiraiConsolePureLoader { } } - @ConsolePureExperimentalAPI + @ConsolePureExperimentalApi fun printHelpMessage() { val help = listOf( "" to "Mirai-Console[Pure FrontEnd] v" + kotlin.runCatching { @@ -96,7 +96,7 @@ object MiraiConsolePureLoader { } } - @ConsolePureExperimentalAPI + @ConsolePureExperimentalApi fun parse(args: Array, exitProcess: Boolean = false) { val iterator = args.iterator() while (iterator.hasNext()) { diff --git a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt index d77667538..d04a69470 100644 --- a/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt +++ b/frontend/mirai-console-pure/src/main/kotlin/net/mamoe/mirai/console/pure/noconsole/NoConsole.kt @@ -10,11 +10,11 @@ /* * @author Karlatemp */ -@file:OptIn(ConsolePureExperimentalAPI::class) +@file:OptIn(ConsolePureExperimentalApi::class) package net.mamoe.mirai.console.pure.noconsole -import net.mamoe.mirai.console.pure.ConsolePureExperimentalAPI +import net.mamoe.mirai.console.pure.ConsolePureExperimentalApi import net.mamoe.mirai.console.pure.ConsolePureSettings import org.jline.keymap.KeyMap import org.jline.reader.* @@ -38,8 +38,7 @@ internal object NoConsoleNonBlockingReader : NonBlockingReader() { return LN_INT } - override fun close() { - } + override fun close() {} override fun readBuffered(b: CharArray?): Int { return 0 @@ -47,13 +46,9 @@ internal object NoConsoleNonBlockingReader : NonBlockingReader() { } internal object AllNextLineInputStream : InputStream() { - override fun read(): Int { - return LN_INT - } + override fun read(): Int = LN_INT - override fun available(): Int { - return 1 - } + override fun available(): Int = 1 override fun read(b: ByteArray, off: Int, len: Int): Int { for (i in off until (off + len)) { @@ -62,28 +57,19 @@ internal object AllNextLineInputStream : InputStream() { return len } - override fun close() { - } + override fun close() {} } internal object AllIgnoredOutputStream : OutputStream() { - override fun close() { - } - - override fun write(b: ByteArray, off: Int, len: Int) { - } - - override fun write(b: ByteArray) { - } - - override fun write(b: Int) { - } - - override fun flush() { - } + override fun close() {} + override fun write(b: ByteArray, off: Int, len: Int) {} + override fun write(b: ByteArray) {} + override fun write(b: Int) {} + override fun flush() {} } internal val SystemOutputPrintStream by lazy { + @OptIn(ConsolePureExperimentalApi::class) if (ConsolePureSettings.setupAnsi) { org.fusesource.jansi.AnsiConsole.systemInstall() } @@ -95,12 +81,13 @@ internal object AllEmptyLineReader : LineReader { override fun printAbove(str: String?) { if (str == null) return + @OptIn(ConsolePureExperimentalApi::class) if (ConsolePureSettings.noAnsi) { SystemOutputPrintStream.println(ANSI_REGEX.replace(str, "")) } else SystemOutputPrintStream.println(str) } - @OptIn(ConsolePureExperimentalAPI::class) + @OptIn(ConsolePureExperimentalApi::class) override fun readLine(): String = if (ConsolePureSettings.noConsoleSafeReading) ConsolePureSettings.noConsoleReadingReplacement else error("Unsupported Reading line when console front-end closed.") @@ -111,13 +98,13 @@ internal object AllEmptyLineReader : LineReader { override fun readLine(prompt: String?): String = readLine() override fun readLine(prompt: String?, mask: Char?): String = readLine() override fun readLine(prompt: String?, mask: Char?, buffer: String?): String = readLine() - override fun readLine(prompt: String?, rightPrompt: String?, mask: Char?, buffer: String?): String = "" + override fun readLine(prompt: String?, rightPrompt: String?, mask: Char?, buffer: String?): String = readLine() override fun readLine( prompt: String?, rightPrompt: String?, maskingCallback: MaskingCallback?, buffer: String? - ): String = "" + ): String = readLine() override fun printAbove(str: AttributedString?) { str?.let { printAbove(it.toAnsi()) }