Skip to content

Commit b99c178

Browse files
committed
simplify ifClosed logic
1 parent 6126abe commit b99c178

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ class StormfrontClient(
559559
} else {
560560
doAppendToStream(styledText, stream, ignoreWhenBlank)
561561
}
562-
if (stream == mainStream || windows[stream.name]?.styleIfClosed == "main") {
562+
if (stream == mainStream || ifClosedStream(stream) == mainStream) {
563563
isPrompting = false
564564
val text = styledText.toString()
565565
if (text.isNotBlank()) {
@@ -573,10 +573,10 @@ class StormfrontClient(
573573
if (ignoreWhenBlank && styledText.isBlank())
574574
return
575575
stream.appendLine(styledText, ignoreWhenBlank)
576-
doIfClosed(stream.name) { targetStream ->
577-
val currentWindow = windows[stream.name]
576+
doIfClosed(stream) { targetStream ->
577+
val style = windows[stream.name]?.styleIfClosed
578578
targetStream.appendLine(
579-
text = currentWindow?.styleIfClosed?.let { styledText.applyStyle(WarlockStyle(it)) } ?: styledText,
579+
text = style?.let { styledText.applyStyle(WarlockStyle(it)) } ?: styledText,
580580
ignoreWhenBlank = ignoreWhenBlank
581581
)
582582
}
@@ -598,16 +598,24 @@ class StormfrontClient(
598598
buffer = null
599599
}
600600

601-
private suspend fun doIfClosed(streamName: String, action: suspend (TextStream) -> Unit) {
602-
val currentWindow = windows[streamName]
603-
if (currentWindow != null) {
604-
val ifClosed = currentWindow.ifClosed ?: "main"
605-
if (ifClosed != streamName && ifClosed.isNotBlank() && !openWindows.value.contains(streamName)) {
606-
action(getStream(ifClosed))
601+
private suspend fun doIfClosed(stream: TextStream, action: suspend (TextStream) -> Unit) {
602+
if (!openWindows.value.contains(stream.name)) {
603+
ifClosedStream(stream)?.let { closedStream ->
604+
action(closedStream)
607605
}
608606
}
609607
}
610608

609+
private fun ifClosedStream(stream: TextStream): TextStream? {
610+
val window = windows[stream.name] ?: return null
611+
val ifClosedName = window.ifClosed ?: "main"
612+
return if (stream.name != ifClosedName && ifClosedName.isNotBlank()) {
613+
getStream(ifClosedName)
614+
} else {
615+
null
616+
}
617+
}
618+
611619
override suspend fun sendCommand(line: String, echo: Boolean): SendCommandType {
612620
if (echo) {
613621
printCommand(line)

0 commit comments

Comments
 (0)