Skip to content

Commit 9fb1d9e

Browse files
committed
switch back to using a style stack
1 parent a2dfc96 commit 9fb1d9e

File tree

9 files changed

+51
-30
lines changed

9 files changed

+51
-30
lines changed

core/src/commonMain/kotlin/warlockfe/warlock3/core/text/WarlockStyle.kt

+1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ data class WarlockStyle(val name: String, val annotation: Pair<String, String>?
1313
val Thought = WarlockStyle("thought")
1414
val Watching = WarlockStyle("watching")
1515
val Whisper = WarlockStyle("whisper")
16+
val Default = WarlockStyle("")
1617
}
1718
}

stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/network/StormfrontClient.kt

+13-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import kotlinx.collections.immutable.persistentListOf
88
import kotlinx.collections.immutable.persistentMapOf
99
import kotlinx.collections.immutable.plus
1010
import kotlinx.collections.immutable.toPersistentHashSet
11+
import kotlinx.collections.immutable.toPersistentList
1112
import kotlinx.coroutines.CoroutineScope
1213
import kotlinx.coroutines.Dispatchers
1314
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -73,15 +74,16 @@ import warlockfe.warlock3.stormfront.protocol.StormfrontModeEvent
7374
import warlockfe.warlock3.stormfront.protocol.StormfrontNavEvent
7475
import warlockfe.warlock3.stormfront.protocol.StormfrontOutputEvent
7576
import warlockfe.warlock3.stormfront.protocol.StormfrontParseErrorEvent
77+
import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent
7678
import warlockfe.warlock3.stormfront.protocol.StormfrontProgressBarEvent
7779
import warlockfe.warlock3.stormfront.protocol.StormfrontPromptEvent
7880
import warlockfe.warlock3.stormfront.protocol.StormfrontPropertyEvent
7981
import warlockfe.warlock3.stormfront.protocol.StormfrontProtocolHandler
82+
import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent
8083
import warlockfe.warlock3.stormfront.protocol.StormfrontRoundTimeEvent
8184
import warlockfe.warlock3.stormfront.protocol.StormfrontSettingsInfoEvent
8285
import warlockfe.warlock3.stormfront.protocol.StormfrontStreamEvent
8386
import warlockfe.warlock3.stormfront.protocol.StormfrontStreamWindowEvent
84-
import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent
8587
import warlockfe.warlock3.stormfront.protocol.StormfrontTimeEvent
8688
import warlockfe.warlock3.stormfront.protocol.StormfrontUnhandledTagEvent
8789
import warlockfe.warlock3.stormfront.stream.StormfrontWindow
@@ -94,6 +96,7 @@ import java.net.Socket
9496
import java.net.SocketException
9597
import java.net.SocketTimeoutException
9698
import java.nio.charset.Charset
99+
import java.util.*
97100
import java.util.concurrent.ConcurrentHashMap
98101
import kotlin.math.max
99102

