Commit a2ae001f authored by jiahua.liu's avatar jiahua.liu

Console Terminal Chinese Supported

parent 168d91e6
...@@ -15,11 +15,13 @@ import com.googlecode.lanterna.terminal.swing.SwingTerminalFrame ...@@ -15,11 +15,13 @@ import com.googlecode.lanterna.terminal.swing.SwingTerminalFrame
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.io.close import kotlinx.coroutines.io.close
import kotlinx.io.core.IoBuffer import kotlinx.io.core.IoBuffer
import kotlinx.io.core.use
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.cleanPage import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.cleanPage
import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.drawLog import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.drawLog
import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.redrawLogs import net.mamoe.mirai.console.MiraiConsoleTerminalUI.LoggerDrawer.redrawLogs
import net.mamoe.mirai.utils.LoginSolver import net.mamoe.mirai.utils.LoginSolver
import net.mamoe.mirai.utils.createCharImg
import net.mamoe.mirai.utils.writeChannel import net.mamoe.mirai.utils.writeChannel
import java.awt.Font import java.awt.Font
import java.io.File import java.io.File
...@@ -29,6 +31,7 @@ import java.nio.charset.Charset ...@@ -29,6 +31,7 @@ import java.nio.charset.Charset
import java.util.* import java.util.*
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedDeque import java.util.concurrent.ConcurrentLinkedDeque
import javax.imageio.ImageIO
import kotlin.concurrent.thread import kotlin.concurrent.thread
import kotlin.system.exitProcess import kotlin.system.exitProcess
...@@ -156,7 +159,17 @@ object MiraiConsoleTerminalUI : MiraiConsoleUI { ...@@ -156,7 +159,17 @@ object MiraiConsoleTerminalUI : MiraiConsoleUI {
error("[Login Solver]验证码无法保存[Error0001]") error("[Login Solver]验证码无法保存[Error0001]")
} }
} }
pushLog(0, "[Login Solver]请输入 4 位字母验证码. 若要更换验证码, 请直接回车")
var toLog = ""
tempFile.inputStream().use {
val img = ImageIO.read(it)
if (img == null) {
toLog += "无法创建字符图片. 请查看文件\n"
} else {
toLog += img.createCharImg((terminal.terminalSize.columns / 1.5).toInt())
}
}
pushLog(0, "$toLog[Login Solver]请输验证码. ${tempFile.absolutePath}")
return requestInput("[Login Solver]请输入 4 位字母验证码. 若要更换验证码, 请直接回车")!! return requestInput("[Login Solver]请输入 4 位字母验证码. 若要更换验证码, 请直接回车")!!
.takeUnless { it.isEmpty() || it.length != 4 } .takeUnless { it.isEmpty() || it.length != 4 }
.also { .also {
...@@ -468,40 +481,47 @@ object MiraiConsoleTerminalUI : MiraiConsoleUI { ...@@ -468,40 +481,47 @@ object MiraiConsoleTerminalUI : MiraiConsoleUI {
val maxHeight = terminal.terminalSize.rows - 4 val maxHeight = terminal.terminalSize.rows - 4
val heightNeed = (string.actualLength() / (terminal.terminalSize.columns - 6)) + 1 val heightNeed = (string.actualLength() / (terminal.terminalSize.columns - 6)) + 1
if (heightNeed - 1 > maxHeight) { if (heightNeed - 1 > maxHeight) {
pushLog(0, "[UI ERROR]: 您的屏幕太小, 有一条超长LOG无法显示")
return//拒绝打印 return//拒绝打印
} }
if (currentHeight + heightNeed > maxHeight) { if (currentHeight + heightNeed > maxHeight) {
cleanPage() cleanPage()//翻页
} }
val width = terminal.terminalSize.columns - 6 if (string.contains("\n")) {
var x = string string.split("\n").forEach {
while (true) { drawLog(string, false)
if (x == "") break }
val toWrite = if (x.actualLength() > width) { } else {
val index = x.getSubStringIndexByActualLength(width) val width = terminal.terminalSize.columns - 6
x.substring(0, index).also { var x = string
x = if (index < x.length) { while (true) {
x.substring(index) if (x == "") break
} else { val toWrite = if (x.actualLength() > width) {
"" val index = x.getSubStringIndexByActualLength(width)
x.substring(0, index).also {
x = if (index < x.length) {
x.substring(index)
} else {
""
}
}
} else {
x.also {
x = ""
} }
} }
} else { try {
x.also { textGraphics.foregroundColor = TextColor.ANSI.GREEN
x = "" textGraphics.backgroundColor = TextColor.ANSI.DEFAULT
textGraphics.putString(
3,
currentHeight, toWrite, SGR.ITALIC
)
} catch (ignored: Exception) {
//
} }
++currentHeight
} }
try {
textGraphics.foregroundColor = TextColor.ANSI.GREEN
textGraphics.backgroundColor = TextColor.ANSI.DEFAULT
textGraphics.putString(
3,
currentHeight, toWrite, SGR.ITALIC
)
} catch (ignored: Exception) {
//
}
++currentHeight
} }
if (flush && terminal is SwingTerminalFrame) { if (flush && terminal is SwingTerminalFrame) {
terminal.flush() terminal.flush()
......
...@@ -134,7 +134,7 @@ private val loginSolverLock = Mutex() ...@@ -134,7 +134,7 @@ private val loginSolverLock = Mutex()
/** /**
* @author NaturalHG * @author NaturalHG
*/ */
private fun BufferedImage.createCharImg(outputWidth: Int = 100, ignoreRate: Double = 0.95): String { public fun BufferedImage.createCharImg(outputWidth: Int = 100, ignoreRate: Double = 0.95): String {
val newHeight = (this.height * (outputWidth.toDouble() / this.width)).toInt() val newHeight = (this.height * (outputWidth.toDouble() / this.width)).toInt()
val tmp = this.getScaledInstance(outputWidth, newHeight, Image.SCALE_SMOOTH) val tmp = this.getScaledInstance(outputWidth, newHeight, Image.SCALE_SMOOTH)
val image = BufferedImage(outputWidth, newHeight, BufferedImage.TYPE_INT_ARGB) val image = BufferedImage(outputWidth, newHeight, BufferedImage.TYPE_INT_ARGB)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment