Skip to content

Commit 89cb462

Browse files
committed
Added silencing of the output
1 parent a91d82a commit 89cb462

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dotty.tools.repl
22

3-
import java.io.PrintStream
3+
import java.io.{PrintStream, OutputStream}
44

55
import dotty.tools.dotc.ast.Trees._
66
import dotty.tools.dotc.ast.{tpd, untpd}
@@ -54,6 +54,8 @@ case class State(objectIndex: Int,
5454
/** Main REPL instance, orchestrating input, compilation and presentation */
5555
class ReplDriver(settings: Array[String],
5656
out: PrintStream = Console.out,
57+
initialCommands: Option[String] = None,
58+
cleanupCommands: Option[String] = None,
5759
classLoader: Option[ClassLoader] = None) extends Driver {
5860

5961
/** Overridden to `false` in order to not have to give sources on the
@@ -121,13 +123,28 @@ class ReplDriver(settings: Array[String],
121123
}
122124
}
123125

126+
def readAll(rawCommands: Option[String])(initialState: State): State = {
127+
rawCommands match {
128+
case None => initialState
129+
case Some(cmds) => {
130+
val command = ParseResult(cmds)(rootCtx)
131+
interpret(command)(initialState)
132+
}
133+
}
134+
}
135+
124136
@tailrec def loop(state: State): State = {
125137
val res = readLine(state)
126138
if (res == Quit) state
127139
else loop(interpret(res)(state))
128140
}
129141

130-
try withRedirectedOutput { loop(initState) }
142+
try {
143+
// TODO: how to chain it well?
144+
val a = withSilencedOutput { readAll(initialCommands)(initState) }
145+
val b = withRedirectedOutput { loop(a) }
146+
withSilencedOutput { readAll(cleanupCommands)(b) }
147+
}
131148
finally terminal.close()
132149
}
133150

@@ -136,9 +153,20 @@ class ReplDriver(settings: Array[String],
136153
interpret(parsed)
137154
}
138155

156+
private def ignored: PrintStream = {
157+
val ignoringOutputStream = new OutputStream {
158+
def write(b: Int): Unit = {}
159+
}
160+
161+
new PrintStream(ignoringOutputStream)
162+
}
163+
139164
private def withRedirectedOutput(op: => State): State =
140165
Console.withOut(out) { Console.withErr(out) { op } }
141166

167+
private def withSilencedOutput(op: => State): State =
168+
Console.withOut(ignored) { Console.withErr(out) { op } }
169+
142170
private def newRun(state: State) = {
143171
val run = compiler.newRun(rootCtx.fresh.setReporter(newStoreReporter), state.objectIndex)
144172
state.copy(context = run.runContext)

sbt-bridge/src/xsbt/ConsoleInterface.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ class ConsoleInterface {
5151
} ++
5252
Array("-classpath", classpathString)
5353

54-
new ReplDriver(completeArgs, classLoader = Some(loader)).runUntilQuit()
54+
new ReplDriver(
55+
completeArgs,
56+
classLoader = Some(loader),
57+
initialCommands = Some(initialCommands),
58+
cleanupCommands = Some(cleanupCommands)).runUntilQuit()
5559
}
5660
}

0 commit comments

Comments
 (0)