Skip to content

Respect -color:never in REPL output #5098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package dotty.tools
package dotc
package printing

import dotty.tools.dotc.core.Contexts.Context

import parsing.Tokens._
import scala.annotation.switch
import scala.collection.mutable.StringBuilder
Expand Down Expand Up @@ -45,7 +47,12 @@ object SyntaxHighlighting {
'{' :: '}' :: ')' :: '(' :: '[' :: ']' :: '=' :: ' ' :: ',' :: '.' :: '|' ::
'&' :: '\n' :: Nil

def apply(chars: Iterable[Char]): Iterable[Char] = {
def apply(chars: Iterable[Char])(implicit ctx: Context): Iterable[Char] = {
if (ctx.settings.color.value != "never") highlight(chars)
else chars
}

def highlight(chars: Iterable[Char]): Iterable[Char] = {
var prev: Char = 0
var remaining = chars.toStream
val newBuf = new StringBuilder
Expand Down
10 changes: 6 additions & 4 deletions compiler/src/dotty/tools/repl/JLineTerminal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ final class JLineTerminal extends java.io.Closeable {
.build()
private val history = new DefaultHistory

private def blue(str: String) = Console.BLUE + str + Console.RESET
private val prompt = blue("scala> ")
private val newLinePrompt = blue(" | ")
private def blue(str: String)(implicit ctx: Context) =
if (ctx.settings.color.value != "never") Console.BLUE + str + Console.RESET
else str
private def prompt(implicit ctx: Context) = blue("scala> ")
private def newLinePrompt(implicit ctx: Context) = blue(" | ")

/** Blockingly read line from `System.in`
*
Expand Down Expand Up @@ -64,7 +66,7 @@ final class JLineTerminal extends java.io.Closeable {
def close() = terminal.close()

/** Provide syntax highlighting */
private class Highlighter extends reader.Highlighter {
private class Highlighter(implicit ctx: Context) extends reader.Highlighter {
def highlight(reader: LineReader, buffer: String): AttributedString = {
val highlighted = SyntaxHighlighting(buffer).mkString
AttributedString.fromAnsi(highlighted)
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,14 @@ class ReplDriver(settings: Array[String],
case TypeOf(expr) =>
compiler.typeOf(expr)(newRun(state)).fold(
displayErrors,
res => out.println(SyntaxHighlighting(res))
res => out.println(SyntaxHighlighting(res)(state.context))
)
state

case DocOf(expr) =>
compiler.docOf(expr)(newRun(state)).fold(
displayErrors,
res => out.println(SyntaxHighlighting(res))
res => out.println(SyntaxHighlighting(res)(state.context))
)
state

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SyntaxHighlightingTests {
import SyntaxHighlighting._

private def test(source: String, expected: String): Unit = {
val highlighted = SyntaxHighlighting.apply(source)
val highlighted = SyntaxHighlighting.highlight(source)
.mkString
.replace(NoColor, ">")
.replace(CommentColor, "<C|")
Expand Down