diff options
author | Som Snytt <som.snytt@gmail.com> | 2015-06-22 20:56:40 -0700 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2015-06-23 15:52:01 -0700 |
commit | 934a31488b43b75a5b437e0cb293b6b5b4f076d7 (patch) | |
tree | 0e5e85a6125fb56eff158b633495f643a4abc200 /src/repl | |
parent | 1b9cb466430b6300355185917257493ca2e6a240 (diff) | |
download | scala-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.
Diffstat (limited to 'src/repl')
4 files changed, 37 insertions, 17 deletions
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 |