From 0f90909a4aadbbb835d7d12915ff6d9acab09370 Mon Sep 17 00:00:00 2001 From: Michael Pollmeier Date: Tue, 13 Feb 2024 17:41:54 +0100 Subject: [PATCH] adjust column width dynamically by line length (#418) --- .../traversal/help/TableTests.scala | 50 +++++++++---------- .../overflowdb/traversal/help/Table.scala | 14 ++++-- .../traversal/help/TraversalHelp.scala | 2 +- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/traversal-tests/src/test/scala/overflowdb/traversal/help/TableTests.scala b/traversal-tests/src/test/scala/overflowdb/traversal/help/TableTests.scala index a2bd5e39..728e6000 100644 --- a/traversal-tests/src/test/scala/overflowdb/traversal/help/TableTests.scala +++ b/traversal-tests/src/test/scala/overflowdb/traversal/help/TableTests.scala @@ -11,18 +11,18 @@ class TableTests extends AnyWordSpec { Seq("column a", "column b"), Seq( Seq("abc 1", "bde 1"), - Seq("abc 2", "bde 2") + Seq("abc 2", "bde 2 - little more content here") ) ) - implicit val availableWidthProvider: AvailableWidthProvider = new Table.ConstantWidth(100) + implicit val availableWidthProvider: AvailableWidthProvider = new Table.ConstantWidth(50) table.render.trim shouldBe - """┌─────────────────────────────────────────────────┬────────────────────────────────────────────────┐ - |│column a │column b │ - |├─────────────────────────────────────────────────┼────────────────────────────────────────────────┤ - |│abc 1 │bde 1 │ - |│abc 2 │bde 2 │ - |└─────────────────────────────────────────────────┴────────────────────────────────────────────────┘ + """┌────────┬────────────────────────────────┐ + |│column a│column b │ + |├────────┼────────────────────────────────┤ + |│abc 1 │bde 1 │ + |│abc 2 │bde 2 - little more content here│ + |└────────┴────────────────────────────────┘ |""".stripMargin.trim } @@ -32,34 +32,34 @@ class TableTests extends AnyWordSpec { Seq( Seq( "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 " + - "ex ea commodo consequat." + " dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip" ) ) ) - var currentTerminalWidth = 80 // think "looking up current value from an actual terminal" + var currentTerminalWidth = 50 // think "looking up current value from an actual terminal" 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 ex ea commodo consequat. │ - |└──────────────────────────────────────────────────────────────────────────────┘ + """┌───────────────────────────────────────────────────────┐ + |│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 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 ex ea commodo consequat. │ - |└──────────────────────────────────────────────────────────────────────────────────────────────────┘ + """┌───────────────────────────────────────────────────────────────────────────────────────────────┐ + |│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 } diff --git a/traversal/src/main/scala/overflowdb/traversal/help/Table.scala b/traversal/src/main/scala/overflowdb/traversal/help/Table.scala index 2ca673d5..66a45393 100644 --- a/traversal/src/main/scala/overflowdb/traversal/help/Table.scala +++ b/traversal/src/main/scala/overflowdb/traversal/help/Table.scala @@ -1,13 +1,13 @@ package overflowdb.traversal.help -import de.vandermeer.asciitable.AsciiTable +import de.vandermeer.asciitable.{AsciiTable, CWC_LongestLine} import de.vandermeer.asciithemes.TA_GridThemes import de.vandermeer.skb.interfaces.transformers.textformat.TextAlignment import overflowdb.traversal.help.Table._ import scala.jdk.CollectionConverters.SeqHasAsJava -case class Table(columnNames: Seq[String], rows: Seq[Seq[String]]) { +case class Table(columnNames: Seq[String], rows: Seq[Row]) { def render(implicit availableWidthProvider: AvailableWidthProvider): String = { if (columnNames.isEmpty && rows.isEmpty) { @@ -24,16 +24,22 @@ case class Table(columnNames: Seq[String], rows: Seq[Seq[String]]) { table.getContext.setGridTheme(TA_GridThemes.FULL) table.setTextAlignment(TextAlignment.LEFT) + val renderingWidth = math.max(availableWidthProvider.apply(), 60) + val minWidth = 5 + val maxWidth = renderingWidth - minWidth + val columnWidthCalculator = new CWC_LongestLine().add(minWidth, maxWidth) + table.getRenderer.setCWC(columnWidthCalculator) + // some terminal emulators (e.g. on github actions CI) report to have a width of 0... // that doesn't work for rendering a table, so we compensate by using a minimum width - val renderingWidth = math.max(availableWidthProvider.apply(), 60) table.render(renderingWidth) } } - } object Table { + type Row = Seq[String] + trait AvailableWidthProvider extends (() => Int) class ConstantWidth(width: Int) extends AvailableWidthProvider { diff --git a/traversal/src/main/scala/overflowdb/traversal/help/TraversalHelp.scala b/traversal/src/main/scala/overflowdb/traversal/help/TraversalHelp.scala index 9b670ebc..7a63c138 100644 --- a/traversal/src/main/scala/overflowdb/traversal/help/TraversalHelp.scala +++ b/traversal/src/main/scala/overflowdb/traversal/help/TraversalHelp.scala @@ -119,6 +119,6 @@ class TraversalHelp(searchPackages: DocSearchPackages) { } object TraversalHelp { - private val ColumnNames = Array("step", "description") + private val ColumnNames = Seq("step", "description") private val ColumnNamesVerbose = ColumnNames :+ "traversal name" }