Skip to content

Commit

Permalink
switch back to using a style stack
Browse files Browse the repository at this point in the history
  • Loading branch information
sproctor committed Jan 23, 2025
1 parent a2dfc96 commit 9fb1d9e
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ data class WarlockStyle(val name: String, val annotation: Pair<String, String>?
val Thought = WarlockStyle("thought")
val Watching = WarlockStyle("watching")
val Whisper = WarlockStyle("whisper")
val Default = WarlockStyle("")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -240,7 +243,7 @@ class StormfrontClient(
private var parseText = true

private var currentStream: TextStream = mainStream
private var currentStyle: WarlockStyle? = null
private val styleStack = Stack<WarlockStyle>()

// Output style is for echo style! Not received text
private var outputStyle: WarlockStyle? = null
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 9fb1d9e

Please sign in to comment.