outsource ANSI (kutil)

This commit is contained in:
Bixilon 2023-06-15 14:26:07 +02:00
parent 9ef20161eb
commit f455f2a20b
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 36 additions and 7 deletions

View File

@ -0,0 +1,27 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.text.formatting
@Deprecated("Kutil 1.23")
object ANSI {
const val ESCAPE = "\u001b["
fun escape(text: String): String {
return ESCAPE + text + "m"
}
fun formatting(code: Int): String = escape(code.toString())
fun rgb(red: Int, green: Int, blue: Int): String = escape("38;2;$red;$green;${blue}")
}

View File

@ -14,18 +14,19 @@ package de.bixilon.minosoft.data.text.formatting
import de.bixilon.kutil.enums.EnumUtil import de.bixilon.kutil.enums.EnumUtil
import de.bixilon.kutil.enums.ValuesEnum import de.bixilon.kutil.enums.ValuesEnum
import de.bixilon.minosoft.data.text.formatting.ANSI.formatting
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
enum class FormattingCodes( enum class FormattingCodes(
val char: Char, val char: Char,
val ansi: String, val ansi: String,
) { ) {
OBFUSCATED('k', "\u001b[5m"), OBFUSCATED('k', formatting(5)),
BOLD('l', "\u001b[1m"), BOLD('l', formatting(1)),
STRIKETHROUGH('m', "\u001b[9m"), STRIKETHROUGH('m', formatting(9)),
UNDERLINED('n', "\u001b[4m"), UNDERLINED('n', formatting(4)),
ITALIC('o', "\u001b[3m"), ITALIC('o', formatting(3)),
RESET('r', "\u001B[0m"), RESET('r', formatting(0)),
; ;
val json: String = name.lowercase() val json: String = name.lowercase()

View File

@ -17,11 +17,12 @@ import de.bixilon.kotlinglm.vec3.Vec3
import de.bixilon.kotlinglm.vec4.Vec4 import de.bixilon.kotlinglm.vec4.Vec4
import de.bixilon.minosoft.data.text.ChatComponent import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.data.text.TextComponent import de.bixilon.minosoft.data.text.TextComponent
import de.bixilon.minosoft.data.text.formatting.ANSI
import de.bixilon.minosoft.data.text.formatting.TextFormattable import de.bixilon.minosoft.data.text.formatting.TextFormattable
import org.checkerframework.common.value.qual.IntRange import org.checkerframework.common.value.qual.IntRange
class RGBColor(val rgba: Int) : TextFormattable { class RGBColor(val rgba: Int) : TextFormattable {
val ansi: String = "\u001b[38;2;$red;$green;${blue}m" val ansi: String = ANSI.rgb(red, green, blue)
@JvmOverloads @JvmOverloads
constructor(red: Int, green: Int, blue: Int, alpha: Int = 0xFF) : this(alpha or (blue shl 8) or (green shl 16) or (red shl 24)) constructor(red: Int, green: Int, blue: Int, alpha: Int = 0xFF) : this(alpha or (blue shl 8) or (green shl 16) or (red shl 24))