Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mslinn/afda334db4a8d7d882ef89794ec3ec72 to your computer and use it in GitHub Desktop.
Save mslinn/afda334db4a8d7d882ef89794ec3ec72 to your computer and use it in GitHub Desktop.
Setting up a "Walk through pre-loaded example snippets every time you press spacebar" experience in the Ammonite REPL
lihaoyi Ammonite$ amm
Loading...
Welcome to the Ammonite Repl 0.8.0
(Scala 2.11.8 Java 1.8.0_112)
@ {
val funnyFilter = ammonite.terminal.Filter.simple(" ")((b, c, m) =>
if (FunnyFrontEnd.nextLine == Nil) (b.take(c) ++ " " ++ b.drop(c), c+1)
else {
val head :: tail = FunnyFrontEnd.nextLine
FunnyFrontEnd.nextReader = new java.io.StringReader(head)
FunnyFrontEnd.nextLine = tail
(b, c)
}
)
object FunnyFrontEnd extends ammonite.repl.AmmoniteFrontEnd(funnyFilter){
var nextLine = List.empty[String]
var nextReader = new java.io.StringReader("")
def setNextLines(s: Seq[String]) = nextLine = s.toList
override def readLine(reader: java.io.Reader,
output: java.io.OutputStream,
prompt: String,
colors: ammonite.util.Colors,
compilerComplete: (Int, String) => (Int, Seq[String], Seq[String]),
history: IndexedSeq[String]) = {
val readerX = new java.io.Reader{
def close() = {reader.close(); nextReader.close()}
def read(cbuf: Array[Char], off: Int, len: Int) = {
val preRead = nextReader.read(cbuf, off, len)
if(preRead == len) preRead
else{
val preReadNum = math.max(0, preRead)
val postRead = reader.read(cbuf, off + preReadNum, len - preReadNum)
if (postRead == -1) -1 else postRead + preReadNum
}
}
}
super.readLine(readerX, output, prompt, colors, compilerComplete, history)
}
}
repl.frontEnd() = FunnyFrontEnd
}
funnyFilter: AnyRef with ammonite.terminal.Filter{def op(ti: ammonite.terminal.TermInfo): Option[ammonite.terminal.TermState]} = $sess.cmd0.funnyFilter:1
defined object FunnyFrontEnd
@ FunnyFrontEnd.setNextLines(Seq("println(\"Hello\")", "println(\"World\")"))
@ println("Hello")
Hello
@ println("World")
World
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment