summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbuild.xml1
-rw-r--r--src/repl/scala/tools/nsc/interpreter/ConsoleReaderHelper.scala49
-rw-r--r--src/repl/scala/tools/nsc/interpreter/JLineReader.scala2
-rw-r--r--test/junit/scala/tools/nsc/interpreter/TabulatorTest.scala41
4 files changed, 73 insertions, 20 deletions
diff --git a/build.xml b/build.xml
index 37d894e7d3..0c6eb1e50d 100755
--- a/build.xml
+++ b/build.xml
@@ -933,6 +933,7 @@ TODO:
<path id="test.junit.compiler.build.path">
<pathelement location="${test.junit.classes}"/>
<path refid="quick.compiler.build.path"/>
+ <path refid="quick.repl.build.path"/>
<path refid="junit.classpath"/>
</path>
diff --git a/src/repl/scala/tools/nsc/interpreter/ConsoleReaderHelper.scala b/src/repl/scala/tools/nsc/interpreter/ConsoleReaderHelper.scala
index a4315760a2..2206c8a772 100644
--- a/src/repl/scala/tools/nsc/interpreter/ConsoleReaderHelper.scala
+++ b/src/repl/scala/tools/nsc/interpreter/ConsoleReaderHelper.scala
@@ -8,7 +8,7 @@ package interpreter
import jline.console.{ ConsoleReader, CursorBuffer }
-trait ConsoleReaderHelper { _: ConsoleReader =>
+trait ConsoleReaderHelper { _: ConsoleReader with Tabulator =>
def isAcross: Boolean
def terminal = getTerminal()
@@ -18,7 +18,8 @@ trait ConsoleReaderHelper { _: ConsoleReader =>
def readOneKey(prompt: String): Int
def eraseLine(): Unit
- private val marginSize = 3
+ val marginSize = 3
+
private def morePrompt = "--More--"
private def emulateMore(): Int = {
val key = readOneKey(morePrompt)
@@ -42,21 +43,40 @@ trait ConsoleReaderHelper { _: ConsoleReader =>
override def printColumns(items: JCollection[_ <: CharSequence]): Unit =
printColumns_(items: List[String])
+ private def printColumns_(items: List[String]): Unit = if (items exists (_ != "")) {
+ val grouped = tabulate(items)
+ var linesLeft = if (isPaginationEnabled()) height - 1 else Int.MaxValue
+ grouped foreach { xs =>
+ println(xs.mkString)
+ linesLeft -= 1
+ if (linesLeft <= 0) {
+ linesLeft = emulateMore()
+ if (linesLeft < 0)
+ return
+ }
+ }
+ }
+}
+
+trait Tabulator {
+ def isAcross: Boolean
+ def width: Int
+ def marginSize: Int
+
private def fits(items: List[String], width: Int): Boolean = (
(items map (_.length)).sum + (items.length - 1) * marginSize < width
)
- private def printColumns_(items: List[String]): Unit = if (items exists (_ != "")) {
- if (fits(items, width)) println(items mkString " " * marginSize)
+ def tabulate(items: List[String]): Seq[Seq[String]] = {
+ if (fits(items, width)) Seq(Seq(items mkString " " * marginSize))
else printMultiLineColumns(items)
}
- private def printMultiLineColumns(items: List[String]): Unit = {
+ private def printMultiLineColumns(items: List[String]): Seq[Seq[String]] = {
import SimpleMath._
val longest = (items map (_.length)).max
//val shortest = (items map (_.length)).min
val columnWidth = longest + marginSize
val maxcols = {
- if (isPaginationEnabled) 1
- else if (columnWidth >= width) 1
+ if (columnWidth >= width) 1
else 1 max (width / columnWidth) // make sure it doesn't divide to 0
}
val nrows = items.size /% maxcols
@@ -65,7 +85,8 @@ trait ConsoleReaderHelper { _: ConsoleReader =>
val padded = items map (s"%-${columnWidth}s" format _)
val xwise = isAcross || ncols > items.length
val grouped: Seq[Seq[String]] =
- if (xwise) (padded grouped groupSize).toSeq
+ if (groupSize == 1) Seq(items)
+ else if (xwise) (padded grouped groupSize).toSeq
else {
val h = 1 max padded.size /% groupSize
val cols = (padded grouped h).toList
@@ -73,17 +94,7 @@ trait ConsoleReaderHelper { _: ConsoleReader =>
for (j <- 0 until groupSize) yield
if (i < cols(j).size) cols(j)(i) else ""
}
-
- var linesLeft = if (isPaginationEnabled()) height - 1 else Int.MaxValue
- grouped foreach { xs =>
- println(xs.mkString)
- linesLeft -= 1
- if (linesLeft <= 0) {
- linesLeft = emulateMore()
- if (linesLeft < 0)
- return
- }
- }
+ grouped
}
}
diff --git a/src/repl/scala/tools/nsc/interpreter/JLineReader.scala b/src/repl/scala/tools/nsc/interpreter/JLineReader.scala
index 970efc1282..e50c7d3e0a 100644
--- a/src/repl/scala/tools/nsc/interpreter/JLineReader.scala
+++ b/src/repl/scala/tools/nsc/interpreter/JLineReader.scala
@@ -33,7 +33,7 @@ class JLineReader(_completion: => Completion) extends InteractiveReader {
}
}
- class JLineConsoleReader extends ConsoleReader with ConsoleReaderHelper {
+ class JLineConsoleReader extends ConsoleReader with ConsoleReaderHelper with Tabulator {
val isAcross = interpreter.`package`.isAcross
this setPaginationEnabled interpreter.`package`.isPaged
diff --git a/test/junit/scala/tools/nsc/interpreter/TabulatorTest.scala b/test/junit/scala/tools/nsc/interpreter/TabulatorTest.scala
new file mode 100644
index 0000000000..e252942f89
--- /dev/null
+++ b/test/junit/scala/tools/nsc/interpreter/TabulatorTest.scala
@@ -0,0 +1,41 @@
+package scala.tools.nsc
+package interpreter
+
+//import org.junit.Assert._
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+
+case class Tabby(width: Int = 80, isAcross: Boolean = false, marginSize: Int = 3) extends Tabulator
+
+@RunWith(classOf[JUnit4])
+class TabulatorTest {
+
+ @Test def oneliner() = {
+ val sut = Tabby()
+ val items = List("a", "b", "c")
+ val res = sut tabulate items
+ assert(res.size == 1)
+ assert(res(0).size == 1)
+ assert(res(0)(0) startsWith "a")
+ assert(res(0)(0) endsWith "c")
+ }
+ @Test def twoliner() = {
+ val sut = Tabby(width = 40)
+ val items = List("a" * 15, "b" * 15, "c" * 15)
+ val res = sut tabulate items
+ assert(res.size == 2)
+ assert(res(0).size == 2)
+ assert(res(1).size == 2) // trailing empty strings
+ assert(res(1)(0) startsWith "b")
+ }
+ @Test def twolinerx() = {
+ val sut = Tabby(width = 40, isAcross = true)
+ val items = List("a" * 15, "b" * 15, "c" * 15)
+ val res = sut tabulate items
+ assert(res.size == 2)
+ assert(res(0).size == 2)
+ assert(res(1).size == 1) // no trailing empty strings
+ assert(res(1)(0) startsWith "c")
+ }
+}