diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt | 15 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt | 9 |
2 files changed, 24 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt index daa619982..a9ac0ca82 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt @@ -156,6 +156,21 @@ object StringUtils { } } + /** + * Creates a comma-separated list using natural formatting (a, b, and c). + * @param list - the list of strings to join into a string, containing 0 or more elements. + * @param delimiterColor - the color code of the delimiter, inserted before each delimiter (commas and "and"). + * @return a string representing the list joined with the Oxford comma and the word "and". + */ + fun createCommaSeparatedList(list: List<String>, delimiterColor: String = ""): String { + if (list.isEmpty()) return "" + if (list.size == 1) return list[0] + if (list.size == 2) return "${list[0]}$delimiterColor and ${list[1]}" + val lastIndex = list.size - 1 + val allButLast = list.subList(0, lastIndex).joinToString("$delimiterColor, ") + return "$allButLast$delimiterColor, and ${list[lastIndex]}" + } + fun optionalPlural(number: Int, singular: String, plural: String) = "${number.addSeparators()} " + canBePlural(number, singular, plural) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt index ecb842f63..6aa418f85 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/TimeUtils.kt @@ -6,6 +6,8 @@ import io.github.moulberry.notenoughupdates.util.SkyBlockTime import java.time.LocalDate import java.time.ZoneId import kotlin.time.Duration +import kotlin.time.Duration.Companion.minutes +import kotlin.time.Duration.Companion.seconds import kotlin.time.DurationUnit import kotlin.time.toDuration @@ -30,6 +32,13 @@ object TimeUtils { inWholeMilliseconds - 999, biggestUnit, showMilliSeconds, longName, maxUnits ) + fun Duration.timerColor(default: String = "§f") = when (this) { + in 0.seconds..60.seconds -> "§c" + in 60.seconds..3.minutes -> "§6" + in 3.minutes..10.minutes -> "§e" + else -> default + } + fun formatDuration( millis: Long, biggestUnit: TimeUnit = TimeUnit.YEAR, |