move terminal

This commit is contained in:
jiahua.liu 2020-02-16 15:24:55 +08:00
parent 1bc39f09ef
commit aca12a5600
6 changed files with 77 additions and 61 deletions

View File

@ -8,6 +8,12 @@ plugins {
apply(plugin = "com.github.johnrengelman.shadow") apply(plugin = "com.github.johnrengelman.shadow")
tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>() {
manifest {
attributes["Main-Class"] = "net.mamoe.mirai.console.MiraiConsoleTerminalLoader"
}
}
val kotlinVersion: String by rootProject.ext val kotlinVersion: String by rootProject.ext
val atomicFuVersion: String by rootProject.ext val atomicFuVersion: String by rootProject.ext
val coroutinesVersion: String by rootProject.ext val coroutinesVersion: String by rootProject.ext
@ -29,12 +35,9 @@ dependencies {
api(project(":mirai-core")) api(project(":mirai-core"))
api(project(":mirai-core-qqandroid")) api(project(":mirai-core-qqandroid"))
api(project(":mirai-api-http")) api(project(":mirai-api-http"))
api(project(":mirai-console"))
runtimeOnly(files("../mirai-core-qqandroid/build/classes/kotlin/jvm/main")) runtimeOnly(files("../mirai-core-qqandroid/build/classes/kotlin/jvm/main"))
runtimeOnly(files("../mirai-core/build/classes/kotlin/jvm/main")) runtimeOnly(files("../mirai-core/build/classes/kotlin/jvm/main"))
api(kotlin("serialization"))
api(group = "com.alibaba", name = "fastjson", version = "1.2.62")
api(group = "org.yaml", name = "snakeyaml", version = "1.25")
api(group = "com.moandjiezana.toml", name = "toml4j", version = "0.7.2")
api(group = "com.googlecode.lanterna", name = "lanterna", version = "3.0.2") api(group = "com.googlecode.lanterna", name = "lanterna", version = "3.0.2")
api("org.bouncycastle:bcprov-jdk15on:1.64") api("org.bouncycastle:bcprov-jdk15on:1.64")
// classpath is not set correctly by IDE // classpath is not set correctly by IDE

View File

@ -0,0 +1,18 @@
package net.mamoe.mirai.console
import kotlin.concurrent.thread
class MiraiConsoleTerminalLoader {
companion object {
@JvmStatic
fun main(args: Array<String>) {
MiraiConsoleTerminalUI.start()
MiraiConsole.start(
MiraiConsoleTerminalUI
)
Runtime.getRuntime().addShutdownHook(thread(start = false) {
MiraiConsole.stop()
})
}
}
}

View File

@ -16,15 +16,18 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.mamoe.mirai.console.MiraiConsoleUI.LoggerDrawer.cleanPage import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.MiraiConsoleUI.LoggerDrawer.drawLog import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.cleanPage
import net.mamoe.mirai.console.MiraiConsoleUI.LoggerDrawer.redrawLogs import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.drawLog
import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.redrawLogs
import java.awt.Font import java.awt.Font
import java.io.OutputStream import java.io.OutputStream
import java.io.PrintStream import java.io.PrintStream
import java.nio.charset.Charset import java.nio.charset.Charset
import java.util.* import java.util.*
import java.util.concurrent.ConcurrentLinkedQueue
import kotlin.concurrent.thread import kotlin.concurrent.thread
import kotlin.system.exitProcess
/** /**
* 此文件不推荐任何人看 * 此文件不推荐任何人看
@ -37,31 +40,40 @@ import kotlin.concurrent.thread
* *
*/ */
@SuppressWarnings("UNCHECKED") object MiraiConsoleTerminalUI : MiraiConsoleUIFrontEnd {
object MiraiConsoleUI {
val cacheLogSize = 50 val cacheLogSize = 50
val log = mutableMapOf<Long, LimitLinkedQueue<String>>().also { override fun pushLog(identity: Long, message: String) {
it[0L] = log[identity]!!.offer(message)
LimitLinkedQueue(cacheLogSize) if (identity == screens[currentScreenId]) {
it[2821869985L] = drawLog(message)
LimitLinkedQueue(cacheLogSize) }
}
override fun prePushBot(identity: Long) {
log[identity] = LimitLinkedQueue(cacheLogSize)
botAdminCount[identity] = 0
screens.add(identity)
}
override fun pushBot(bot: Bot) {
//nothing to do
}
override fun pushBotAdminStatus(identity: Long, admins: List<Long>) {
botAdminCount[identity] = admins.size
} }
val botAdminCount = mutableMapOf<Long, Long>()
private val screens = mutableListOf(0L, 2821869985L) val log = mutableMapOf<Long, Queue<String>>().also {
it[0L] = LimitLinkedQueue(cacheLogSize)
}
val botAdminCount = mutableMapOf<Long, Int>()
private val screens = mutableListOf(0L)
private var currentScreenId = 0 private var currentScreenId = 0
fun addBotScreen(uin: Long) {
screens.add(uin)
log[uin] =
LimitLinkedQueue(cacheLogSize)
botAdminCount[uin] = 0
}
lateinit var terminal: Terminal lateinit var terminal: Terminal
lateinit var textGraphics: TextGraphics lateinit var textGraphics: TextGraphics
@ -195,6 +207,9 @@ object MiraiConsoleUI {
) )
emptyCommand() emptyCommand()
} }
KeyType.Escape -> {
exitProcess(0)
}
else -> { else -> {
if (keyStroke.character != null) { if (keyStroke.character != null) {
if (keyStroke.character.toInt() == 8) { if (keyStroke.character.toInt() == 8) {
@ -360,22 +375,24 @@ object MiraiConsoleUI {
} }
fun redrawLogs(toDraw: List<String>) { fun redrawLogs(toDraw: Queue<String>) {
//this.cleanPage() //this.cleanPage()
currentHeight = 6 currentHeight = 6
var logsToDraw = 0 var logsToDraw = 0
var vara = 0 var vara = 0
val toPrint = mutableListOf<String>()
toDraw.forEach { toDraw.forEach {
val heightNeed = (it.length / (terminal.terminalSize.columns - 6)) + 1 val heightNeed = (it.length / (terminal.terminalSize.columns - 6)) + 1
vara += heightNeed vara += heightNeed
if (currentHeight + vara < terminal.terminalSize.rows - 4) { if (currentHeight + vara < terminal.terminalSize.rows - 4) {
logsToDraw++ logsToDraw++
toPrint.add(it)
} else { } else {
return@forEach return@forEach
} }
} }
for (index in 0 until logsToDraw) { toPrint.reversed().forEach {
drawLog(toDraw[logsToDraw - index - 1], false) drawLog(it, false)
} }
if (terminal is SwingTerminalFrame) { if (terminal is SwingTerminalFrame) {
terminal.flush() terminal.flush()
@ -383,13 +400,6 @@ object MiraiConsoleUI {
} }
} }
fun pushLog(uin: Long, str: String) {
log[uin]!!.push(str)
if (uin == screens[currentScreenId]) {
drawLog(str)
}
}
var commandBuilder = StringBuilder() var commandBuilder = StringBuilder()
fun redrawCommand() { fun redrawCommand() {
@ -475,13 +485,14 @@ object MiraiConsoleUI {
} }
} }
class LimitLinkedQueue<T>( class LimitLinkedQueue<T>(
val limit: Int = 50 val limit: Int = 50
) : LinkedList<T>(), List<T> { ) : ConcurrentLinkedQueue<T>() {
override fun push(e: T) { override fun offer(e: T): Boolean {
if (size >= limit) { if (size >= limit) {
pollLast() poll()
} }
super.push(e) return super.offer(e)
} }
} }

View File

@ -8,12 +8,6 @@ plugins {
apply(plugin = "com.github.johnrengelman.shadow") apply(plugin = "com.github.johnrengelman.shadow")
tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>() {
manifest {
attributes["Main-Class"] = "net.mamoe.mirai.console.MiraiConsoleLoader"
}
}
val kotlinVersion: String by rootProject.ext val kotlinVersion: String by rootProject.ext
val atomicFuVersion: String by rootProject.ext val atomicFuVersion: String by rootProject.ext
val coroutinesVersion: String by rootProject.ext val coroutinesVersion: String by rootProject.ext

View File

@ -50,7 +50,11 @@ object MiraiConsole {
var coreVersion = "0.15" var coreVersion = "0.15"
val build = "Beta" val build = "Beta"
fun start() { lateinit var frontEnd: MiraiConsoleUIFrontEnd
fun start(
frontEnd: MiraiConsoleUIFrontEnd
) {
this.frontEnd = frontEnd
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 | core version v$coreVersion] is still in testing stage, majority feature is available")
logger( logger(
"Mirai-console now running under " + System.getProperty( "Mirai-console now running under " + System.getProperty(
@ -283,7 +287,7 @@ object MiraiConsole {
operator fun invoke(identityStr: String, identity: Long, any: Any? = null) { operator fun invoke(identityStr: String, identity: Long, any: Any? = null) {
if (any != null) { if (any != null) {
MiraiConsoleUI.pushLog(identity, "$identityStr: $any") frontEnd.pushLog(identity, "$identityStr: $any")
} }
} }
} }
@ -301,18 +305,5 @@ object MiraiConsole {
} }
class MiraiConsoleLoader {
companion object {
@JvmStatic
fun main(args: Array<String>) {
MiraiConsoleUI.start()
MiraiConsole.start()
Runtime.getRuntime().addShutdownHook(thread(start = false) {
MiraiConsole.stop()
})
}
}
}

View File

@ -14,7 +14,6 @@ interface MiraiConsoleUIFrontEnd {
* identitylog所属的screen, Main=0; Bot=Bot.uin * identitylog所属的screen, Main=0; Bot=Bot.uin
*/ */
fun pushLog( fun pushLog(
identityString: String,
identity: Long, identity: Long,
message: String message: String
) )