Why doesn't the read-line return after pressing ENTER (it seems to hang) using lein run, but does it work with lein repl?

The problem is that when I run my program using lein run , it falls into the (read-line) , and I cannot get out of it, that is: 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 line "User input:" on the screen. The strange part is, if I ran lein repl and called (ask-for-input) , then it works correctly: S

+7
source share
2 answers

Try to launch the lein diving board , it works.

The following are frequently asked questions about leiningen:

Q: I do not have access to stdin inside my project.

A: This is a limitation of JVM processing methods; none of them expose stdin correctly. This means that functions such as read-line will not work as expected in most contexts, although the answer task must include a workaround. You can also use the trampoline task to start the JVM project after Leiningen exits, rather than starting it as a subprocess.

+12
source

I tried your source code but missed the flash. It worked without problems. What version of Clojure are you using? I tried the following code with Clojure 1.3.

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

Edit: I changed one of your functions, which I copied and tested, and now it works autonomously and works with lein. In the original example, you had (flash).

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

From what I can get, println causes a flash, printing does not work, and you need to make a flash after printing. A.

I am adding this information if it can be useful. I have a Clojure project called repl-test. Here is my header for the core.clj file of the repl-test project. Your source, already submitted, is in this file with some other functions not related to your message.

 (ns repl-test.core (:gen-class) (:use clojure.contrib.command-line) (:require [clojure.contrib.string :as cstr]) (:require [clojure.contrib.trace :as ctr]) (:require [clojure.string :as sstr]) (:use clojure-csv.core)) 

And here is the project.clj file:

 (defproject repl-test "0.0.1-SNAPSHOT" :description "TODO: add summary of your project" :dependencies [[org.clojure/clojure "1.3.0"] [org.clojure/clojure-contrib "1.2.0"] [clojure-csv/clojure-csv "1.2.4"] [org.clojure/tools.cli "0.1.0"] [clj-http "0.1.3"]] :aot [repl-test.core] :main repl-test.core) 
0
source

All Articles