diff --git a/README.md b/README.md index 87ffb93..8038e74 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ allprojects { } dependencies { - compile 'com.github.ivanfrolovmd:asciitable:0.0.3' + compile 'com.github.ivanfrolovmd:asciitable:0.0.4' } ``` @@ -52,12 +52,12 @@ dependencies { com.github.ivanfrolovmd asciitable - 0.0.3 + 0.0.4 ``` ### SBT ``` resolvers += "jitpack" at "https://jitpack.io" -libraryDependencies += "com.github.ivanfrolovmd" % "asciitable" % "0.0.3" +libraryDependencies += "com.github.ivanfrolovmd" % "asciitable" % "0.0.4" ``` diff --git a/src/main/scala/io/github/asciitable/AsciiTable.scala b/src/main/scala/io/github/asciitable/AsciiTable.scala index 15c9c3f..fc78ef0 100644 --- a/src/main/scala/io/github/asciitable/AsciiTable.scala +++ b/src/main/scala/io/github/asciitable/AsciiTable.scala @@ -7,6 +7,8 @@ import io.github.asciitable.AsciiTable._ import scala.collection.immutable.Stream.StreamBuilder import Maths._ +import scala.language.postfixOps + class AsciiTable { private var header: Option[Seq[String]] = None private val streamBuilder = new StreamBuilder[Seq[String]] @@ -127,16 +129,26 @@ class AsciiTable { val proportions = medians.map(m => m.toDouble / mediansSum) val availableWidth = width - medians.length - 1 - (proportions zip maximums) - .foldLeft((Seq.empty[Int], availableWidth, 1.0)) { - case ((ws, avWidth, avRatio), (colRatio, colMax)) => + (proportions zip maximums zipWithIndex) + .sortBy(_._1._1) // sort by proportions ascending + .foldLeft((Seq.empty[(Int, Int)], availableWidth, 1.0)) { + // calculate actual apportioned widths + case ((ws, avWidth, avRatio), ((colRatio, colMax), ix)) => val proportionalWidth = if (avRatio > 0) Math.ceil(avWidth * colRatio / avRatio).toInt else 0 val minWidth = columnMinWidth min colMax val actualWidth = proportionalWidth max minWidth - if (actualWidth <= avWidth && avWidth > 0) - (ws :+ actualWidth, avWidth - actualWidth, avRatio - colRatio) + (ws :+ (actualWidth, ix), avWidth - actualWidth, avRatio - colRatio) + } + ._1 + .sortBy(_._2) // sort by index + .map(_._1) + .foldLeft((Seq.empty[Int], availableWidth)) { + // cutoff at maxWidth + case ((ws, avWidth), colWidth) => + if (colWidth <= avWidth) + (ws :+ colWidth, avWidth - colWidth) else - (ws :+ 0, 0, 0) + (ws :+ 0, 0) } ._1 } diff --git a/src/test/scala/io/github/asciitable/AsciiTableTest.scala b/src/test/scala/io/github/asciitable/AsciiTableTest.scala index 7f608cc..7f1af86 100644 --- a/src/test/scala/io/github/asciitable/AsciiTableTest.scala +++ b/src/test/scala/io/github/asciitable/AsciiTableTest.scala @@ -122,7 +122,35 @@ class AsciiTableTest extends FreeSpec with Matchers { |│12345│1234…│12345│→ |└─────┴─────┴─────┘→ |""".stripMargin + } + "should render small columns when in the right side of the table" in { + val str = "12345" + AsciiTable() + .width(15) + .columnMinWidth(5) + .header("h1", "h2", "h3", "h4", "h5") + .row("0", str * 3, "1", "22", "3") + .row("0", str * 3, "1", "22", "3") + .row("0", str * 3, "1", "22", "3") + .toString shouldBe + """┌─┬─────┬─┬──┐→ + |│h│h2 │h│h4│→ + |│1│ │3│ │→ + |├─┼─────┼─┼──┤→ + |│0│12345│1│22│→ + |│ │12345│ │ │→ + |│ │12345│ │ │→ + |├─┼─────┼─┼──┤→ + |│0│12345│1│22│→ + |│ │12345│ │ │→ + |│ │12345│ │ │→ + |├─┼─────┼─┼──┤→ + |│0│12345│1│22│→ + |│ │12345│ │ │→ + |│ │12345│ │ │→ + |└─┴─────┴─┴──┘→ + |""".stripMargin } } }