Skip to content

Commit

Permalink
Merge pull request #6 from ivanfrolovmd/fix-width-calc
Browse files Browse the repository at this point in the history
Fix width calculations for small columns
  • Loading branch information
ivanfrolovmd committed Jul 27, 2018
2 parents 2c31324 + 4e1760b commit 1a450f9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ allprojects {
}
dependencies {
compile 'com.github.ivanfrolovmd:asciitable:0.0.3'
compile 'com.github.ivanfrolovmd:asciitable:0.0.4'
}
```

Expand All @@ -52,12 +52,12 @@ dependencies {
<dependency>
<groupId>com.github.ivanfrolovmd</groupId>
<artifactId>asciitable</artifactId>
<version>0.0.3</version>
<version>0.0.4</version>
</dependency>
```

### SBT
```
resolvers += "jitpack" at "https://jitpack.io"
libraryDependencies += "com.github.ivanfrolovmd" % "asciitable" % "0.0.3"
libraryDependencies += "com.github.ivanfrolovmd" % "asciitable" % "0.0.4"
```
24 changes: 18 additions & 6 deletions src/main/scala/io/github/asciitable/AsciiTable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down Expand Up @@ -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
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/scala/io/github/asciitable/AsciiTableTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

0 comments on commit 1a450f9

Please sign in to comment.