Skip to content

Commit

Permalink
Merge pull request #450 from trepidacious/silent-repl
Browse files Browse the repository at this point in the history
Allow silencing of REPL output from Jupyter API
  • Loading branch information
sbrunk authored Oct 15, 2019
2 parents 601093e + 9f1a0f7 commit d5dfccb
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ abstract class JupyterApi { api =>

def stdinOpt(prompt: String = "", password: Boolean = false): Option[String]

def silent(s: Boolean): Unit = {}
def silent: Boolean = false

protected implicit def changingPublish: OutputHandler =
new almond.interpreter.api.OutputHandler.OnlyUpdateVia(commHandler)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ final class Execute(
storage: Storage,
logCtx: LoggerContext,
updateBackgroundVariablesEcOpt: Option[ExecutionContext],
commHandlerOpt: => Option[CommHandler]
commHandlerOpt: => Option[CommHandler],
silent: Ref[Boolean]
) {

private val log = logCtx(getClass)
Expand Down Expand Up @@ -223,7 +224,7 @@ final class Execute(
resultOutput.clear()
resultVariables.clear()
log.debug(s"Compiling / evaluating $code ($stmts)")
val r = ammInterp.processLine(code, stmts, currentLine0, silent = false, incrementLine = () => currentLine0 += 1)
val r = ammInterp.processLine(code, stmts, currentLine0, silent = silent(), incrementLine = () => currentLine0 += 1)

log.debug(s"Handling output of $code")
Repl.handleOutput(ammInterp, r)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.nio.charset.StandardCharsets
import almond.api.{FullJupyterApi, JupyterApi}
import almond.internals.HtmlAnsiOutputStream
import almond.interpreter.api.CommHandler
import ammonite.util.Ref
import pprint.{TPrint, TPrintColors}

import scala.concurrent.Await
Expand All @@ -16,7 +17,8 @@ import scala.reflect.ClassTag
final class JupyterApiImpl(
execute: Execute,
commHandlerOpt: => Option[CommHandler],
replApi: ReplApiImpl
replApi: ReplApiImpl,
silent0: Ref[Boolean]
) extends FullJupyterApi {

protected def printOnChange[T](
Expand All @@ -34,6 +36,9 @@ final class JupyterApiImpl(
replApi.Internal.print(value, ident, custom)(tprint, tcolors, classTagT)
}

override def silent(s: Boolean): Unit = silent0.update(s)
override def silent: Boolean = silent0.apply()

protected def ansiTextToHtml(text: String): String = {
val baos = new ByteArrayOutputStream
val haos = new HtmlAnsiOutputStream(baos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ final class ScalaInterpreter(

private val colors0: Ref[Colors] = Ref(params.initialColors)

private val silent0: Ref[Boolean] = Ref(false)

private var commHandlerOpt = Option.empty[CommHandler]

private val storage =
Expand All @@ -51,7 +53,8 @@ final class ScalaInterpreter(
storage,
logCtx,
params.updateBackgroundVariablesEcOpt,
commHandlerOpt
commHandlerOpt,
silent0
)


Expand All @@ -69,7 +72,7 @@ final class ScalaInterpreter(
)

val jupyterApi =
new JupyterApiImpl(execute0, commHandlerOpt, replApi)
new JupyterApiImpl(execute0, commHandlerOpt, replApi, silent0)

for (ec <- params.updateBackgroundVariablesEcOpt)
UpdatableFuture.setup(replApi, jupyterApi, ec)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object ScalaInterpreterTests extends TestSuite {

private val sbv = scala.util.Properties.versionNumberString.split('.').take(2).mkString(".")

private val interpreter: Interpreter =
private def newInterpreter: Interpreter =
new ScalaInterpreter(
params = ScalaInterpreterParams(
initialColors = Colors.BlackWhite,
Expand All @@ -31,6 +31,8 @@ object ScalaInterpreterTests extends TestSuite {
logCtx = logCtx
)

private val interpreter: Interpreter = newInterpreter

private object Predef {
private def predefPath(name: String): Path =
Paths.get(getClass.getResource(s"/test-predefs/$name.sc").toURI)
Expand Down Expand Up @@ -237,6 +239,103 @@ object ScalaInterpreterTests extends TestSuite {
"exception" - Predef.exception(fileBased = true)
}

"silent" - {
"defaults false" - {
val code = "val silent = kernel.silent"
val res = newInterpreter.execute(code)
val expectedRes = ExecuteResult.Success(DisplayData.text("silent: Boolean = false"))
assert(res == expectedRes)
}
"can be set to true" - {
val code =
"""
| val silentBefore = kernel.silent
| kernel.silent(true)
| val silentAfter = kernel.silent
|""".stripMargin
val res = newInterpreter.execute(code)
val expectedRes = ExecuteResult.Success(DisplayData.text(
"""silentBefore: Boolean = false
|silentAfter: Boolean = true""".stripMargin))
assert(res == expectedRes)
}
"can be set to false" - {
val code =
"""
| kernel.silent(true)
| val silentBefore = kernel.silent
| kernel.silent(false)
| val silentAfter = kernel.silent
|""".stripMargin
val res = newInterpreter.execute(code)
val expectedRes = ExecuteResult.Success(DisplayData.text(
"""silentBefore: Boolean = true
|silentAfter: Boolean = false""".stripMargin))
assert(res == expectedRes)
}
"affects subsequent calls to execute when enabled" - {
val code0 =
"""
| kernel.silent(true)
| val noEffectInSameExecute = kernel.silent
|""".stripMargin
val code1 =
"""
| val effectInNextExecute = 0
|""".stripMargin
val code2 =
"""
| val effectInNextExecuteAgain = 0
|""".stripMargin
val i = newInterpreter
val res0 = i.execute(code0)
val res1 = i.execute(code1)
val res2 = i.execute(code2)
val expectedRes0 = ExecuteResult.Success(DisplayData.text(
"noEffectInSameExecute: Boolean = true"))
val expectedRes1 = ExecuteResult.Success(DisplayData.empty)
val expectedRes2 = ExecuteResult.Success(DisplayData.empty)
assert(res0 == expectedRes0)
assert(res1 == expectedRes1)
assert(res2 == expectedRes2)
}

"affects subsequent calls to execute when disabled" - {
val code0 = "kernel.silent(true)"
val code1 =
"""
| kernel.silent(false)
| val noEffectInSameExecute = kernel.silent
|""".stripMargin
val code2 =
"""
| val effectInNextExecute = kernel.silent
|""".stripMargin
val code3 =
"""
| val effectInNextExecuteAgain = kernel.silent
|""".stripMargin

val i = newInterpreter
val res0 = i.execute(code0)
val res1 = i.execute(code1)
val res2 = i.execute(code2)
val res3 = i.execute(code3)

val expectedRes0 = ExecuteResult.Success(DisplayData.empty)
val expectedRes1 = ExecuteResult.Success(DisplayData.empty)
val expectedRes2 = ExecuteResult.Success(DisplayData.text(
"effectInNextExecute: Boolean = false"))
val expectedRes3 = ExecuteResult.Success(DisplayData.text(
"effectInNextExecuteAgain: Boolean = false"))

assert(res0 == expectedRes0)
assert(res1 == expectedRes1)
assert(res2 == expectedRes2)
assert(res3 == expectedRes3)
}
}

"dependencies" - {
"auto dependency" - {
"example" - {
Expand Down

0 comments on commit d5dfccb

Please sign in to comment.