diff options
author | appable <enzospiacitelli@gmail.com> | 2024-06-01 03:31:25 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-01 12:31:25 +0200 |
commit | 2698b9d13de081be2e72e63faa213a74cafb6a6b (patch) | |
tree | 33764051474a286d91da3c6c5f93dd804eb66ca1 /src/main | |
parent | 020bd7ec2c4250a5787f805b7961cc7af99d70a1 (diff) | |
download | skyhanni-2698b9d13de081be2e72e63faa213a74cafb6a6b.tar.gz skyhanni-2698b9d13de081be2e72e63faa213a74cafb6a6b.tar.bz2 skyhanni-2698b9d13de081be2e72e63faa213a74cafb6a6b.zip |
Backend: Keep visual formatting appearance with CharSequence.removeColor(keepFormatting=true) (#1824)
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt | 19 |
1 files changed, 18 insertions, 1 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 |