Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions help/src/main/scala/flatgraph/help/Table.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,27 @@ case class Table(columnNames: Seq[String], rows: Seq[Row]) {
if (columnNames.isEmpty && rows.isEmpty) {
""
} else {
val renderingWidth = math.max(availableWidthProvider.apply(), 60)
val minWidth = 5
val maxWidth = renderingWidth - minWidth
val allRows = columnNames +: rows
val numCols = columnNames.size
val minWidth = 1
val allRows = columnNames +: rows
val numCols = columnNames.size
val separatorWidth = numCols + 1 // │ around and between each column
val minRenderingWidth = math.max(60, numCols * minWidth + separatorWidth)
val renderingWidth = math.max(availableWidthProvider.apply(), minRenderingWidth)
val availableForContent = renderingWidth - separatorWidth

// calculate column widths: longest content per column, clamped to [minWidth, maxWidth]
val colWidths = (0 until numCols).map { col =>
// calculate natural column widths: longest content per column, clamped to minWidth
val widths = (0 until numCols).map { col =>
val longest = allRows.map(row => if (col < row.size) row(col).length else 0).max
math.min(math.max(longest, minWidth), maxWidth)
math.max(longest, minWidth)
}.toArray

// shrink widest columns until total fits within available space
val colWidths = {
while (widths.sum > availableForContent) {
val maxIdx = widths.zipWithIndex.maxBy(_._1)._2
widths(maxIdx) = math.max(widths(maxIdx) - 1, minWidth)
}
widths.toIndexedSeq
}

val sb = new StringBuilder()
Expand Down
32 changes: 17 additions & 15 deletions help/src/test/scala/flatgraph/help/TableTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,28 @@ class TableTests extends AnyWordSpec {
implicit val availableWidthProvider: AvailableWidthProvider = () => currentTerminalWidth

table.render.trim shouldBe
"""┌───────────────────────────────────────────────────────┐
|│lorem ipsum │
|├───────────────────────────────────────────────────────┤
|│Lorem ipsum dolor sit amet, consectetur adipiscing
|│elit, sed do eiusmod tempor incididunt ut labore et
|│dolore magna aliqua. Ut enim ad minim veniam, quis
|│nostrud exercitation ullamco laboris nisi ut aliquip │
|└───────────────────────────────────────────────────────┘
"""┌──────────────────────────────────────────────────────────
|│lorem ipsum
|├──────────────────────────────────────────────────────────
|│Lorem ipsum dolor sit amet, consectetur adipiscing elit,
|│sed do eiusmod tempor incididunt ut labore et dolore magna
|│aliqua. Ut enim ad minim veniam, quis nostrud exercitation
|│ullamco laboris nisi ut aliquip
|└──────────────────────────────────────────────────────────
|""".stripMargin.trim
table.render.trim.linesIterator.foreach(_.length shouldBe 60) // 60 is min rendering width

currentTerminalWidth = 100 // emulating: terminal size has changed
table.render.trim shouldBe
"""┌───────────────────────────────────────────────────────────────────────────────────────────────┐
|│lorem ipsum │
|├───────────────────────────────────────────────────────────────────────────────────────────────┤
|│Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut │
|│labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
|│laboris nisi ut aliquip │
|└───────────────────────────────────────────────────────────────────────────────────────────────┘
"""┌──────────────────────────────────────────────────────────────────────────────────────────────────
|│lorem ipsum
|├──────────────────────────────────────────────────────────────────────────────────────────────────
|│Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
|│labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
|│nisi ut aliquip
|└──────────────────────────────────────────────────────────────────────────────────────────────────
|""".stripMargin.trim
table.render.trim.linesIterator.foreach(_.length shouldBe currentTerminalWidth)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ class TraversalTests extends AnyWordSpec {
// thingTraversalHelpVerbose should include("simple.SimpleDomainTravers")
thingTraversalHelpVerbose should include("node label")
thingTraversalHelpVerbose should include("flatgraph.traversal.NodeSteps")
thingTraversalHelpVerbose should include("result to a list")
thingTraversalHelpVerbose should include(
"│.l │Execute the traversal and convert the result to a │flatgraph.traversal.GenericSteps│ │"
)
thingTraversalHelpVerbose should include("flatgraph.traversal.GenericSt")
}
}
Expand Down
Loading