in nlpcraft/src/main/scala/org/apache/nlpcraft/internal/ascii/NCAsciiTable.scala [53:176]
def apply(sty: String): Style =
val cs = new Style
if sty.nonEmpty then
for (e <- sty.split(','))
val a = e.split(":")
require(a.length == 2, s"Invalid cell style: ${e.trim}")
val a0 = a(0).trim
val a1 = a(1).trim
a0 match
case "leftPad" => cs.leftPad = a1.toInt
case "rightPad" => cs.rightPad = a1.toInt
case "maxWidth" => cs.maxWidth = a1.toInt
case "align" => cs.align = a1.toLowerCase
case _ => assert(assertion = false, s"Invalid style: ${e.trim}")
require(cs.leftPad >= 0, "Style 'leftPad' must >= 0.")
require(cs.rightPad >= 0, "Style 'rightPad' must >= 0.")
require(cs.maxWidth > 0, "Style 'maxWidth' must > 0.")
require(cs.align == "center" || cs.align == "left" || cs.align == "right", "Style 'align' must be 'left', 'right' or 'center'.")
cs
/**
* Cell holder.
*
* @param style
* @param lines Lines that are already cut up per `style`, if required.
*/
private sealed case class Cell(style: Style, lines: Seq[String]):
// Cell's calculated width including padding.
lazy val width: Int = style.padding + (if height > 0 then lines.map(_.length).max else 0)
// Gets height of the cell.
lazy val height: Int = lines.length
/**
* Margin holder.
*/
private sealed case class Margin(
top: Int = 0,
right: Int = 0,
bottom: Int = 0,
left: Int = 0
)
// Table drawing symbols.
private val HDR_HOR = "="
private val HDR_VER = "|"
private val HDR_CRS = "+"
private val ROW_HOR = "-"
private val ROW_VER = "|"
private val ROW_CRS = "+"
// Headers & rows.
private var hdr = IndexedSeq.empty[Cell]
private var rows = IndexedSeq.empty[IndexedSeq[Cell]]
// Current row, if any.
private var curRow: IndexedSeq[Cell] = _
// Table's margin, if any.
private var margin = Margin()
/**
* Global flag indicating whether or not to draw inside horizontal lines
* between individual rows.
*/
var insideBorder = false
/**
* Global Flag indicating whether of not to automatically draw horizontal lines
* for multiline rows.
*/
var multiLineAutoBorder = true
/**
* If lines exceeds the style's maximum width it will be broken up
* either by nearest space (by whole words) or mid-word.
*/
var breakUpByWords = true
/** Default row style. */
var defaultRowStyle: String = DFLT_ROW_STYLE
/** Default header style. */
var defaultHeaderStyle: String = DFLT_HEADER_STYLE
private def dash(ch: String, len: Int): String = ch * len
private def space(len: Int): String = " " * len
/**
* Sets table's margin.
*
* @param top Top margin.
* @param right Right margin.
* @param bottom Bottom margin.
* @param left Left margin.
*/
def margin(top: Int = 0, right: Int = 0, bottom: Int = 0, left: Int = 0): NCAsciiTable =
margin = Margin(top, right, bottom, left)
this
/**
* Starts data row.
*/
def startRow(): Unit =
curRow = IndexedSeq.empty[Cell]
/**
* Ends data row.
*/
def endRow(): Unit =
rows :+= curRow
curRow = null
/**
* Adds row (one or more row cells).
*
* @param cells Row cells. For multi-line cells - use `Seq(...)`.
*/
def +=(cells: Any*): NCAsciiTable =
startRow()
cells foreach {
case i: Iterable[_] => addRowCell(i.iterator.toSeq: _*)
case a => addRowCell(a)
}