summaryrefslogtreecommitdiff
path: root/src/repl
diff options
context:
space:
mode:
authorSom Snytt <som.snytt@gmail.com>2013-11-14 12:37:09 -0800
committerSom Snytt <som.snytt@gmail.com>2013-11-15 10:45:59 -0800
commit02359a09ebb75deee2481d48835d5352b59e1c7e (patch)
tree8a6beaddf669daedaae8e3b106112afa1ba2879d /src/repl
parent28cfe16fdde550146047f50c38de86d8020706fd (diff)
downloadscala-02359a09ebb75deee2481d48835d5352b59e1c7e.tar.gz
scala-02359a09ebb75deee2481d48835d5352b59e1c7e.tar.bz2
scala-02359a09ebb75deee2481d48835d5352b59e1c7e.zip
SI-7969 Refactor to trait with test
Make REPL classes testable in junit. Test the Tabulator.
Diffstat (limited to 'src/repl')
-rw-r--r--src/repl/scala/tools/nsc/interpreter/ConsoleReaderHelper.scala49
-rw-r--r--src/repl/scala/tools/nsc/interpreter/JLineReader.scala2
2 files changed, 31 insertions, 20 deletions
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