-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
help util: replace jtextutils with asciitable (#145)
* help util: replace jtextutils with asciitable * main motivation: jtextutils became vulnerable because the domain `com.massisframework` is available for sale... * also: asciitable is better * includes latest tweakments from overflowdb as of v1.186 * fix tests * fmt
- Loading branch information
1 parent
da3af3d
commit 12b9197
Showing
7 changed files
with
130 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,43 @@ | ||
package flatgraph.help | ||
|
||
import dnl.utils.text.table.TextTable | ||
import java.io.{ByteArrayOutputStream, PrintStream} | ||
import java.nio.charset.StandardCharsets | ||
import scala.util.Using | ||
|
||
case class Table(columnNames: Iterable[String], rows: Iterable[Iterable[String]]) { | ||
|
||
lazy val render: String = { | ||
Using.Manager { use => | ||
val charset = StandardCharsets.UTF_8 | ||
val baos = use(new ByteArrayOutputStream) | ||
val ps = use(new PrintStream(baos, true, charset.name)) | ||
val rowsAsArray = rows.map(_.map(_ + " ").toArray.asInstanceOf[Array[Object]]).toArray | ||
new TextTable(columnNames.toArray, rowsAsArray).printTable(ps, 0) | ||
new String(baos.toByteArray, charset) | ||
}.get | ||
import de.vandermeer.asciitable.AsciiTable | ||
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]]) { | ||
|
||
def render(implicit availableWidthProvider: AvailableWidthProvider): String = { | ||
if (columnNames.isEmpty && rows.isEmpty) { | ||
"" | ||
} else { | ||
val table = new AsciiTable() | ||
table.addRule() | ||
table.addRow(columnNames.asJava) | ||
table.addRule() | ||
if (rows.nonEmpty) { | ||
rows.map(_.asJava).foreach(table.addRow) | ||
} | ||
table.addRule() | ||
table.getContext.setGridTheme(TA_GridThemes.FULL) | ||
table.setTextAlignment(TextAlignment.LEFT) | ||
|
||
// 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 { | ||
trait AvailableWidthProvider extends (() => Int) | ||
|
||
class ConstantWidth(width: Int) extends AvailableWidthProvider { | ||
override def apply() = width | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package flatgraph.help | ||
|
||
import org.scalatest.matchers.should.Matchers._ | ||
import org.scalatest.wordspec.AnyWordSpec | ||
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"))) | ||
|
||
implicit val availableWidthProvider: AvailableWidthProvider = new Table.ConstantWidth(100) | ||
table.render.trim shouldBe | ||
"""┌─────────────────────────────────────────────────┬────────────────────────────────────────────────┐ | ||
|│column a │column b │ | ||
|├─────────────────────────────────────────────────┼────────────────────────────────────────────────┤ | ||
|│abc 1 │bde 1 │ | ||
|│abc 2 │bde 2 │ | ||
|└─────────────────────────────────────────────────┴────────────────────────────────────────────────┘ | ||
|""".stripMargin.trim | ||
} | ||
|
||
"adapt to dynamically changing terminal width" in { | ||
val table = Table( | ||
Seq("lorem ipsum"), | ||
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." | ||
) | ||
) | ||
) | ||
|
||
var currentTerminalWidth = 80 // 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. │ | ||
|└──────────────────────────────────────────────────────────────────────────────┘ | ||
|""".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. │ | ||
|└──────────────────────────────────────────────────────────────────────────────────────────────────┘ | ||
|""".stripMargin.trim | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters