From fb639312f40d3a6b568cbbaed711a794a93470cf Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Fri, 7 Mar 2025 00:02:42 +0100 Subject: [PATCH] Refactor commands and enhance UI with mordant widgets Updated various commands to use mordant widgets for improved output formatting, including tables and panels. Replaced `error` calls with `UsageError` for better exception handling consistency. Added a basic test for replay argument parsing and included project-specific settings for GitToolBox. --- .idea/git_toolbox_prj.xml | 15 ++++++ src/main/kotlin/de/chaoscaot/replay/Main.kt | 2 +- .../replay/commands/ExtractSchematics.kt | 5 +- .../de/chaoscaot/replay/commands/Info.kt | 46 ++++++++++++------- .../chaoscaot/replay/commands/ListPackets.kt | 11 +++-- src/test/kotlin/PathArgTest.kt | 10 ++++ 6 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 .idea/git_toolbox_prj.xml create mode 100644 src/test/kotlin/PathArgTest.kt diff --git a/.idea/git_toolbox_prj.xml b/.idea/git_toolbox_prj.xml new file mode 100644 index 0000000..02b915b --- /dev/null +++ b/.idea/git_toolbox_prj.xml @@ -0,0 +1,15 @@ + + + + + + + \ No newline at end of file diff --git a/src/main/kotlin/de/chaoscaot/replay/Main.kt b/src/main/kotlin/de/chaoscaot/replay/Main.kt index f183cc4..2d1ed0b 100644 --- a/src/main/kotlin/de/chaoscaot/replay/Main.kt +++ b/src/main/kotlin/de/chaoscaot/replay/Main.kt @@ -14,7 +14,7 @@ object ReplayUtils : CliktCommand() { override val printHelpOnEmptyArgs = true private val config by findOrSetObject { RunConfig() } - private val replay by argument(help = "Replay ID or Path").optional() + val replay by argument(help = "Replay ID or Path").optional() private val path by option("-p", "--path", help = "Replay is a Path").flag() override fun run() { diff --git a/src/main/kotlin/de/chaoscaot/replay/commands/ExtractSchematics.kt b/src/main/kotlin/de/chaoscaot/replay/commands/ExtractSchematics.kt index 13039e8..61889c9 100644 --- a/src/main/kotlin/de/chaoscaot/replay/commands/ExtractSchematics.kt +++ b/src/main/kotlin/de/chaoscaot/replay/commands/ExtractSchematics.kt @@ -1,7 +1,6 @@ package de.chaoscaot.replay.commands -import com.github.ajalt.clikt.core.CliktCommand -import com.github.ajalt.clikt.core.requireObject +import com.github.ajalt.clikt.core.* import de.chaoscaot.replay.RunConfig import de.chaoscaot.replay.parser.BlueEmbeddedSchemPacket import de.chaoscaot.replay.parser.RedEmbeddedSchemPacket @@ -17,7 +16,7 @@ object ExtractSchematics: CliktCommand() { override fun run() { if (config.replay == null) { - error("No Replay specified") + throw UsageError("No Replay specified") } val nbt by lazy { Nbt() } diff --git a/src/main/kotlin/de/chaoscaot/replay/commands/Info.kt b/src/main/kotlin/de/chaoscaot/replay/commands/Info.kt index 1a8d3cd..0941b81 100644 --- a/src/main/kotlin/de/chaoscaot/replay/commands/Info.kt +++ b/src/main/kotlin/de/chaoscaot/replay/commands/Info.kt @@ -1,11 +1,17 @@ package de.chaoscaot.replay.commands import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.clikt.core.UsageError import com.github.ajalt.clikt.core.requireObject import com.github.ajalt.clikt.parameters.options.flag import com.github.ajalt.clikt.parameters.options.multiple import com.github.ajalt.clikt.parameters.options.option +import com.github.ajalt.mordant.rendering.BorderType +import com.github.ajalt.mordant.table.Borders import com.github.ajalt.mordant.table.table +import com.github.ajalt.mordant.widgets.Panel +import com.github.ajalt.mordant.widgets.Text +import com.github.ajalt.mordant.widgets.UnorderedList import de.chaoscaot.replay.RunConfig import de.chaoscaot.replay.parser.ReplayParser import de.chaoscaot.replay.parser.UserJoinPacket @@ -18,36 +24,42 @@ object Info : CliktCommand() { override fun run() { if (config.replay == null) { - error("No Replay specified") + throw UsageError("No Replay specified") } val packets = ReplayParser.read(config.replay!!).toList() if (players) { - echo("Players:") - packets.filterIsInstance().map { it.userId }.forEach { echo("\t$it") } - echo() + echo(Panel( + UnorderedList(packets.filterIsInstance().map { Text(it.userId.toString()) }), + Text("Players") + )) } if (toCount.isNotEmpty()) { - echo(table { - header { - row("PacketType", "Count") - } + echo(Panel( + table { + borderType = BorderType.SQUARE_DOUBLE_SECTION_SEPARATOR + tableBorders = Borders.NONE + header { + row("PacketType", "Count") + } - body { - toCount.forEach { name -> - val klass = packets.firstOrNull { it.javaClass.simpleName.lowercase() == "${name}packet".lowercase() }?.javaClass + body { + toCount.forEach { name -> + val klass = packets.firstOrNull { it.javaClass.simpleName.lowercase() == "${name}packet".lowercase() }?.javaClass - if (klass == null) { - row(name, 0) - } else { - row(klass.simpleName, packets.count { it.javaClass == klass }) + if (klass == null) { + row(name, 0) + } else { + row(klass.simpleName, packets.count { it.javaClass == klass }) + } } } - } - }) + }, + Text("Packets") + )) } } } \ No newline at end of file diff --git a/src/main/kotlin/de/chaoscaot/replay/commands/ListPackets.kt b/src/main/kotlin/de/chaoscaot/replay/commands/ListPackets.kt index 6a9d4f2..9174981 100644 --- a/src/main/kotlin/de/chaoscaot/replay/commands/ListPackets.kt +++ b/src/main/kotlin/de/chaoscaot/replay/commands/ListPackets.kt @@ -1,11 +1,16 @@ package de.chaoscaot.replay.commands import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.mordant.table.table import de.chaoscaot.replay.parser.PacketType -object ListPackets : CliktCommand() { +object ListPackets : CliktCommand(name = "packets") { override fun run() { - echo("Packets:") - PacketType.entries.forEach { echo("\t${it.name.lowercase().replace("_packet", "").replace("_", "").padEnd(25, ' ')}: 0x${it.id.toString(16).padStart(2, '0')}") } + echo(table { + header { row("Packet", "ID") } + body { + PacketType.entries.forEach { row(it.name.lowercase().replace("_packet", "").replace("_", ""), "0x${it.id.toString(16).padStart(2, '0')}") } + } + }) } } \ No newline at end of file diff --git a/src/test/kotlin/PathArgTest.kt b/src/test/kotlin/PathArgTest.kt new file mode 100644 index 0000000..3778638 --- /dev/null +++ b/src/test/kotlin/PathArgTest.kt @@ -0,0 +1,10 @@ +import com.github.ajalt.clikt.testing.test +import de.chaoscaot.replay.ReplayUtils +import kotlin.test.Test + +object Tests { + @Test + fun `Test Parsing Replay Id`() { + val result = ReplayUtils.test("123") + } +} \ No newline at end of file