【问题标题】:Is there a good library to embed a command prompt in a scala (or java) application是否有一个很好的库可以在 scala(或 java)应用程序中嵌入命令提示符
【发布时间】:2010-10-30 02:26:33
【问题描述】:

我有一个应用程序,我希望有提示。如果有帮助,这是一个图形数据库实现,我需要一个提示,就像任何其他数据库客户端(MySQL、Postgresql 等)一样。

到目前为止,我有自己的 REPL:

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
}

它有效,但我真的很想有一些历史。不需要制表符补全。

关于一个好的图书馆有什么建议吗? Scala 或 Java 都可以。

需要明确的是,我不需要 REPL 来评估我的代码(我通过 scala 得到了它!),我也不想从命令行调用或使用某些东西。我正在寻找在我的客户端应用启动时作为我的用户体验的提示。

【问题讨论】:

    标签: java scala console client


    【解决方案1】:

    Scala 本身以及那里的许多程序都为其 REPL 使用了类似 readline 的库。具体来说,JLine

    我找到了 another question 关于这个问题的答案,似乎没有希望。

    【讨论】:

    • 是的,一个类似 readline 的库正是我想要的。我还发现了一个名为 java-readline 的 gnu readline 的 jni 包装,但我想我将使用 JLine。谢谢!
    【解决方案2】:

    BeanShell 做一些你想做的事:http://www.beanshell.org/

    【讨论】:

      【解决方案3】:

      我明白了。这两个博客真的很有帮助。

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

      【讨论】:

        猜你喜欢
        • 2011-12-09
        • 1970-01-01
        • 2017-06-22
        • 1970-01-01
        • 1970-01-01
        • 2017-05-14
        • 2020-08-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多