From aa98d9a8c19ca27d85b62d1eccfc868440dc9ab2 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Wed, 17 Jun 2015 13:24:53 -0700 Subject: SI-9206 REPL prompt is more easily configured The scala shell prompt can be provided as either a system property or in compiler.properties. The prompt string is taken as a format string with one argument that is the version string. ``` $ scala -Dscala.repl.prompt="%nScala %s> " Welcome to Scala version 2.11.7-20150616-093756-43a56fb5a1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45). Type in expressions to have them evaluated. Type :help for more information. Scala 2.11.7-20150616-093756-43a56fb5a1> 42 res0: Int = 42 Scala 2.11.7-20150616-093756-43a56fb5a1> :quit ``` --- src/repl/scala/tools/nsc/interpreter/ILoop.scala | 22 +++++++++++----------- .../scala/tools/nsc/interpreter/ReplProps.scala | 10 +++++++++- 2 files changed, 20 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/repl/scala/tools/nsc/interpreter/ILoop.scala b/src/repl/scala/tools/nsc/interpreter/ILoop.scala index 4221126caa..11c843248a 100644 --- a/src/repl/scala/tools/nsc/interpreter/ILoop.scala +++ b/src/repl/scala/tools/nsc/interpreter/ILoop.scala @@ -197,10 +197,8 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter) echo("%d %s".format(index + offset, line)) } - private val currentPrompt = Properties.shellPromptString - /** Prompt to print when awaiting input */ - def prompt = currentPrompt + def prompt = replProps.prompt import LoopCommand.{ cmd, nullary } @@ -410,14 +408,8 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter) } private def readOneLine() = { - import scala.io.AnsiColor.{ MAGENTA, RESET } out.flush() - in readLine ( - if (replProps.colorOk) - MAGENTA + prompt + RESET - else - prompt - ) + in readLine prompt } /** The main read-eval-print loop for the repl. It calls @@ -776,6 +768,14 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter) private object paste extends Pasted { val ContinueString = " | " val PromptString = "scala> " + val testPrompt = PromptString.trim + val testOurPrompt = prompt.trim + val testBoth = testPrompt != testOurPrompt + + def isPrompt(line: String) = { + val text = line.trim + text == testOurPrompt || (testBoth && text == testPrompt) + } def interpret(line: String): Unit = { echo(line.trim) @@ -785,7 +785,7 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter) def transcript(start: String) = { echo("\n// Detected repl transcript paste: ctrl-D to finish.\n") - apply(Iterator(start) ++ readWhile(_.trim != PromptString.trim)) + apply(Iterator(start) ++ readWhile(!isPrompt(_))) } } import paste.{ ContinueString, PromptString } diff --git a/src/repl/scala/tools/nsc/interpreter/ReplProps.scala b/src/repl/scala/tools/nsc/interpreter/ReplProps.scala index 8c4faf7278..19f66e98a2 100644 --- a/src/repl/scala/tools/nsc/interpreter/ReplProps.scala +++ b/src/repl/scala/tools/nsc/interpreter/ReplProps.scala @@ -11,11 +11,19 @@ import Prop._ class ReplProps { private def bool(name: String) = BooleanProp.keyExists(name) - private def int(name: String) = IntProp(name) + private def int(name: String) = Prop[Int](name) // This property is used in TypeDebugging. Let's recycle it. val colorOk = bool("scala.color") + // Handy system prop for shell prompt, or else pick it up from compiler.properties + val prompt = { + import scala.io.AnsiColor.{ MAGENTA, RESET } + val p = Prop[String]("scala.repl.prompt").option getOrElse Properties.shellPromptString + val q = String.format(p, Properties.versionNumberString) + if (colorOk) s"$MAGENTA$q$RESET" else q + } + val info = bool("scala.repl.info") val debug = bool("scala.repl.debug") val trace = bool("scala.repl.trace") -- cgit v1.2.3