Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does read-line not return after hitting ENTER (seems like a hang) using lein run, but works with lein repl?

The problem at hand is that when I run my program with lein run it gets to the (read-line) part and I can't get out of it, meaning: read-line never returns.

Here is the relevant code:

(def command (atom ""))

(defn print-prompt []
  (print "prompt> ")
  (flush)
)

(defn ask-for-input []
    (print-prompt)
    (let [x (str (read-line))]
      (println (str "User input: " x))
      (reset! command x)
    )
)

I never see the "User input: " string on screen. The strange part is, if I run lein repl and call (ask-for-input) then it works correctly :S

like image 994
Deleteman Avatar asked Jun 04 '12 14:06

Deleteman


1 Answers

Try lein trampoline run, it works.

The following is from leiningen FAQ:

Q: I don't have access to stdin inside my project.

A: This is a limitation of the JVM's process-handling methods; none of them expose stdin correctly. This means that functions like read-line will not work as expected in most contexts, though the repl task necessarily includes a workaround. You can also use the trampoline task to launch your project's JVM after Leiningen's has exited rather than launching it as a subprocess.

like image 121
user922621 Avatar answered Dec 21 '22 11:12

user922621