Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust column width dynamically by line length #146

Merged
merged 1 commit into from
Feb 14, 2024
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
18 changes: 12 additions & 6 deletions core/src/main/scala/flatgraph/help/Table.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package flatgraph.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 scala.jdk.CollectionConverters.SeqHasAsJava

import Table.*

case class Table(columnNames: Seq[String], rows: Seq[Seq[String]]) {
import scala.jdk.CollectionConverters.SeqHasAsJava

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
50 changes: 25 additions & 25 deletions core/src/test/scala/flatgraph/help/TableTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import flatgraph.help.Table.AvailableWidthProvider
class TableTests extends AnyWordSpec {

"render a nice generic table" in {
val table = Table(Seq("column a", "column b"), Seq(Seq("abc 1", "bde 1"), Seq("abc 2", "bde 2")))
val table = Table(Seq("column a", "column b"), Seq(Seq("abc 1", "bde 1"), 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 @@ -26,34 +26,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
Loading