Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit aa98d9a

Browse files
committed
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 ```
1 parent 1a9ffaa commit aa98d9a

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/repl/scala/tools/nsc/interpreter/ILoop.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,8 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
197197
echo("%d %s".format(index + offset, line))
198198
}
199199

200-
private val currentPrompt = Properties.shellPromptString
201-
202200
/** Prompt to print when awaiting input */
203-
def prompt = currentPrompt
201+
def prompt = replProps.prompt
204202

205203
import LoopCommand.{ cmd, nullary }
206204

@@ -410,14 +408,8 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
410408
}
411409

412410
private def readOneLine() = {
413-
import scala.io.AnsiColor.{ MAGENTA, RESET }
414411
out.flush()
415-
in readLine (
416-
if (replProps.colorOk)
417-
MAGENTA + prompt + RESET
418-
else
419-
prompt
420-
)
412+
in readLine prompt
421413
}
422414

423415
/** The main read-eval-print loop for the repl. It calls
@@ -776,6 +768,14 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
776768
private object paste extends Pasted {
777769
val ContinueString = " | "
778770
val PromptString = "scala> "
771+
val testPrompt = PromptString.trim
772+
val testOurPrompt = prompt.trim
773+
val testBoth = testPrompt != testOurPrompt
774+
775+
def isPrompt(line: String) = {
776+
val text = line.trim
777+
text == testOurPrompt || (testBoth && text == testPrompt)
778+
}
779779

780780
def interpret(line: String): Unit = {
781781
echo(line.trim)
@@ -785,7 +785,7 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
785785

786786
def transcript(start: String) = {
787787
echo("\n// Detected repl transcript paste: ctrl-D to finish.\n")
788-
apply(Iterator(start) ++ readWhile(_.trim != PromptString.trim))
788+
apply(Iterator(start) ++ readWhile(!isPrompt(_)))
789789
}
790790
}
791791
import paste.{ ContinueString, PromptString }

src/repl/scala/tools/nsc/interpreter/ReplProps.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,19 @@ import Prop._
1111

1212
class ReplProps {
1313
private def bool(name: String) = BooleanProp.keyExists(name)
14-
private def int(name: String) = IntProp(name)
14+
private def int(name: String) = Prop[Int](name)
1515

1616
// This property is used in TypeDebugging. Let's recycle it.
1717
val colorOk = bool("scala.color")
1818

19+
// Handy system prop for shell prompt, or else pick it up from compiler.properties
20+
val prompt = {
21+
import scala.io.AnsiColor.{ MAGENTA, RESET }
22+
val p = Prop[String]("scala.repl.prompt").option getOrElse Properties.shellPromptString
23+
val q = String.format(p, Properties.versionNumberString)
24+
if (colorOk) s"$MAGENTA$q$RESET" else q
25+
}
26+
1927
val info = bool("scala.repl.info")
2028
val debug = bool("scala.repl.debug")
2129
val trace = bool("scala.repl.trace")

0 commit comments

Comments
 (0)