Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- implicit class SeqSeq2Table(table: Seq[Seq[Any]]) {
- private def mkStringWithSpacing(seq: Seq[Any], c: String, spacing: Int) = {
- val s = " " * spacing
- seq.mkString(c + s, s + c + s, s + c)
- }
- def asTable(): String = asTable(1)
- def asTable(spacing: Int): String = table match {
- case Seq() => ""
- case _ =>
- val sizes = for (row <- table) yield (for (cell <- row) yield if (cell == null) 0 else cell.toString.length)
- val colSizes = for (col <- sizes.transpose) yield col.max
- val rows = for (row <- table) yield formatRow(row, colSizes, spacing)
- formatRows(rowSeparator(colSizes, spacing), rows)
- }
- def formatRows(rowSeparator: String, rows: Seq[String]): String =
- (rowSeparator :: rows.head :: rowSeparator :: rows.tail.toList ::: rowSeparator :: List()).mkString("\n")
- def formatRow(row: Seq[Any], colSizes: Seq[Int], spacing: Int) = {
- val cells = (for ((item, size) <- row.zip(colSizes)) yield if (size == 0) "" else ("%" + size + "s").format(item))
- mkStringWithSpacing(cells, "|", spacing)
- }
- def rowSeparator(colSizes: Seq[Int], spacing: Int) = colSizes map { "-" * _ } mkString("+ ", " + ", " +")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement