Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Color REPL #63

Merged
merged 2 commits into from
Sep 24, 2014
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
9 changes: 8 additions & 1 deletion src/repl/scala/tools/nsc/interpreter/ILoop.scala
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,14 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
}

private def readOneLine() = {
import scala.io.AnsiColor.{ MAGENTA, RESET }
out.flush()
in readLine prompt
in readLine (
if (in.colorsOk)
MAGENTA + prompt + RESET
else
prompt
)
}

/** The main read-eval-print loop for the repl. It calls
Expand Down Expand Up @@ -857,6 +863,7 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
case x: JLineReader => x.consoleReader.postInit
case _ =>
}
intp.colorsOk = in.colorsOk
}
def process(settings: Settings): Boolean = savingContextLoader {
this.settings = settings
Expand Down
1 change: 1 addition & 0 deletions src/repl/scala/tools/nsc/interpreter/IMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set
// Used in a test case.
def showDirectory() = replOutput.show(out)

private[nsc] var colorsOk = false // whether we should use colors in results
private[nsc] var printResults = true // whether to print result lines
private[nsc] var totalSilence = false // whether to print anything
private var _initializeComplete = false // compiler is initialized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ trait InteractiveReader {
def history: History
def completion: Completion
def redrawLine(): Unit
def colorsOk: Boolean

def readYesOrNo(prompt: String, alt: => Boolean): Boolean = readOneKey(prompt) match {
case 'y' => true
Expand Down
2 changes: 2 additions & 0 deletions src/repl/scala/tools/nsc/interpreter/JLineReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class JLineReader(_completion: => Completion) extends InteractiveReader {
private def term = consoleReader.getTerminal()
def reset() = term.reset()

lazy val colorsOk = term.isAnsiSupported

def scalaToJline(tc: ScalaCompleter): Completer = new Completer {
def complete(_buf: String, cursor: Int, candidates: JList[CharSequence]): Int = {
val buf = if (_buf == null) "" else _buf
Expand Down
10 changes: 9 additions & 1 deletion src/repl/scala/tools/nsc/interpreter/MemberHandlers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,15 @@ trait MemberHandlers {
if (replProps.vids) s"""" + f"@$${System.identityHashCode($path)}%8x" + """"
else ""

""" + "%s%s: %s = " + %s""".format(string2code(prettyName), vidString, string2code(req typeOf name), resultString)
import scala.io.AnsiColor.{ BOLD, BLUE, GREEN, RESET }

def boldColor(c: String, s: String) =
if (intp.colorsOk) string2code(BOLD) + string2code(c) + s + string2code(RESET)
else s

val nameString = boldColor(BLUE, string2code(prettyName)) + vidString
val typeString = boldColor(GREEN, string2code(req typeOf name))
s""" + "$nameString: $typeString = " + $resultString"""
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/repl/scala/tools/nsc/interpreter/SimpleReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extends InteractiveReader
{
val history = NoHistory
val completion = NoCompletion
val colorsOk = false

def reset() = ()
def redrawLine() = ()
Expand Down