From 57562ebc45d881209e6d1451327177f0177b817f Mon Sep 17 00:00:00 2001 From: AJ Date: Mon, 2 Sep 2024 09:23:46 -0700 Subject: [PATCH] Fix whitespace of aligned table captions on narrow terminals --- CHANGELOG.md | 1 + .../ajalt/mordant/table/VerticalLayout.kt | 2 +- .../github/ajalt/mordant/widgets/Caption.kt | 2 +- .../github/ajalt/mordant/table/TableTest.kt | 25 +++++++++++++++++-- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ea8c214b..eed16680d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ - Fixed ConcurrentModificationException from progress bars when updated under very high concurrency [(#204)](https://github.com/ajalt/mordant/issues/204) - Improved performance of progress bars under high concurrency. [(#207)](https://github.com/ajalt/mordant/issues/207) - Fixed `NoClassDefFoundError` when running with certain gradle plugins [(#217)](https://github.com/ajalt/mordant/issues/217) +- Fixed whitespace of aligned table captions on narrow terminals [(#216)](https://github.com/ajalt/mordant/issues/216) ## 2.7.2 ### Fixed diff --git a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/table/VerticalLayout.kt b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/table/VerticalLayout.kt index 909676bb6..6a159ea85 100644 --- a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/table/VerticalLayout.kt +++ b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/table/VerticalLayout.kt @@ -58,7 +58,7 @@ internal class VerticalLayout private constructor( columnWidth is ColumnWidth.Expand -> width hasAlignedCells -> measure(t, width).max else -> width - } + }.coerceAtMost(width) val lines = mutableListOf() val spacingLine = when (textAlign) { NONE -> EMPTY_LINE diff --git a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/widgets/Caption.kt b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/widgets/Caption.kt index adf461e48..be3fffea5 100644 --- a/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/widgets/Caption.kt +++ b/mordant/src/commonMain/kotlin/com/github/ajalt/mordant/widgets/Caption.kt @@ -26,7 +26,7 @@ class Caption( } override fun render(t: Terminal, width: Int): Lines { - val captionWidth = content.measure(t, width).max + val captionWidth = content.measure(t, width).max.coerceAtMost(width) val lines = mutableListOf() top?.let { lines.addAll(it.render(t, captionWidth).lines) } lines.addAll(content.render(t, width).lines) diff --git a/mordant/src/commonTest/kotlin/com/github/ajalt/mordant/table/TableTest.kt b/mordant/src/commonTest/kotlin/com/github/ajalt/mordant/table/TableTest.kt index 6555e16bb..a939ae53a 100644 --- a/mordant/src/commonTest/kotlin/com/github/ajalt/mordant/table/TableTest.kt +++ b/mordant/src/commonTest/kotlin/com/github/ajalt/mordant/table/TableTest.kt @@ -11,6 +11,8 @@ import com.github.ajalt.mordant.terminal.Terminal import com.github.ajalt.mordant.test.RenderingTest import com.github.ajalt.mordant.widgets.Padding import com.github.ajalt.mordant.widgets.Text +import io.kotest.data.blocking.forAll +import io.kotest.data.row import kotlin.js.JsName import kotlin.test.Test @@ -514,6 +516,25 @@ class TableTest : RenderingTest() { body { row(1, 2, 3) } } + @[Test JsName("caption_with_truncated_table")] + fun `caption with truncated table`() = forAll( + row(TextAlign.LEFT, "Caption "), + row(TextAlign.RIGHT, " Caption"), + ) { align, expected -> + doTest( + """ + ░$expected░ + ░┌────────┐░ + ░│ long ce│░ + ░└────────┘░ + """, + width = 10 + ) { + captionTop("Caption", align = align) + body { row("long cell") } + } + } + @Test fun grid() = checkRender( grid { @@ -542,8 +563,8 @@ class TableTest : RenderingTest() { body { row(111, 222, 333) } } - private fun doTest(expected: String, builder: TableBuilder.() -> Unit) { - checkRender(table(builder), expected) + private fun doTest(expected: String, width: Int = 79, builder: TableBuilder.() -> Unit) { + checkRender(table(builder), expected, width = width) } private fun doBodyTest(expected: String, builder: SectionBuilder.() -> Unit) {