开发者

Is there a good library to embed a command prompt in a scala (or java) application

开发者 https://www.devze.com 2023-01-22 09:57 出处:网络
I have an application that I\'d like to have a prompt in.If it helps, this is a graph database implementation and I need a prompt just like any other database client (MySQL, Postgresql, etc.).

I have an application that I'd like to have a prompt in. If it helps, this is a graph database implementation and I need a prompt just like any other database client (MySQL, Postgresql, etc.).

So far I have my own REPL like so:

object App extends Application {
    REPL ! Read
}

object REPL extends Actor {
    def act() {
        loop {
            react {
                case Read => {
                    print("prompt> ")
                    var message = Console.readLine
                    this ! Eval(message)
                }
                case More(sofar) => {
                    //Eval didn't see a semicolon
                    print("    --> ")
                    var message = Console.readLine
                    this ! Eval(sofar + " " + message)
                }
                case Eval(message) => {
                    Evaluator ! Eval(message)
                }
                case Print(message) => {
                    println(message)
                    //And here's the loop
                    this ! Read
                }
  开发者_开发技巧              case Exit => {
                    exit()
                }
                case _ => {
                    println("App: How did we get here")
                }
            }
        }
    }
    this.start
}

It works, but I would really like to have something with history. Tab completion is not necessary.

Any suggestions on a good library? Scala or Java works.

Just to be clear I don't need an REPL to evaluate my code (I get that with scala!), nor am I looking to call or use something from the command line. I'm looking for a prompt that is my user experience when my client app starts up.


Scala itself, and lots of programs out there, uses a readline-like library for its REPL. Specifically, JLine.

I found another question about this, for which the answers don't seem promising.


BeanShell does some of what you want: http://www.beanshell.org/


I got it. these two blogs really helped.

http://danielwestheide.com/blog/2013/01/09/the-neophytes-guide-to-scala-part-8-welcome-to-the-future.html

http://danielwestheide.com/blog/2013/01/16/the-neophytes-guide-to-scala-part-9-promises-and-futures-in-practice.html

def interprete(code: String) : Future[String] = {
val p = Promise[String]()

Future {
  var result = reader.readLine()
  p.success(result)
}

writer.write(code + "\n")
writer.flush()

p.future

}

for (ln <- io.Source.stdin.getLines){
  val f = interprete(ln)
  f.onComplete {
    case Success(s) =>
      println("future returned: " + s)
    case Failure(ex) =>
      println(s"interpreter failed due to ${ex.getMessage}")
  }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号