7

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

Deleteman
  • 8,500
  • 6
  • 25
  • 39
  • What's command? Would you consider editing your original post and putting that in? I'm trying to build your code and have a look at it. I think I know what it is, but it would be nice to see it. Thanks. – octopusgrabbus Jun 04 '12 at 15:07
  • @octopusgrabbus There, I added the missing line. – Deleteman Jun 04 '12 at 16:36
  • At the lein repl, how do you call this? I just entered (ask-for-input). Admittedly, I have my area set up like a project. I'll post the project and src header in my answer as an edit. – octopusgrabbus Jun 04 '12 at 17:24

2 Answers2

12

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.

Community
  • 1
  • 1
user922621
  • 390
  • 4
  • 10
0

I tried your source code, but omitted the flush. It worked without a problem. 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 altered one of your functions that I copied and tested with, and it works now with standalone and lein run. You had (flush) in your original example.

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

From what I can garner, println causes a flush, print doesn't, and you need a flush after print.

I am adding this information in case it might be of help. I have a Clojure project called repl-test. Here is my repl-test project's core.clj file header. Your source, already posted, is in this file with some other functions, not related to your post.

(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)
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
  • I was using 1.3 and switched to 1.4 to see if that would help, but it didn't. – Deleteman Jun 04 '12 at 16:35
  • Add my project and core.clj headers. – octopusgrabbus Jun 04 '12 at 17:28
  • Same deal, remember that on Repl it works, if I do `lein repl` and then call `(-main)` the program works correctly, but if I run it with `lein run` the read-line never returns. BTW my cored IS working correctly with the trampoline option.. do you happen to know why? – Deleteman Jun 04 '12 at 17:46
  • If you look at my post, you'll see that the `(flush)` is there, you removed it in your example, but it was there all along. – Deleteman Jun 04 '12 at 18:36
  • Got it. I put flush back, and it does work for me running as an uberjar or lein run. – octopusgrabbus Jun 04 '12 at 18:45