diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt | 19 | ||||
-rw-r--r-- | src/test/java/at/hannibal2/skyhanni/test/RemoveColorTest.kt | 20 |
2 files changed, 36 insertions, 3 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt index 347ce2b35..3ddeb498c 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt @@ -43,6 +43,7 @@ object StringUtils { } private val formattingChars = "kmolnrKMOLNR".toSet() + private val colorChars = "abcdefABCDEF0123456789".toSet() /** * Removes color and optionally formatting codes from the given string, leaving plain text. @@ -56,6 +57,9 @@ object StringUtils { // Formatting code: The character following a formatting indicator which specifies what color or text style this sequence corresponds to // Formatting sequence: The combination of a formatting indicator and code that changes the color or format of a string + // Flag for whether there is a text style (non-color and non-reset formatting code) currently being applied + var isFormatted = false + // Find the first formatting indicator var nextFormattingSequence = indexOf('§') @@ -75,14 +79,27 @@ object StringUtils { // Write everything from the read index up to the next formatting indicator into our clean string cleanedString.append(this, readIndex, nextFormattingSequence) + // Get the formatting code (note: this may not be a valid formatting code) + val formattingCode = this.getOrNull(nextFormattingSequence + 1) + // If the next formatting sequence's code indicates a non-color format and we should keep those - if (keepFormatting && nextFormattingSequence + 1 < length && this[nextFormattingSequence + 1] in formattingChars) { + if (keepFormatting && formattingCode in formattingChars) { + // Update formatted flag based on whether this is a reset or a style format code + isFormatted = formattingCode?.lowercaseChar() != 'r' + // Set the readIndex to the formatting indicator, so that the next loop will start writing from that paragraph symbol readIndex = nextFormattingSequence // Find the next § symbol after the formatting sequence nextFormattingSequence = indexOf('§', startIndex = readIndex + 1) } else { // If this formatting sequence should be skipped (either a color code, or !keepFormatting or an incomplete formatting sequence without a code) + + // If being formatted and color code encountered, reset the current formatting code + if (isFormatted && formattingCode in colorChars) { + cleanedString.append("§r") + isFormatted = false + } + // Set the readIndex to after this formatting sequence, so that the next loop will skip over it before writing the string readIndex = nextFormattingSequence + 2 // Find the next § symbol after the formatting sequence diff --git a/src/test/java/at/hannibal2/skyhanni/test/RemoveColorTest.kt b/src/test/java/at/hannibal2/skyhanni/test/RemoveColorTest.kt index f12f295fa..f50fc2403 100644 --- a/src/test/java/at/hannibal2/skyhanni/test/RemoveColorTest.kt +++ b/src/test/java/at/hannibal2/skyhanni/test/RemoveColorTest.kt @@ -21,7 +21,16 @@ class RemoveColorTest { @Test fun testKeepNonColor() { - Assertions.assertEquals("§k§l§m§n§o§r", "§k§l§m§f§n§o§r".removeColor(true)) + Assertions.assertEquals("§k§l§m", "§f§k§l§m".removeColor(true)) + } + + @Test + fun testColorToResetIfFormatted() { + // Replace color code with §r if a formatting style is being applied + Assertions.assertEquals("§k§l§m§r§o", "§k§l§m§f§o".removeColor(true)) + + // Remove the color code otherwise + Assertions.assertEquals("§m§r§l", "§m§r§f§l".removeColor(true)) } @Test @@ -40,6 +49,13 @@ class RemoveColorTest { "Ancient Necron's Chestplate ✪✪✪✪", "§dAncient Necron's Chestplate §6✪§6✪§6✪§6✪".removeColor() ) + Assertions.assertEquals( + "PROMOTE ➜ [158] Manager", + "§5§o§a§lPROMOTE §8➜ §7[158§7] §5Manager".removeColor() + ) + Assertions.assertEquals( + "§o§r§lPROMOTE §r➜ [158] Manager", + "§5§o§a§lPROMOTE §8➜ §7[158§7] §5Manager".removeColor(true) + ) } - } |