mirror of
https://github.com/mamoe/mirai.git
synced 2025-03-09 19:50:27 +08:00
move terminal
This commit is contained in:
parent
1bc39f09ef
commit
aca12a5600
@ -8,6 +8,12 @@ plugins {
|
||||
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 atomicFuVersion: String by rootProject.ext
|
||||
val coroutinesVersion: String by rootProject.ext
|
||||
@ -29,12 +35,9 @@ dependencies {
|
||||
api(project(":mirai-core"))
|
||||
api(project(":mirai-core-qqandroid"))
|
||||
api(project(":mirai-api-http"))
|
||||
api(project(":mirai-console"))
|
||||
runtimeOnly(files("../mirai-core-qqandroid/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("org.bouncycastle:bcprov-jdk15on:1.64")
|
||||
// classpath is not set correctly by IDE
|
||||
|
@ -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()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
@ -16,15 +16,18 @@ import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import net.mamoe.mirai.console.MiraiConsoleUI.LoggerDrawer.cleanPage
|
||||
import net.mamoe.mirai.console.MiraiConsoleUI.LoggerDrawer.drawLog
|
||||
import net.mamoe.mirai.console.MiraiConsoleUI.LoggerDrawer.redrawLogs
|
||||
import net.mamoe.mirai.Bot
|
||||
import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.cleanPage
|
||||
import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.drawLog
|
||||
import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.redrawLogs
|
||||
import java.awt.Font
|
||||
import java.io.OutputStream
|
||||
import java.io.PrintStream
|
||||
import java.nio.charset.Charset
|
||||
import java.util.*
|
||||
import java.util.concurrent.ConcurrentLinkedQueue
|
||||
import kotlin.concurrent.thread
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
/**
|
||||
* 此文件不推荐任何人看
|
||||
@ -37,31 +40,40 @@ import kotlin.concurrent.thread
|
||||
*
|
||||
*/
|
||||
|
||||
@SuppressWarnings("UNCHECKED")
|
||||
object MiraiConsoleUI {
|
||||
object MiraiConsoleTerminalUI : MiraiConsoleUIFrontEnd {
|
||||
val cacheLogSize = 50
|
||||
|
||||
val log = mutableMapOf<Long, LimitLinkedQueue<String>>().also {
|
||||
it[0L] =
|
||||
LimitLinkedQueue(cacheLogSize)
|
||||
it[2821869985L] =
|
||||
LimitLinkedQueue(cacheLogSize)
|
||||
override fun pushLog(identity: Long, message: String) {
|
||||
log[identity]!!.offer(message)
|
||||
if (identity == screens[currentScreenId]) {
|
||||
drawLog(message)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
|
||||
fun addBotScreen(uin: Long) {
|
||||
screens.add(uin)
|
||||
log[uin] =
|
||||
LimitLinkedQueue(cacheLogSize)
|
||||
botAdminCount[uin] = 0
|
||||
}
|
||||
|
||||
|
||||
lateinit var terminal: Terminal
|
||||
lateinit var textGraphics: TextGraphics
|
||||
|
||||
@ -195,6 +207,9 @@ object MiraiConsoleUI {
|
||||
)
|
||||
emptyCommand()
|
||||
}
|
||||
KeyType.Escape -> {
|
||||
exitProcess(0)
|
||||
}
|
||||
else -> {
|
||||
if (keyStroke.character != null) {
|
||||
if (keyStroke.character.toInt() == 8) {
|
||||
@ -360,22 +375,24 @@ object MiraiConsoleUI {
|
||||
}
|
||||
|
||||
|
||||
fun redrawLogs(toDraw: List<String>) {
|
||||
fun redrawLogs(toDraw: Queue<String>) {
|
||||
//this.cleanPage()
|
||||
currentHeight = 6
|
||||
var logsToDraw = 0
|
||||
var vara = 0
|
||||
val toPrint = mutableListOf<String>()
|
||||
toDraw.forEach {
|
||||
val heightNeed = (it.length / (terminal.terminalSize.columns - 6)) + 1
|
||||
vara += heightNeed
|
||||
if (currentHeight + vara < terminal.terminalSize.rows - 4) {
|
||||
logsToDraw++
|
||||
toPrint.add(it)
|
||||
} else {
|
||||
return@forEach
|
||||
}
|
||||
}
|
||||
for (index in 0 until logsToDraw) {
|
||||
drawLog(toDraw[logsToDraw - index - 1], false)
|
||||
toPrint.reversed().forEach {
|
||||
drawLog(it, false)
|
||||
}
|
||||
if (terminal is SwingTerminalFrame) {
|
||||
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()
|
||||
fun redrawCommand() {
|
||||
@ -475,13 +485,14 @@ object MiraiConsoleUI {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class LimitLinkedQueue<T>(
|
||||
val limit: Int = 50
|
||||
) : LinkedList<T>(), List<T> {
|
||||
override fun push(e: T) {
|
||||
) : ConcurrentLinkedQueue<T>() {
|
||||
override fun offer(e: T): Boolean {
|
||||
if (size >= limit) {
|
||||
pollLast()
|
||||
poll()
|
||||
}
|
||||
super.push(e)
|
||||
return super.offer(e)
|
||||
}
|
||||
}
|
||||
}
|
@ -8,12 +8,6 @@ plugins {
|
||||
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 atomicFuVersion: String by rootProject.ext
|
||||
val coroutinesVersion: String by rootProject.ext
|
||||
|
@ -50,7 +50,11 @@ object MiraiConsole {
|
||||
var coreVersion = "0.15"
|
||||
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 now running under " + System.getProperty(
|
||||
@ -283,7 +287,7 @@ object MiraiConsole {
|
||||
|
||||
operator fun invoke(identityStr: String, identity: Long, any: 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()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -14,7 +14,6 @@ interface MiraiConsoleUIFrontEnd {
|
||||
* identity:log所属的screen, Main=0; Bot=Bot.uin
|
||||
*/
|
||||
fun pushLog(
|
||||
identityString: String,
|
||||
identity: Long,
|
||||
message: String
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user