Skip to content

Commit a5db35d

Browse files
committed
Factor out coloring check to new method Context#useColors
1 parent 799007f commit a5db35d

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

src/dotty/tools/dotc/core/Contexts.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ object Contexts {
370370
/** Is the verbose option set? */
371371
def verbose: Boolean = base.settings.verbose.value
372372

373+
/** Should use colors when printing? */
374+
def useColors: Boolean =
375+
List("auto", "always") contains base.settings.color.value
376+
373377
/** A condensed context containing essential information of this but
374378
* no outer contexts except the initial context.
375379
private var _condensed: CondensedContext = null

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ class AmmoniteReader(val interpreter: Interpreter)(implicit ctx: Context) extend
5858
allFilters,
5959
displayTransform = (buffer, cursor) => {
6060
val coloredBuffer =
61-
if (List("auto", "always").contains(ctx.settings.color.value))
62-
SyntaxHighlighting(buffer)
61+
if (ctx.useColors) SyntaxHighlighting(buffer)
6362
else buffer
6463

6564
val ansiBuffer = Ansi.Str.parse(coloredBuffer)

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
406406
}
407407

408408
/** load and run the code using reflection.
409-
* @return A pair consisting of the run's result as a string, and
409+
* @return A pair consisting of the run's result as a `List[String]`, and
410410
* a boolean indicating whether the run succeeded without throwing
411411
* an exception.
412412
*/
@@ -417,12 +417,11 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
417417
interpreterResultObject.getMethod("result")
418418
try {
419419
withOutput(new ByteOutputStream) { ps =>
420-
val rawRes = valMethodRes.invoke(interpreterResultObject).toString
421-
val res =
422-
if (List("auto", "always").contains(ictx.settings.color.value))
423-
new String(SyntaxHighlighting(rawRes).toArray)
420+
val rawRes = valMethodRes.invoke(interpreterResultObject).toString
421+
val res =
422+
if (ictx.useColors) new String(SyntaxHighlighting(rawRes).toArray)
424423
else rawRes
425-
val prints = ps.toString("utf-8")
424+
val prints = ps.toString("utf-8")
426425
val printList = if (prints != "") prints :: Nil else Nil
427426

428427
if (!delayOutput) out.print(prints)

0 commit comments

Comments
 (0)