1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
package io.dico.parcels2.defaultimpl
import io.dico.dicore.Formatting
import io.dico.parcels2.Privilege
import io.dico.parcels2.PrivilegeKey
import io.dico.parcels2.RawPrivileges
import io.dico.parcels2.filterProfilesWithPrivilegeTo
object InfoBuilder {
val infoStringColor1 = Formatting.GREEN
val infoStringColor2 = Formatting.AQUA
inline fun StringBuilder.appendField(field: StringBuilder.() -> Unit, value: StringBuilder.() -> Unit) {
append(infoStringColor1)
field()
append(": ")
append(infoStringColor2)
value()
append(' ')
}
inline fun StringBuilder.appendField(name: String, value: StringBuilder.() -> Unit) {
appendField({ append(name) }, value)
}
inline fun StringBuilder.appendFieldWithCount(name: String, count: Int, value: StringBuilder.() -> Unit) {
appendField({
append(name)
append('(')
append(infoStringColor2)
append(count)
append(infoStringColor1)
append(')')
}, value)
}
fun StringBuilder.appendProfilesWithPrivilege(fieldName: String, local: RawPrivileges, global: RawPrivileges?, privilege: Privilege) {
val map = linkedMapOf<PrivilegeKey, Privilege>()
local.filterProfilesWithPrivilegeTo(map, privilege)
val localCount = map.size
global?.filterProfilesWithPrivilegeTo(map, privilege)
appendPrivilegeProfiles(fieldName, map, localCount)
}
fun StringBuilder.appendPrivilegeProfiles(fieldName: String, map: LinkedHashMap<PrivilegeKey, Privilege>, localCount: Int) {
if (map.isEmpty()) return
appendFieldWithCount(fieldName, map.size) {
// first [localCount] entries are local
val separator = "$infoStringColor1, $infoStringColor2"
val iterator = map.iterator()
if (localCount != 0) {
appendPrivilegeEntry(false, iterator.next().toPair())
repeat(localCount - 1) {
append(separator)
appendPrivilegeEntry(false, iterator.next().toPair())
}
} else if (iterator.hasNext()) {
// ensure there is never a leading or trailing separator
appendPrivilegeEntry(true, iterator.next().toPair())
}
iterator.forEach { next ->
append(separator)
appendPrivilegeEntry(true, next.toPair())
}
}
}
fun StringBuilder.appendPrivilegeEntry(global: Boolean, pair: Pair<PrivilegeKey, Privilege>) {
val (key, priv) = pair
append(key.notNullName)
// suffix. Maybe T should be M for mod or something. T means they have CAN_MANAGE privilege.
append(
when {
global && priv == Privilege.CAN_MANAGE -> " (G) (T)"
global -> " (G)"
priv == Privilege.CAN_MANAGE -> " (T)"
else -> ""
}
)
}
}
|