Skip to content

Commit

Permalink
SI-7969 REPL -C columnar output
Browse files Browse the repository at this point in the history
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
som-snytt committed Nov 14, 2013
1 parent 7bdb3f1 commit 28cfe16
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
54 changes: 43 additions & 11 deletions src/repl/scala/tools/nsc/interpreter/ConsoleReaderHelper.scala
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ package interpreter


import jline.console.{ ConsoleReader, CursorBuffer } import jline.console.{ ConsoleReader, CursorBuffer }


trait ConsoleReaderHelper extends ConsoleReader { trait ConsoleReaderHelper { _: ConsoleReader =>
def isAcross: Boolean

def terminal = getTerminal() def terminal = getTerminal()
def width = terminal.getWidth() def width = terminal.getWidth()
def height = terminal.getHeight() def height = terminal.getHeight()
Expand Down Expand Up @@ -38,19 +40,42 @@ trait ConsoleReaderHelper extends ConsoleReader {
} }


override def printColumns(items: JCollection[_ <: CharSequence]): Unit = override def printColumns(items: JCollection[_ <: CharSequence]): Unit =
printColumns(items: List[String]) printColumns_(items: List[String])


def printColumns(items: List[String]): Unit = { private def fits(items: List[String], width: Int): Boolean = (
if (items forall (_ == "")) (items map (_.length)).sum + (items.length - 1) * marginSize < width
return )
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 var linesLeft = if (isPaginationEnabled()) height - 1 else Int.MaxValue
val columnSize = longest + marginSize grouped foreach { xs =>
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 =>
println(xs.mkString) println(xs.mkString)
linesLeft -= 1 linesLeft -= 1
if (linesLeft <= 0) { if (linesLeft <= 0) {
Expand All @@ -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
}
}
4 changes: 4 additions & 0 deletions src/repl/scala/tools/nsc/interpreter/JLineReader.scala
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class JLineReader(_completion: => Completion) extends InteractiveReader {
} }


class JLineConsoleReader extends ConsoleReader with ConsoleReaderHelper { class JLineConsoleReader extends ConsoleReader with ConsoleReaderHelper {
val isAcross = interpreter.`package`.isAcross

this setPaginationEnabled interpreter.`package`.isPaged

// ASAP // ASAP
this setExpandEvents false this setExpandEvents false


Expand Down
4 changes: 4 additions & 0 deletions src/repl/scala/tools/nsc/interpreter/ReplConfig.scala
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ trait ReplConfig {
def isReplDebug: Boolean = replProps.debug || isReplTrace def isReplDebug: Boolean = replProps.debug || isReplTrace
def isReplInfo: Boolean = replProps.info || isReplDebug def isReplInfo: Boolean = replProps.info || isReplDebug
def isReplPower: Boolean = replProps.power 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")
} }
7 changes: 7 additions & 0 deletions src/repl/scala/tools/nsc/interpreter/ReplProps.scala
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class ReplProps {
val trace = bool("scala.repl.trace") val trace = bool("scala.repl.trace")
val power = bool("scala.repl.power") 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 replAutorunCode = Prop[JFile]("scala.repl.autoruncode")
val powerInitCode = Prop[JFile]("scala.repl.power.initcode") val powerInitCode = Prop[JFile]("scala.repl.power.initcode")
val powerBanner = Prop[JFile]("scala.repl.power.banner") val powerBanner = Prop[JFile]("scala.repl.power.banner")
Expand Down

0 comments on commit 28cfe16

Please sign in to comment.