Skip to content

Commit

Permalink
adjust column width dynamically by line length
Browse files Browse the repository at this point in the history
  • Loading branch information
mpollmeier committed Feb 13, 2024
1 parent ad1d1cd commit 626d691
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
14 changes: 10 additions & 4 deletions traversal/src/main/scala/overflowdb/traversal/help/Table.scala
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 626d691

Please sign in to comment.