@@ -240,7 +243,7 @@ class StormfrontClient(
240243
private var parseText = true
241244

242245
private var currentStream: TextStream = mainStream
243-
private var currentStyle: WarlockStyle? = null
246+
private val styleStack = Stack<WarlockStyle>()
244247

245248
// Output style is for echo style! Not received text
246249
private var outputStyle: WarlockStyle? = null
@@ -337,12 +340,15 @@ class StormfrontClient(
337340
is StormfrontOutputEvent ->
338341
outputStyle = event.style
339342

340-
is StormfrontStyleEvent ->
341-
currentStyle = event.style
343+
is StormfrontPushStyleEvent ->
344+
styleStack.push(event.style)
345+
346+
StormfrontPopStyleEvent ->
347+
styleStack.pop()
342348

343349
is StormfrontPromptEvent -> {
344350
currentTypeAhead.update { max(0, it - 1) }
345-
currentStyle = null
351+
styleStack.clear()
346352
currentStream = mainStream
347353
if (!isPrompting) {
348354
mainStream.appendPartial(
@@ -418,7 +424,7 @@ class StormfrontClient(
418424

419425
is StormfrontComponentDefinitionEvent -> {
420426
// Should not happen on main stream, so don't clear prompt
421-
val styles = currentStyle?.let { persistentListOf(it) } ?: persistentListOf()
427+
val styles = styleStack.toPersistentList()
422428
bufferText(
423429
text = StyledString(
424430
persistentListOf(
@@ -535,7 +541,7 @@ class StormfrontClient(
535541

536542
private fun bufferText(text: StyledString) {
537543
var styledText = text
538-
currentStyle?.let { styledText = styledText.applyStyle(it) }
544+
styleStack.asReversed().forEach { styledText = styledText.applyStyle(it) }
539545
outputStyle?.let { styledText = styledText.applyStyle(it) }
540546
buffer = buffer?.plus(styledText) ?: styledText
541547
}

stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/StormfrontEvent.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ data class StormfrontClearStreamEvent(val id: String) : StormfrontEvent
1414
data class StormfrontModeEvent(val id: String?) : StormfrontEvent
1515
data class StormfrontAppEvent(val character: String?, val game: String?) : StormfrontEvent
1616
data class StormfrontOutputEvent(val style: WarlockStyle?) : StormfrontEvent
17-
data class StormfrontStyleEvent(val style: WarlockStyle?) : StormfrontEvent
17+
data class StormfrontPushStyleEvent(val style: WarlockStyle) : StormfrontEvent
18+
data object StormfrontPopStyleEvent : StormfrontEvent
1819
data class StormfrontPromptEvent(val text: String) : StormfrontEvent
1920
data class StormfrontTimeEvent(val time: String) : StormfrontEvent
2021
data class StormfrontRoundTimeEvent(val time: String) : StormfrontEvent
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package warlockfe.warlock3.stormfront.protocol.elements
22

33
import warlockfe.warlock3.core.text.WarlockStyle
4-
import warlockfe.warlock3.stormfront.protocol.*
4+
import warlockfe.warlock3.stormfront.protocol.BaseElementListener
5+
import warlockfe.warlock3.stormfront.protocol.StartElement
56
import warlockfe.warlock3.stormfront.protocol.StormfrontEvent
6-
import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent
7+
import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent
8+
import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent
79

810
class AHandler : BaseElementListener() {
911
override fun startElement(element: StartElement): StormfrontEvent {
@@ -14,10 +16,10 @@ class AHandler : BaseElementListener() {
1416
// TODO handle GS stuff here
1517
null
1618
}
17-
return StormfrontStyleEvent(WarlockStyle.Link(action))
19+
return StormfrontPushStyleEvent(WarlockStyle.Link(action))
1820
}
1921

2022
override fun endElement(): StormfrontEvent {
21-
return StormfrontStyleEvent(null)
23+
return StormfrontPopStyleEvent
2224
}
2325
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package warlockfe.warlock3.stormfront.protocol.elements
22

33
import warlockfe.warlock3.core.text.WarlockStyle
4-
import warlockfe.warlock3.stormfront.protocol.*
4+
import warlockfe.warlock3.stormfront.protocol.BaseElementListener
5+
import warlockfe.warlock3.stormfront.protocol.StartElement
56
import warlockfe.warlock3.stormfront.protocol.StormfrontEvent
6-
import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent
7+
import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent
8+
import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent
79

810
class BHandler : BaseElementListener() {
911
override fun startElement(element: StartElement): StormfrontEvent {
10-
return StormfrontStyleEvent(WarlockStyle.Bold)
12+
return StormfrontPushStyleEvent(WarlockStyle.Bold)
1113
}
1214

1315
override fun endElement(): StormfrontEvent {
14-
return StormfrontStyleEvent(null)
16+
return StormfrontPopStyleEvent
1517
}
1618
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package warlockfe.warlock3.stormfront.protocol.elements
22

3-
import warlockfe.warlock3.stormfront.protocol.*
3+
import warlockfe.warlock3.stormfront.protocol.BaseElementListener
4+
import warlockfe.warlock3.stormfront.protocol.StartElement
45
import warlockfe.warlock3.stormfront.protocol.StormfrontEvent
5-
import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent
6+
import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent
67

78
class PopBoldHandler : BaseElementListener() {
89
override fun startElement(element: StartElement): StormfrontEvent {
9-
return StormfrontStyleEvent(null)
10+
return StormfrontPopStyleEvent
1011
}
1112
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package warlockfe.warlock3.stormfront.protocol.elements
22

33
import warlockfe.warlock3.core.text.WarlockStyle
4-
import warlockfe.warlock3.stormfront.protocol.*
4+
import warlockfe.warlock3.stormfront.protocol.BaseElementListener
5+
import warlockfe.warlock3.stormfront.protocol.StartElement
56
import warlockfe.warlock3.stormfront.protocol.StormfrontEvent
6-
import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent
7+
import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent
8+
import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent
79

810
class PresetHandler : BaseElementListener() {
9-
override fun startElement(element: StartElement): StormfrontStyleEvent {
11+
override fun startElement(element: StartElement): StormfrontEvent {
1012
val style = element.attributes["id"]?.let { WarlockStyle(it) }
11-
return StormfrontStyleEvent(style)
13+
return StormfrontPushStyleEvent(style ?: WarlockStyle.Default)
1214
}
1315

1416
override fun endElement(): StormfrontEvent {
15-
return StormfrontStyleEvent(null)
17+
return StormfrontPopStyleEvent
1618
}
1719
}

stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PushBoldHandler.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import warlockfe.warlock3.core.text.WarlockStyle
44
import warlockfe.warlock3.stormfront.protocol.BaseElementListener
55
import warlockfe.warlock3.stormfront.protocol.StartElement
66
import warlockfe.warlock3.stormfront.protocol.StormfrontEvent
7-
import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent
7+
import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent
88

99
class PushBoldHandler() : BaseElementListener() {
1010
override fun startElement(element: StartElement): StormfrontEvent {
11-
return StormfrontStyleEvent(WarlockStyle.Bold)
11+
return StormfrontPushStyleEvent(WarlockStyle.Bold)
1212
}
1313
}

stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/StyleHandler.kt

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ package warlockfe.warlock3.stormfront.protocol.elements
33
import warlockfe.warlock3.core.text.WarlockStyle
44
import warlockfe.warlock3.stormfront.protocol.BaseElementListener
55
import warlockfe.warlock3.stormfront.protocol.StartElement
6-
import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent
6+
import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent
7+
import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent
78

89
class StyleHandler : BaseElementListener() {
9-
override fun startElement(element: StartElement): StormfrontStyleEvent {
10-
val style = element.attributes["id"]?.let { WarlockStyle(it) }
11-
return StormfrontStyleEvent(style)
10+
override fun startElement(element: StartElement): StormfrontPushStyleEvent {
11+
return element.attributes["id"]
12+
?.let { WarlockStyle(it) }
13+
.let { StormfrontPushStyleEvent(it ?: WarlockStyle.Default) }
14+
}
15+
16+
override fun endElement(): StormfrontPopStyleEvent {
17+
return StormfrontPopStyleEvent
1218
}
1319
}

0 commit comments

Comments
 (0)