Skip to content

Commit 934a314

Browse files
committed
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.
1 parent 1b9cb46 commit 934a314

File tree

7 files changed

+51
-17
lines changed

7 files changed

+51
-17
lines changed

src/compiler/scala/tools/nsc/Properties.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@ object Properties extends scala.util.PropertiesTrait {
1212
protected def pickJarBasedOn = classOf[Global]
1313

1414
// settings based on jar properties, falling back to System prefixed by "scala."
15+
16+
// messages to display at startup or prompt, format string with string parameters
17+
// Scala version, Java version, JVM name
1518
def residentPromptString = scalaPropOrElse("resident.prompt", "\nnsc> ")
1619
def shellPromptString = scalaPropOrElse("shell.prompt", "%nscala> ")
20+
def shellWelcomeString = scalaPropOrElse("shell.welcome",
21+
"""Welcome to Scala %1$s (%3$s, Java %2$s).
22+
|Type in expressions to have them evaluated.
23+
|Type :help for more information.""".stripMargin
24+
)
25+
1726
// message to display at EOF (which by default ends with
1827
// a newline so as not to break the user's terminal)
1928
def shellInterruptedString = scalaPropOrElse("shell.interrupted", f":quit$lineSeparator")

src/library/scala/sys/BooleanProp.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ object BooleanProp {
5050
def get: String = "" + value
5151
val clear, enable, disable, toggle = ()
5252
def option = if (isSet) Some(value) else None
53+
//def or[T1 >: Boolean](alt: => T1): T1 = if (value) true else alt
5354

5455
protected def zero = false
5556
}

src/library/scala/sys/Prop.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ trait Prop[+T] {
5858
*/
5959
def option: Option[T]
6060

61+
// Do not open until 2.12.
62+
//** This value if the property is set, an alternative value otherwise. */
63+
//def or[T1 >: T](alt: => T1): T1
64+
6165
/** Removes the property from the underlying map.
6266
*/
6367
def clear(): Unit

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ class Formatting(indent: Int) {
3030
}
3131
)
3232
}
33+
object Formatting {
34+
def forPrompt(prompt: String) = new Formatting(prompt.lines.toList.last.length)
35+
}

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,9 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
5656

5757
private var globalFuture: Future[Boolean] = _
5858

59-
/** Print a welcome message */
60-
def printWelcome() {
61-
echo(s"""
62-
|Welcome to Scala $versionString ($javaVmName, Java $javaVersion).
63-
|Type in expressions to have them evaluated.
64-
|Type :help for more information.""".trim.stripMargin
65-
)
59+
/** Print a welcome message! */
60+
def printWelcome(): Unit = {
61+
Option(replProps.welcome) filter (!_.isEmpty) foreach echo
6662
replinfo("[info] started at " + new java.util.Date)
6763
}
6864

@@ -111,10 +107,6 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
111107
}
112108

113109
class ILoopInterpreter extends IMain(settings, out) {
114-
// the expanded prompt but without color escapes and without leading newline, for purposes of indenting
115-
override lazy val formatting: Formatting = new Formatting(
116-
(replProps.promptString format Properties.versionNumberString).lines.toList.last.length
117-
)
118110
override protected def parentClassLoader =
119111
settings.explicitParentLoader.getOrElse( classOf[ILoop].getClassLoader )
120112
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set
113113
def this() = this(new Settings())
114114

115115
// the expanded prompt but without color escapes and without leading newline, for purposes of indenting
116-
lazy val formatting: Formatting = new Formatting(
117-
(replProps.promptString format Properties.versionNumberString).lines.toList.last.length
118-
)
116+
lazy val formatting = Formatting.forPrompt(replProps.promptText)
119117
lazy val reporter: ReplReporter = new ReplReporter(this)
120118

121119
import formatting.indentCode

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
package scala.tools.nsc
77
package interpreter
88

9-
import Properties.shellPromptString
9+
import Properties.{ javaVersion, javaVmName, shellPromptString, shellWelcomeString,
10+
versionString, versionNumberString }
1011
import scala.sys._
1112
import Prop._
13+
import java.util.{ Formattable, FormattableFlags, Formatter }
1214

1315
class ReplProps {
1416
private def bool(name: String) = BooleanProp.keyExists(name)
@@ -22,12 +24,37 @@ class ReplProps {
2224
val trace = bool("scala.repl.trace")
2325
val power = bool("scala.repl.power")
2426

27+
def enversion(s: String) = {
28+
import FormattableFlags._
29+
val v = new Formattable {
30+
override def formatTo(formatter: Formatter, flags: Int, width: Int, precision: Int) = {
31+
val version = if ((flags & ALTERNATE) != 0) versionNumberString else versionString
32+
val left = if ((flags & LEFT_JUSTIFY) != 0) "-" else ""
33+
val w = if (width >= 0) s"$width" else ""
34+
val p = if (precision >= 0) s".$precision" else ""
35+
val fmt = s"%${left}${w}${p}s"
36+
formatter.format(fmt, version)
37+
}
38+
}
39+
s.format(v, javaVersion, javaVmName)
40+
}
41+
def encolor(s: String) = {
42+
import scala.io.AnsiColor.{ MAGENTA, RESET }
43+
if (colorOk) s"$MAGENTA$s$RESET" else s
44+
}
45+
2546
// Handy system prop for shell prompt, or else pick it up from compiler.properties
2647
val promptString = Prop[String]("scala.repl.prompt").option getOrElse (if (info) "%nscala %s> " else shellPromptString)
48+
def promptText = enversion(promptString)
2749
val prompt = {
2850
import scala.io.AnsiColor.{ MAGENTA, RESET }
29-
val p = promptString format Properties.versionNumberString
30-
if (colorOk) s"$MAGENTA$p$RESET" else p
51+
if (colorOk) s"$MAGENTA$promptText$RESET" else promptText
52+
}
53+
54+
//def welcome = enversion(Prop[String]("scala.repl.welcome") or shellWelcomeString)
55+
def welcome = enversion {
56+
val p = Prop[String]("scala.repl.welcome")
57+
if (p.isSet) p.value else shellWelcomeString
3158
}
3259

3360
/** CSV of paged,across to enable pagination or `-x` style

0 commit comments

Comments
 (0)