What I am trying to do is create a program to act as an intercessor between the REPL (lets say the scala repl) and the user. In other words, the user should type commands that instead of going directly to the REPL for execution, they are passed to the program, and then the program, after doing some editing gives it back to the REPL to execute, show the answer and so on.
What I have gotten so far is a simple interface using scala.tools.jline.console.ConsoleReader to show a prompt, get the user input and show the results after calling get_edited_data
I also know that calling "scala".!< does the job quite nicely on its own. What I can't wrap my mind around is how to connect them. I have already tried using ProcessIO with no luck.
import java.io.PrintWriter
import scala.sys.process._
import scala.tools.jline.console.ConsoleReader
object Interfere {
def main(args: Array[String]) {
val repl = "scala"
val console = new ConsoleReader
val prompt = "prompt> "
console.setPrompt(prompt)
val out = new PrintWriter(
console.getTerminal.wrapOutIfNeeded(System.out)
)
var line = console.readLine
while (!line.equals("bye")) {
val res = get_edited_data(line)
out.println(s"The answer: $res")
out.flush()
line = console.readLine
}
}
}
What I did with ProcessIO was something like below. But it does not make sense because it is not possible to give the current line as input, and generally have access to what is given to it.
Process(repl).run(
new ProcessIO(
stdin => line,
stdout => out,
stderr => out
)
)
答案 0 :(得分:0)
我最近遇到过类似的问题。后来我使用java.lang.Process来处理输入和输出。
更好的选择是使用scala.tools.nsc.interpreter.ILoop或scala.tools.nsc.interpreter.IMain来构建您的REPL而不是创建其他流程。
here是ILoop的一个例子。