Skip to content
Browse files

SI-7969 REPL -C columnar output

Let `ConsoleReaderHelper.printColumns` print in a vertical
orientation (similar to `ls -C`) instead of horizontally (`ls -x`).

To support the old behavior, add a property `-Dscala.repl.format=across`
where "across" is the presumptive mnemonic for `-x`.

The format property also takes value `paged` to turn on pagination,
which currently forces single-column output (as does `ls | more`)
for simplicity.
  • Loading branch information...
1 parent 7bdb3f1 commit 28cfe16fdde550146047f50c38de86d8020706fd @som-snytt som-snytt committed Nov 13, 2013
View
54 src/repl/scala/tools/nsc/interpreter/ConsoleReaderHelper.scala
@@ -8,7 +8,9 @@ package interpreter
import jline.console.{ ConsoleReader, CursorBuffer }
-trait ConsoleReaderHelper extends ConsoleReader {
+trait ConsoleReaderHelper { _: ConsoleReader =>
+ def isAcross: Boolean
+
def terminal = getTerminal()
def width = terminal.getWidth()
def height = terminal.getHeight()
@@ -38,19 +40,42 @@ trait ConsoleReaderHelper extends ConsoleReader {
}
override def printColumns(items: JCollection[_ <: CharSequence]): Unit =
- printColumns(items: List[String])
+ printColumns_(items: List[String])
- def printColumns(items: List[String]): Unit = {
- if (items forall (_ == ""))
- return
+ 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)
+ else printMultiLineColumns(items)
+ }
+ private def printMultiLineColumns(items: List[String]): Unit = {
+ 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
+ else 1 max (width / columnWidth) // make sure it doesn't divide to 0
+ }
+ val nrows = items.size /% maxcols
+ val ncols = items.size /% nrows
+ val groupSize = ncols
+ 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
+ else {
+ val h = 1 max padded.size /% groupSize
+ val cols = (padded grouped h).toList
+ for (i <- 0 until h) yield
+ for (j <- 0 until groupSize) yield
+ if (i < cols(j).size) cols(j)(i) else ""
+ }
- val longest = items map (_.length) max
var linesLeft = if (isPaginationEnabled()) height - 1 else Int.MaxValue
- val columnSize = longest + marginSize
- val padded = items map ("%-" + columnSize + "s" format _)
- val groupSize = 1 max (width / columnSize) // make sure it doesn't divide to 0
-
- padded grouped groupSize foreach { xs =>
+ grouped foreach { xs =>
println(xs.mkString)
linesLeft -= 1
if (linesLeft <= 0) {
@@ -61,3 +86,10 @@ trait ConsoleReaderHelper extends ConsoleReader {
}
}
}
+
+private[interpreter] object SimpleMath {
+ implicit class DivRem(private val i: Int) extends AnyVal {
+ /** i/n + if (i % n != 0) 1 else 0 */
+ def /%(n: Int): Int = (i + n - 1) / n
+ }
+}
View
4 src/repl/scala/tools/nsc/interpreter/JLineReader.scala
@@ -34,6 +34,10 @@ class JLineReader(_completion: => Completion) extends InteractiveReader {
}
class JLineConsoleReader extends ConsoleReader with ConsoleReaderHelper {
+ val isAcross = interpreter.`package`.isAcross
+
+ this setPaginationEnabled interpreter.`package`.isPaged
+
// ASAP
this setExpandEvents false
View
4 src/repl/scala/tools/nsc/interpreter/ReplConfig.scala
@@ -46,4 +46,8 @@ trait ReplConfig {
def isReplDebug: Boolean = replProps.debug || isReplTrace
def isReplInfo: Boolean = replProps.info || isReplDebug
def isReplPower: Boolean = replProps.power
+
+ private def csv(p: String, v: String) = p split "," contains v
+ def isPaged: Boolean = replProps.format.isSet && csv(replProps.format.get, "paged")
+ def isAcross: Boolean = replProps.format.isSet && csv(replProps.format.get, "across")
}
View
7 src/repl/scala/tools/nsc/interpreter/ReplProps.scala
@@ -18,6 +18,13 @@ class ReplProps {
val trace = bool("scala.repl.trace")
val power = bool("scala.repl.power")
+ /** CSV of paged,across to enable pagination or `-x` style
+ * columns, "across" instead of down the column. Since
+ * pagination turns off columnar output, these flags are
+ * currently mutually exclusive.
+ */
+ val format = Prop[String]("scala.repl.format")
+
val replAutorunCode = Prop[JFile]("scala.repl.autoruncode")
val powerInitCode = Prop[JFile]("scala.repl.power.initcode")
val powerBanner = Prop[JFile]("scala.repl.power.banner")

0 comments on commit 28cfe16

Please sign in to comment.
Something went wrong with that request. Please try again.