summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSom Snytt <som.snytt@gmail.com>2015-06-22 20:56:40 -0700
committerSom Snytt <som.snytt@gmail.com>2015-06-23 15:52:01 -0700
commit934a31488b43b75a5b437e0cb293b6b5b4f076d7 (patch)
tree0e5e85a6125fb56eff158b633495f643a4abc200
parent1b9cb466430b6300355185917257493ca2e6a240 (diff)
downloadscala-934a31488b43b75a5b437e0cb293b6b5b4f076d7.tar.gz
scala-934a31488b43b75a5b437e0cb293b6b5b4f076d7.tar.bz2
scala-934a31488b43b75a5b437e0cb293b6b5b4f076d7.zip
SI-9206: REPL custom welcome message
Can be specified by `-Dscala.repl.welcome=Greeting` or in properties file. It takes the same format arguments as the prompt, viz, version, Java version and JVM name. It can be disabled by `-Dscala.repl.welcome` with no text.
-rw-r--r--src/compiler/scala/tools/nsc/Properties.scala9
-rw-r--r--src/library/scala/sys/BooleanProp.scala1
-rw-r--r--src/library/scala/sys/Prop.scala4
-rw-r--r--src/repl/scala/tools/nsc/interpreter/Formatting.scala3
-rw-r--r--src/repl/scala/tools/nsc/interpreter/ILoop.scala14
-rw-r--r--src/repl/scala/tools/nsc/interpreter/IMain.scala4
-rw-r--r--src/repl/scala/tools/nsc/interpreter/ReplProps.scala33
7 files changed, 51 insertions, 17 deletions
diff --git a/src/compiler/scala/tools/nsc/Properties.scala b/src/compiler/scala/tools/nsc/Properties.scala
index ca7d8776d4..c8b9922623 100644
--- a/src/compiler/scala/tools/nsc/Properties.scala
+++ b/src/compiler/scala/tools/nsc/Properties.scala
@@ -12,8 +12,17 @@ object Properties extends scala.util.PropertiesTrait {
protected def pickJarBasedOn = classOf[Global]
// settings based on jar properties, falling back to System prefixed by "scala."
+
+ // messages to display at startup or prompt, format string with string parameters
+ // Scala version, Java version, JVM name
def residentPromptString = scalaPropOrElse("resident.prompt", "\nnsc> ")
def shellPromptString = scalaPropOrElse("shell.prompt", "%nscala> ")
+ def shellWelcomeString = scalaPropOrElse("shell.welcome",
+ """Welcome to Scala %1$s (%3$s, Java %2$s).
+ |Type in expressions to have them evaluated.
+ |Type :help for more information.""".stripMargin
+ )
+
// message to display at EOF (which by default ends with
// a newline so as not to break the user's terminal)
def shellInterruptedString = scalaPropOrElse("shell.interrupted", f":quit$lineSeparator")
diff --git a/src/library/scala/sys/BooleanProp.scala b/src/library/scala/sys/BooleanProp.scala
index e5e4668edb..b0008b41fd 100644
--- a/src/library/scala/sys/BooleanProp.scala
+++ b/src/library/scala/sys/BooleanProp.scala
@@ -50,6 +50,7 @@ object BooleanProp {
def get: String = "" + value
val clear, enable, disable, toggle = ()
def option = if (isSet) Some(value) else None
+ //def or[T1 >: Boolean](alt: => T1): T1 = if (value) true else alt
protected def zero = false
}
diff --git a/src/library/scala/sys/Prop.scala b/src/library/scala/sys/Prop.scala
index 17ae8cb69c..52a3d89ecb 100644
--- a/src/library/scala/sys/Prop.scala
+++ b/src/library/scala/sys/Prop.scala
@@ -58,6 +58,10 @@ trait Prop[+T] {
*/
def option: Option[T]
+ // Do not open until 2.12.
+ //** This value if the property is set, an alternative value otherwise. */
+ //def or[T1 >: T](alt: => T1): T1
+
/** Removes the property from the underlying map.
*/
def clear(): Unit
diff --git a/src/repl/scala/tools/nsc/interpreter/Formatting.scala b/src/repl/scala/tools/nsc/interpreter/Formatting.scala
index 844997429c..4a9548730a 100644
--- a/src/repl/scala/tools/nsc/interpreter/Formatting.scala
+++ b/src/repl/scala/tools/nsc/interpreter/Formatting.scala
@@ -30,3 +30,6 @@ class Formatting(indent: Int) {
}
)
}
+object Formatting {
+ def forPrompt(prompt: String) = new Formatting(prompt.lines.toList.last.length)
+}
diff --git a/src/repl/scala/tools/nsc/interpreter/ILoop.scala b/src/repl/scala/tools/nsc/interpreter/ILoop.scala
index b95789a16d..7262aab425 100644
--- a/src/repl/scala/tools/nsc/interpreter/ILoop.scala
+++ b/src/repl/scala/tools/nsc/interpreter/ILoop.scala
@@ -56,13 +56,9 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
private var globalFuture: Future[Boolean] = _
- /** Print a welcome message */
- def printWelcome() {
- echo(s"""
- |Welcome to Scala $versionString ($javaVmName, Java $javaVersion).
- |Type in expressions to have them evaluated.
- |Type :help for more information.""".trim.stripMargin
- )
+ /** Print a welcome message! */
+ def printWelcome(): Unit = {
+ Option(replProps.welcome) filter (!_.isEmpty) foreach echo
replinfo("[info] started at " + new java.util.Date)
}
@@ -111,10 +107,6 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
}
class ILoopInterpreter extends IMain(settings, out) {
- // the expanded prompt but without color escapes and without leading newline, for purposes of indenting
- override lazy val formatting: Formatting = new Formatting(
- (replProps.promptString format Properties.versionNumberString).lines.toList.last.length
- )
override protected def parentClassLoader =
settings.explicitParentLoader.getOrElse( classOf[ILoop].getClassLoader )
}
diff --git a/src/repl/scala/tools/nsc/interpreter/IMain.scala b/src/repl/scala/tools/nsc/interpreter/IMain.scala
index 2550a5dc57..841b4abfa5 100644
--- a/src/repl/scala/tools/nsc/interpreter/IMain.scala
+++ b/src/repl/scala/tools/nsc/interpreter/IMain.scala
@@ -113,9 +113,7 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set
def this() = this(new Settings())
// the expanded prompt but without color escapes and without leading newline, for purposes of indenting
- lazy val formatting: Formatting = new Formatting(
- (replProps.promptString format Properties.versionNumberString).lines.toList.last.length
- )
+ lazy val formatting = Formatting.forPrompt(replProps.promptText)
lazy val reporter: ReplReporter = new ReplReporter(this)
import formatting.indentCode
diff --git a/src/repl/scala/tools/nsc/interpreter/ReplProps.scala b/src/repl/scala/tools/nsc/interpreter/ReplProps.scala
index df65e9974d..8fac040a5e 100644
--- a/src/repl/scala/tools/nsc/interpreter/ReplProps.scala
+++ b/src/repl/scala/tools/nsc/interpreter/ReplProps.scala
@@ -6,9 +6,11 @@
package scala.tools.nsc
package interpreter
-import Properties.shellPromptString
+import Properties.{ javaVersion, javaVmName, shellPromptString, shellWelcomeString,
+ versionString, versionNumberString }
import scala.sys._
import Prop._
+import java.util.{ Formattable, FormattableFlags, Formatter }
class ReplProps {
private def bool(name: String) = BooleanProp.keyExists(name)
@@ -22,12 +24,37 @@ class ReplProps {
val trace = bool("scala.repl.trace")
val power = bool("scala.repl.power")
+ def enversion(s: String) = {
+ import FormattableFlags._
+ val v = new Formattable {
+ override def formatTo(formatter: Formatter, flags: Int, width: Int, precision: Int) = {
+ val version = if ((flags & ALTERNATE) != 0) versionNumberString else versionString
+ val left = if ((flags & LEFT_JUSTIFY) != 0) "-" else ""
+ val w = if (width >= 0) s"$width" else ""
+ val p = if (precision >= 0) s".$precision" else ""
+ val fmt = s"%${left}${w}${p}s"
+ formatter.format(fmt, version)
+ }
+ }
+ s.format(v, javaVersion, javaVmName)
+ }
+ def encolor(s: String) = {
+ import scala.io.AnsiColor.{ MAGENTA, RESET }
+ if (colorOk) s"$MAGENTA$s$RESET" else s
+ }
+
// Handy system prop for shell prompt, or else pick it up from compiler.properties
val promptString = Prop[String]("scala.repl.prompt").option getOrElse (if (info) "%nscala %s> " else shellPromptString)
+ def promptText = enversion(promptString)
val prompt = {
import scala.io.AnsiColor.{ MAGENTA, RESET }
- val p = promptString format Properties.versionNumberString
- if (colorOk) s"$MAGENTA$p$RESET" else p
+ if (colorOk) s"$MAGENTA$promptText$RESET" else promptText
+ }
+
+ //def welcome = enversion(Prop[String]("scala.repl.welcome") or shellWelcomeString)
+ def welcome = enversion {
+ val p = Prop[String]("scala.repl.welcome")
+ if (p.isSet) p.value else shellWelcomeString
}
/** CSV of paged,across to enable pagination or `-x` style