Skip to content

Commit d1241fb

Browse files
committed
feat: More fine grained time display
1 parent 4d0a730 commit d1241fb

File tree

2 files changed

+54
-48
lines changed

2 files changed

+54
-48
lines changed
+53-47
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,65 @@
1-
2-
31
package moe.nea.firmament.util
42

53
import com.google.common.math.IntMath.pow
64
import kotlin.math.absoluteValue
75
import kotlin.time.Duration
6+
import kotlin.time.Duration.Companion.seconds
87

98
object FirmFormatters {
10-
fun formatCommas(int: Int, segments: Int = 3): String = formatCommas(int.toLong(), segments)
11-
fun formatCommas(long: Long, segments: Int = 3): String {
12-
val α = long / 1000
13-
if!= 0L) {
14-
return formatCommas(α, segments) + "," + (long - α * 1000).toString().padStart(3, '0')
15-
}
16-
return long.toString()
17-
}
9+
fun formatCommas(int: Int, segments: Int = 3): String = formatCommas(int.toLong(), segments)
10+
fun formatCommas(long: Long, segments: Int = 3): String {
11+
val α = long / 1000
12+
if!= 0L) {
13+
return formatCommas(α, segments) + "," + (long - α * 1000).toString().padStart(3, '0')
14+
}
15+
return long.toString()
16+
}
1817

19-
fun formatCommas(float: Float, fractionalDigits: Int): String = formatCommas(float.toDouble(), fractionalDigits)
20-
fun formatCommas(double: Double, fractionalDigits: Int): String {
21-
val long = double.toLong()
22-
val δ = (double - long).absoluteValue
23-
val μ = pow(10, fractionalDigits)
24-
val digits =* δ).toInt().toString().padStart(fractionalDigits, '0').trimEnd('0')
25-
return formatCommas(long) + (if (digits.isEmpty()) "" else ".$digits")
26-
}
18+
fun formatCommas(float: Float, fractionalDigits: Int): String = formatCommas(float.toDouble(), fractionalDigits)
19+
fun formatCommas(double: Double, fractionalDigits: Int): String {
20+
val long = double.toLong()
21+
val δ = (double - long).absoluteValue
22+
val μ = pow(10, fractionalDigits)
23+
val digits =* δ).toInt().toString().padStart(fractionalDigits, '0').trimEnd('0')
24+
return formatCommas(long) + (if (digits.isEmpty()) "" else ".$digits")
25+
}
2726

28-
fun formatDistance(distance: Double): String {
29-
if (distance < 10)
30-
return "%.1fm".format(distance)
31-
return "%dm".format(distance.toInt())
32-
}
27+
fun formatDistance(distance: Double): String {
28+
if (distance < 10)
29+
return "%.1fm".format(distance)
30+
return "%dm".format(distance.toInt())
31+
}
3332

34-
fun formatTimespan(duration: Duration, millis: Boolean = false): String {
35-
if (duration.isInfinite()) {
36-
return if (duration.isPositive()) ""
37-
else "-∞"
38-
}
39-
val sb = StringBuilder()
40-
if (duration.isNegative()) sb.append("-")
41-
duration.toComponents { days, hours, minutes, seconds, nanoseconds ->
42-
if (days > 0) {
43-
sb.append(days).append("d")
44-
}
45-
if (hours > 0) {
46-
sb.append(hours).append("h")
47-
}
48-
if (minutes > 0) {
49-
sb.append(minutes).append("m")
50-
}
51-
sb.append(seconds).append("s")
52-
if (millis) {
53-
sb.append(nanoseconds / 1_000_000).append("ms")
54-
}
55-
}
56-
return sb.toString()
57-
}
33+
fun formatTimespan(duration: Duration, millis: Boolean = false): String {
34+
if (duration.isInfinite()) {
35+
return if (duration.isPositive()) ""
36+
else "-∞"
37+
}
38+
val sb = StringBuilder()
39+
if (duration.isNegative()) sb.append("-")
40+
duration.toComponents { days, hours, minutes, seconds, nanoseconds ->
41+
if (days > 0) {
42+
sb.append(days).append("d")
43+
}
44+
if (hours > 0) {
45+
sb.append(hours).append("h")
46+
}
47+
if (minutes > 0) {
48+
sb.append(minutes).append("m")
49+
}
50+
val milliTime = nanoseconds / 1_000_000
51+
val deciseconds = milliTime / 100
52+
if (millis) {
53+
sb.append(seconds).append("s")
54+
sb.append(milliTime).append("ms")
55+
} else if (duration.absoluteValue < 5.seconds && deciseconds != 0) {
56+
sb.append(seconds).append('.').append(deciseconds.digitToChar()).append("s")
57+
} else {
58+
sb.append(seconds).append("s")
59+
}
60+
Unit
61+
}
62+
return sb.toString()
63+
}
5864

5965
}

src/main/kotlin/util/textutil.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fun CharSequence.removeColorCodes(keepNonColorCodes: Boolean = false): String {
9090
}
9191

9292
val Text.unformattedString: String
93-
get() = string.removeColorCodes()
93+
get() = string.removeColorCodes() // TODO: maybe shortcircuit this with .visit
9494

9595
val Text.directLiteralStringContent: String? get() = (this.content as? PlainTextContent)?.string()
9696

0 commit comments

Comments
 (0)