Repository: ajalt/mordant
Pull request #85 (2023-01-29 20:07:11)

Ignore OverflowWrap when Whitespace.wrap=false
#84 pointed out that the behavior was incorrect when combining OverflowWrap and Whitespace.wrap=false


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d080cbfd..7c6f186b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,9 @@
 # Changelog
 
+## Unreleased
+### Fixed
+- `OverflowWrap` is now properly ignored when using a non-wrapping `Whitespace` value. 
+
 ## 2.0.0-beta11
 ### Added
 - Tables and other layouts can now be completely empty [(#82)](https://github.com/ajalt/mordant/issues/82)
diff --git a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/rendering/OverflowWrap.kt b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/rendering/OverflowWrap.kt
index 69014927..0983f305 100644
--- a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/rendering/OverflowWrap.kt
+++ b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/rendering/OverflowWrap.kt
@@ -4,7 +4,9 @@ package com.github.ajalt.mordant.rendering
  * Setting for handling of long words that exceed the line length by themselves.
  *
  * These values correspond to the values of the CSS
- * [overflow-wrap][https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-wrap] property.
+ * [overflow-wrap](https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-wrap) property.
+ *
+ * Note that `OverflowWrap` has no effect unless [Whitespace].[wrap][Whitespace.wrap] is `true`.
  */
 enum class OverflowWrap {
     /** Don't break or alter long words */
diff --git a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/rendering/Whitespace.kt b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/rendering/Whitespace.kt
index 814de8a1..674dd426 100644
--- a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/rendering/Whitespace.kt
+++ b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/rendering/Whitespace.kt
@@ -4,7 +4,7 @@ package com.github.ajalt.mordant.rendering
  * Settings for handling of whitespace and line wrapping.
  *
  * These values correspond to the values of the CSS
- * [white-space][https://developer.mozilla.org/en-US/docs/Web/CSS/white-space] property.
+ * [white-space](https://developer.mozilla.org/en-US/docs/Web/CSS/white-space) property.
  *
  * @property collapseNewlines If true, line breaks in the text will be replaced with spaces.
  * @property collapseSpaces If true, consecutive spaces will be replaced with a single space.
diff --git a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/widgets/Text.kt b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/widgets/Text.kt
index 6572ceca..90f38aae 100644
--- a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/widgets/Text.kt
+++ b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/widgets/Text.kt
@@ -130,8 +130,8 @@ class Text internal constructor(
                     if (pieceIsWhitespace) continue
                 }
 
-                // overflow wrap
-                if (cellWidth > wrapWidth) {
+                // overflow wrap if the span by itself is longer than the line
+                if (whitespace.wrap && cellWidth > wrapWidth) {
                     when (overflowWrap) {
                         OverflowWrap.NORMAL -> {
                         }


Test patch:
diff --git a/mordant/src/commonTest/kotlin/com/github/ajalt/mordant/rendering/TextOverflowWrapTest.kt b/mordant/src/commonTest/kotlin/com/github/ajalt/mordant/rendering/TextOverflowWrapTest.kt
index 7e8e3ee5..2d5f1c86 100644
--- a/mordant/src/commonTest/kotlin/com/github/ajalt/mordant/rendering/TextOverflowWrapTest.kt
+++ b/mordant/src/commonTest/kotlin/com/github/ajalt/mordant/rendering/TextOverflowWrapTest.kt
@@ -7,45 +7,66 @@ import kotlin.test.Test
 
 class TextOverflowWrapTest : RenderingTest() {
     @Test
-    fun normal() = doTest(OverflowWrap.NORMAL, """
+    fun normal() = doTest(
+        """
         ░The weather today is
         ░21°C in
         ░Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch
-    """)
+        """, OverflowWrap.NORMAL
+    )
 
     @Test
-    fun break_word() = doTest(OverflowWrap.BREAK_WORD, """
+    fun break_word() = doTest(
+        """
         ░The weather today is
         ░21°C in
         ░Llanfairpwllgwyngyllgog
         ░erychwyrndrobwllllantys
         ░iliogogogoch
-    """)
+        """, OverflowWrap.BREAK_WORD
+    )
 
     @Test
-    fun truncate() = doTest(OverflowWrap.TRUNCATE, """
+    fun truncate() = doTest(
+        """
         ░The weather today is
         ░21°C in
         ░Llanfairpwllgwyngyllgog
-    """)
+        """, OverflowWrap.TRUNCATE
+    )
 
     @Test
-    fun ellipses() = doTest(OverflowWrap.ELLIPSES, """
+    fun ellipses() = doTest(
+        """
         ░The weather today is
         ░21°C in
         ░Llanfairpwllgwyngyllgo…
-    """)
+        """, OverflowWrap.ELLIPSES
+    )
 
-    private fun doTest(wrap: OverflowWrap, expected: String) {
+    @Test
+    fun nowrap() = doTest(
+        """
+        ░The weather today is 21°C in Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch
+        """, OverflowWrap.ELLIPSES, Whitespace.NOWRAP
+    )
+
+    private fun doTest(
+        expected: String,
+        wrap: OverflowWrap,
+        whitespace: Whitespace = Whitespace.NORMAL,
+        width: Int = 23,
+    ) {
         val text = """
         ░The weather today is 21°C in
         ░Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch
         """.trimMargin("░")
-        checkRender(Text(
+        val widget = Text(
             text,
-            whitespace = Whitespace.NORMAL,
+            whitespace = whitespace,
             align = TextAlign.NONE,
             overflowWrap = wrap
-        ), expected, width = 23)
+        )
+        checkRender(widget, expected, width = width)
     }
 }