3

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.

Jason S
  • 184,598
  • 164
  • 608
  • 970
bdaniels
  • 233
  • 2
  • 9

3 Answers3

6

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.

Community
  • 1
  • 1
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • Yes a readline-like library is exactly what I was looking for. I also found a jni wrap of gnu readline called java-readline, but I think I will be using JLine. Thanks! – bdaniels Oct 30 '10 at 15:43
2

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

0

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}")
  }

}
bhomass
  • 3,414
  • 8
  • 45
  • 75