Skip to content

Commit dc36755

Browse files
committed
Add CLI option to disable REPL syntax highlighting
1 parent fdf2424 commit dc36755

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class ScalaSettings extends Settings.SettingGroup {
9494
val sourceReader = StringSetting("-Xsource-reader", "classname", "Specify a custom method for reading source files.", "")
9595
val XnoValueClasses = BooleanSetting("-Xno-value-classes", "Do not use value classes. Helps debugging.")
9696
val XreplLineWidth = IntSetting("-Xrepl-line-width", "Maximial number of columns per line for REPL output", 390)
97+
val XreplNoColor = BooleanSetting("-Xrepl-no-color", "Removes syntax highlighting in REPL")
9798
val XoldPatmat = BooleanSetting("-Xoldpatmat", "Use the pre-2.10 pattern matcher. Otherwise, the 'virtualizing' pattern matcher is used in 2.10.")
9899
val XnoPatmatAnalysis = BooleanSetting("-Xno-patmat-analysis", "Don't perform exhaustivity/unreachability analysis. Also, ignore @switch annotation.")
99100
val XfullLubs = BooleanSetting("-Xfull-lubs", "Retains pre 2.10 behavior of less aggressive truncation of least upper bounds.")

src/dotty/tools/dotc/repl/AmmoniteReader.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ class AmmoniteReader(val interpreter: Interpreter)(implicit ctx: Context) extend
5757
writer,
5858
allFilters,
5959
displayTransform = (buffer, cursor) => {
60-
val ansiBuffer = Ansi.Str.parse(SyntaxHighlighting(buffer))
60+
val coloredBuffer =
61+
if (!ctx.settings.XreplNoColor.value) SyntaxHighlighting(buffer)
62+
else buffer
63+
val ansiBuffer = Ansi.Str.parse(coloredBuffer)
6164
val (newBuffer, cursorOffset) = SelectionFilter.mangleBuffer(
6265
selectionFilter, ansiBuffer, cursor, Ansi.Reversed.On
6366
)

src/dotty/tools/dotc/repl/CompilingInterpreter.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,12 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
418418
try {
419419
withOutput(new ByteOutputStream) { ps =>
420420
val rawRes = valMethodRes.invoke(interpreterResultObject).toString
421-
val res = new String(SyntaxHighlighting(rawRes).toArray)
421+
val res =
422+
if (!ictx.settings.XreplNoColor.value)
423+
new String(SyntaxHighlighting(rawRes).toArray)
424+
else rawRes
422425
val prints = ps.toString("utf-8")
423-
val printList =
424-
if (prints == "") Nil
425-
else prints :: Nil
426+
val printList = if (prints != "") prints :: Nil else Nil
426427

427428
if (!delayOutput) out.print(prints)
428429

src/dotty/tools/dotc/repl/REPL.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ class REPL extends Driver {
2525

2626
lazy val config = new REPL.Config
2727

28+
override def setup(args: Array[String], rootCtx: Context): (List[String], Context) = {
29+
val (strs, ctx) = super.setup(args, rootCtx)
30+
(strs, config.context(ctx))
31+
}
32+
2833
override def newCompiler(implicit ctx: Context): Compiler =
2934
new repl.CompilingInterpreter(config.output, ctx)
3035

@@ -45,6 +50,8 @@ object REPL {
4550
val continuationPrompt = " | "
4651
val version = ".next (pre-alpha)"
4752

53+
def context(ctx: Context): Context = ctx
54+
4855
/** The default input reader */
4956
def input(in: Interpreter)(implicit ctx: Context): InteractiveReader = {
5057
val emacsShell = System.getProperty("env.emacs", "") != ""

test/test/TestREPL.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class TestREPL(script: String) extends REPL {
2020
override lazy val config = new REPL.Config {
2121
override val output = new NewLinePrintWriter(out)
2222

23+
override def context(ctx: Context) =
24+
ctx.fresh.setSetting(ctx.settings.XreplNoColor, true)
25+
2326
override def input(in: Interpreter)(implicit ctx: Context) = new InteractiveReader {
2427
val lines = script.lines
2528
def readLine(prompt: String): String = {
@@ -38,8 +41,7 @@ class TestREPL(script: String) extends REPL {
3841
out.close()
3942
val printed = out.toString
4043
val transcript = printed.drop(printed.indexOf(config.prompt))
41-
val transcriptNoColors = transcript.toString.replaceAll("\u001B\\[[;\\d]*m", "")
42-
if (transcriptNoColors != script) {
44+
if (transcript.toString != script) {
4345
println("input differs from transcript:")
4446
println(transcript)
4547
assert(false)

0 commit comments

Comments
 (0)