From 9fb1d9e467c8d4458d004c75d9134b78248aebd5 Mon Sep 17 00:00:00 2001 From: Sean Proctor Date: Thu, 23 Jan 2025 09:28:04 -0500 Subject: [PATCH] switch back to using a style stack --- .../warlock3/core/text/WarlockStyle.kt | 1 + .../stormfront/network/StormfrontClient.kt | 20 ++++++++++++------- .../stormfront/protocol/StormfrontEvent.kt | 3 ++- .../stormfront/protocol/elements/AHandler.kt | 10 ++++++---- .../stormfront/protocol/elements/BHandler.kt | 10 ++++++---- .../protocol/elements/PopBoldHandler.kt | 7 ++++--- .../protocol/elements/PresetHandler.kt | 12 ++++++----- .../protocol/elements/PushBoldHandler.kt | 4 ++-- .../protocol/elements/StyleHandler.kt | 14 +++++++++---- 9 files changed, 51 insertions(+), 30 deletions(-) diff --git a/core/src/commonMain/kotlin/warlockfe/warlock3/core/text/WarlockStyle.kt b/core/src/commonMain/kotlin/warlockfe/warlock3/core/text/WarlockStyle.kt index da9a38fc..da55cc60 100644 --- a/core/src/commonMain/kotlin/warlockfe/warlock3/core/text/WarlockStyle.kt +++ b/core/src/commonMain/kotlin/warlockfe/warlock3/core/text/WarlockStyle.kt @@ -13,5 +13,6 @@ data class WarlockStyle(val name: String, val annotation: Pair? val Thought = WarlockStyle("thought") val Watching = WarlockStyle("watching") val Whisper = WarlockStyle("whisper") + val Default = WarlockStyle("") } } \ No newline at end of file diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/network/StormfrontClient.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/network/StormfrontClient.kt index 448f3583..2141c6ff 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/network/StormfrontClient.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/network/StormfrontClient.kt @@ -8,6 +8,7 @@ import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.persistentMapOf import kotlinx.collections.immutable.plus import kotlinx.collections.immutable.toPersistentHashSet +import kotlinx.collections.immutable.toPersistentList import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -73,15 +74,16 @@ import warlockfe.warlock3.stormfront.protocol.StormfrontModeEvent import warlockfe.warlock3.stormfront.protocol.StormfrontNavEvent import warlockfe.warlock3.stormfront.protocol.StormfrontOutputEvent import warlockfe.warlock3.stormfront.protocol.StormfrontParseErrorEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent import warlockfe.warlock3.stormfront.protocol.StormfrontProgressBarEvent import warlockfe.warlock3.stormfront.protocol.StormfrontPromptEvent import warlockfe.warlock3.stormfront.protocol.StormfrontPropertyEvent import warlockfe.warlock3.stormfront.protocol.StormfrontProtocolHandler +import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent import warlockfe.warlock3.stormfront.protocol.StormfrontRoundTimeEvent import warlockfe.warlock3.stormfront.protocol.StormfrontSettingsInfoEvent import warlockfe.warlock3.stormfront.protocol.StormfrontStreamEvent import warlockfe.warlock3.stormfront.protocol.StormfrontStreamWindowEvent -import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent import warlockfe.warlock3.stormfront.protocol.StormfrontTimeEvent import warlockfe.warlock3.stormfront.protocol.StormfrontUnhandledTagEvent import warlockfe.warlock3.stormfront.stream.StormfrontWindow @@ -94,6 +96,7 @@ import java.net.Socket import java.net.SocketException import java.net.SocketTimeoutException import java.nio.charset.Charset +import java.util.* import java.util.concurrent.ConcurrentHashMap import kotlin.math.max @@ -240,7 +243,7 @@ class StormfrontClient( private var parseText = true private var currentStream: TextStream = mainStream - private var currentStyle: WarlockStyle? = null + private val styleStack = Stack() // Output style is for echo style! Not received text private var outputStyle: WarlockStyle? = null @@ -337,12 +340,15 @@ class StormfrontClient( is StormfrontOutputEvent -> outputStyle = event.style - is StormfrontStyleEvent -> - currentStyle = event.style + is StormfrontPushStyleEvent -> + styleStack.push(event.style) + + StormfrontPopStyleEvent -> + styleStack.pop() is StormfrontPromptEvent -> { currentTypeAhead.update { max(0, it - 1) } - currentStyle = null + styleStack.clear() currentStream = mainStream if (!isPrompting) { mainStream.appendPartial( @@ -418,7 +424,7 @@ class StormfrontClient( is StormfrontComponentDefinitionEvent -> { // Should not happen on main stream, so don't clear prompt - val styles = currentStyle?.let { persistentListOf(it) } ?: persistentListOf() + val styles = styleStack.toPersistentList() bufferText( text = StyledString( persistentListOf( @@ -535,7 +541,7 @@ class StormfrontClient( private fun bufferText(text: StyledString) { var styledText = text - currentStyle?.let { styledText = styledText.applyStyle(it) } + styleStack.asReversed().forEach { styledText = styledText.applyStyle(it) } outputStyle?.let { styledText = styledText.applyStyle(it) } buffer = buffer?.plus(styledText) ?: styledText } diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/StormfrontEvent.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/StormfrontEvent.kt index 2f22e5c5..27e0ea42 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/StormfrontEvent.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/StormfrontEvent.kt @@ -14,7 +14,8 @@ data class StormfrontClearStreamEvent(val id: String) : StormfrontEvent data class StormfrontModeEvent(val id: String?) : StormfrontEvent data class StormfrontAppEvent(val character: String?, val game: String?) : StormfrontEvent data class StormfrontOutputEvent(val style: WarlockStyle?) : StormfrontEvent -data class StormfrontStyleEvent(val style: WarlockStyle?) : StormfrontEvent +data class StormfrontPushStyleEvent(val style: WarlockStyle) : StormfrontEvent +data object StormfrontPopStyleEvent : StormfrontEvent data class StormfrontPromptEvent(val text: String) : StormfrontEvent data class StormfrontTimeEvent(val time: String) : StormfrontEvent data class StormfrontRoundTimeEvent(val time: String) : StormfrontEvent diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/AHandler.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/AHandler.kt index 07c882dc..d9a73db1 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/AHandler.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/AHandler.kt @@ -1,9 +1,11 @@ package warlockfe.warlock3.stormfront.protocol.elements import warlockfe.warlock3.core.text.WarlockStyle -import warlockfe.warlock3.stormfront.protocol.* +import warlockfe.warlock3.stormfront.protocol.BaseElementListener +import warlockfe.warlock3.stormfront.protocol.StartElement import warlockfe.warlock3.stormfront.protocol.StormfrontEvent -import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent class AHandler : BaseElementListener() { override fun startElement(element: StartElement): StormfrontEvent { @@ -14,10 +16,10 @@ class AHandler : BaseElementListener() { // TODO handle GS stuff here null } - return StormfrontStyleEvent(WarlockStyle.Link(action)) + return StormfrontPushStyleEvent(WarlockStyle.Link(action)) } override fun endElement(): StormfrontEvent { - return StormfrontStyleEvent(null) + return StormfrontPopStyleEvent } } \ No newline at end of file diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/BHandler.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/BHandler.kt index 234b2a65..03ab5678 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/BHandler.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/BHandler.kt @@ -1,16 +1,18 @@ package warlockfe.warlock3.stormfront.protocol.elements import warlockfe.warlock3.core.text.WarlockStyle -import warlockfe.warlock3.stormfront.protocol.* +import warlockfe.warlock3.stormfront.protocol.BaseElementListener +import warlockfe.warlock3.stormfront.protocol.StartElement import warlockfe.warlock3.stormfront.protocol.StormfrontEvent -import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent class BHandler : BaseElementListener() { override fun startElement(element: StartElement): StormfrontEvent { - return StormfrontStyleEvent(WarlockStyle.Bold) + return StormfrontPushStyleEvent(WarlockStyle.Bold) } override fun endElement(): StormfrontEvent { - return StormfrontStyleEvent(null) + return StormfrontPopStyleEvent } } \ No newline at end of file diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PopBoldHandler.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PopBoldHandler.kt index d9e2bd1e..26f13968 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PopBoldHandler.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PopBoldHandler.kt @@ -1,11 +1,12 @@ package warlockfe.warlock3.stormfront.protocol.elements -import warlockfe.warlock3.stormfront.protocol.* +import warlockfe.warlock3.stormfront.protocol.BaseElementListener +import warlockfe.warlock3.stormfront.protocol.StartElement import warlockfe.warlock3.stormfront.protocol.StormfrontEvent -import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent class PopBoldHandler : BaseElementListener() { override fun startElement(element: StartElement): StormfrontEvent { - return StormfrontStyleEvent(null) + return StormfrontPopStyleEvent } } \ No newline at end of file diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PresetHandler.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PresetHandler.kt index cf73c256..7ee370d6 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PresetHandler.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PresetHandler.kt @@ -1,17 +1,19 @@ package warlockfe.warlock3.stormfront.protocol.elements import warlockfe.warlock3.core.text.WarlockStyle -import warlockfe.warlock3.stormfront.protocol.* +import warlockfe.warlock3.stormfront.protocol.BaseElementListener +import warlockfe.warlock3.stormfront.protocol.StartElement import warlockfe.warlock3.stormfront.protocol.StormfrontEvent -import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent class PresetHandler : BaseElementListener() { - override fun startElement(element: StartElement): StormfrontStyleEvent { + override fun startElement(element: StartElement): StormfrontEvent { val style = element.attributes["id"]?.let { WarlockStyle(it) } - return StormfrontStyleEvent(style) + return StormfrontPushStyleEvent(style ?: WarlockStyle.Default) } override fun endElement(): StormfrontEvent { - return StormfrontStyleEvent(null) + return StormfrontPopStyleEvent } } \ No newline at end of file diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PushBoldHandler.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PushBoldHandler.kt index 1035fd42..8b1126ac 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PushBoldHandler.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/PushBoldHandler.kt @@ -4,10 +4,10 @@ import warlockfe.warlock3.core.text.WarlockStyle import warlockfe.warlock3.stormfront.protocol.BaseElementListener import warlockfe.warlock3.stormfront.protocol.StartElement import warlockfe.warlock3.stormfront.protocol.StormfrontEvent -import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent class PushBoldHandler() : BaseElementListener() { override fun startElement(element: StartElement): StormfrontEvent { - return StormfrontStyleEvent(WarlockStyle.Bold) + return StormfrontPushStyleEvent(WarlockStyle.Bold) } } \ No newline at end of file diff --git a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/StyleHandler.kt b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/StyleHandler.kt index 040b95d9..52785655 100644 --- a/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/StyleHandler.kt +++ b/stormfront/src/main/kotlin/warlockfe/warlock3/stormfront/protocol/elements/StyleHandler.kt @@ -3,11 +3,17 @@ package warlockfe.warlock3.stormfront.protocol.elements import warlockfe.warlock3.core.text.WarlockStyle import warlockfe.warlock3.stormfront.protocol.BaseElementListener import warlockfe.warlock3.stormfront.protocol.StartElement -import warlockfe.warlock3.stormfront.protocol.StormfrontStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPopStyleEvent +import warlockfe.warlock3.stormfront.protocol.StormfrontPushStyleEvent class StyleHandler : BaseElementListener() { - override fun startElement(element: StartElement): StormfrontStyleEvent { - val style = element.attributes["id"]?.let { WarlockStyle(it) } - return StormfrontStyleEvent(style) + override fun startElement(element: StartElement): StormfrontPushStyleEvent { + return element.attributes["id"] + ?.let { WarlockStyle(it) } + .let { StormfrontPushStyleEvent(it ?: WarlockStyle.Default) } + } + + override fun endElement(): StormfrontPopStyleEvent { + return StormfrontPopStyleEvent } } \ No newline at end of file