Skip to content

Commit

Permalink
Support having codec for PrettyPrint
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial committed Feb 19, 2017
1 parent 1b9928c commit b1bcd67
Showing 1 changed file with 45 additions and 29 deletions.
74 changes: 45 additions & 29 deletions wvlet-core/src/main/scala/wvlet/core/tablet/text/PrettyPrint.scala
@@ -1,8 +1,6 @@
package wvlet.core.tablet.text

import java.nio.charset.StandardCharsets

import org.msgpack.value.Variable
import wvlet.core.tablet.msgpack.MessageFormatter
import wvlet.core.tablet.obj.ObjectTabletReader
import wvlet.log.LogSupport
import wvlet.obj.ObjectSchema
Expand All @@ -12,51 +10,69 @@ import wvlet.obj.ObjectSchema
*/
object PrettyPrint extends LogSupport {

def show[A](seq:Seq[A], limit:Int=20) {
private val defaultPrinter = new PrettyPrint()

def show[A](seq: Seq[A], limit: Int = 20) {
defaultPrinter.pp(seq.take(limit))
}

def pp[A](seq: Seq[A]) {
info(defaultPrinter.pf(seq).mkString("\n"))
}

def screenTextLength(s: String): Int = {
(for (i <- 0 until s.length) yield {
val ch = s.charAt(i)
if (ch < 128) {
1
}
else {
2
}
}).sum
}

def maxColWidths(rows: Seq[Seq[String]]): IndexedSeq[Int] = {
val maxWidth = (0 until rows.head.length).map(i => 0).toArray
for (r <- rows; (c, i) <- r.zipWithIndex) {
maxWidth(i) = math.max(screenTextLength(c), maxWidth(i))
}
maxWidth.toIndexedSeq
}

def pad(s: String, colWidth: Int): String = {
(" " * Math.max(0, colWidth - screenTextLength(s))) + s
}

}

class PrettyPrint(codec: Map[Class[_], MessageFormatter[_]] = Map.empty) extends LogSupport {
def show[A](seq: Seq[A], limit: Int = 20) {
pp(seq.take(limit))
}

def pp[A](seq:Seq[A]) {
def pp[A](seq: Seq[A]) {
info(pf(seq).mkString("\n"))
}

def pf[A](seq:Seq[A]) : Seq[String] = {
def pf[A](seq: Seq[A]): Seq[String] = {
val b = Seq.newBuilder[Seq[String]]
val paramNames = seq.headOption.map { x =>
val paramNames = seq.headOption.map {x =>
val schema = ObjectSchema(x.getClass)
b += schema.parameters.map(_.name).toSeq
}

val reader = new ObjectTabletReader(seq)
val reader = new ObjectTabletReader(seq, codec)
b ++= (reader | RecordPrinter)
val s = Seq.newBuilder[String]
val rows = b.result
val colWidth = maxColWidths(rows)
val colWidth = PrettyPrint.maxColWidths(rows)
for (r <- rows) {
val cols = for ((c, i) <- r.zipWithIndex) yield pad(c, colWidth(i))
val cols = for ((c, i) <- r.zipWithIndex) yield PrettyPrint.pad(c, colWidth(i))
s += cols.mkString(" ")
}
s.result()
}

def screenTextLength(s:String) : Int = {
(for (i <- 0 until s.length) yield {
val ch = s.charAt(i)
if (ch < 128) 1 else 2
}).sum
}

def maxColWidths(rows:Seq[Seq[String]]) : IndexedSeq[Int] = {
val maxWidth = (0 until rows.head.length).map(i => 0).toArray
for(r <- rows; (c, i) <- r.zipWithIndex) {
maxWidth(i) = math.max(screenTextLength(c), maxWidth(i))
}
maxWidth.toIndexedSeq
}

def pad(s:String, colWidth:Int) : String = {
(" " * Math.max(0, colWidth - screenTextLength(s))) + s
}

}

0 comments on commit b1bcd67

Please sign in to comment.