conch
conch copied to clipboard
Consider auto-flushing *out* impl instead of System/out
Would this be an acceptable solution to the issue raised at https://github.com/Raynes/conch/blob/master/src/me/raynes/conch/low_level.clj#L102
I think it's a better tradeoff than System/out, since output can show up in tooling repls such as cider.
In #32 I also note my confusion around *err*
behavior in CIDER. Would your proposal address that issue?
The following, at least, didn't change behavior under CIDER for me, but maybe I have the wrong idea about implementation.
;; in (ns me.raynes.conch.low-level)
(defn auto-flush
"Returns a PrintWriter suitable for binding to *out* or *err*. This
writer will call .flush() on every write, ensuring that all output
is flushed, even if output was written from a background thread."
[writer]
(proxy [java.io.PrintWriter] [writer]
(write
([s]
(.write writer s)
(.flush writer))
([s ^Integer off ^Integer len]
(.write writer s off len)
(.flush writer)))))
(defn auto-stream-to-out
[process from & args]
(binding [*out* (auto-flush (java.io.StringWriter.))]
(apply stream-to process from *out* args)))
user> (require '[me.raynes.conch.low-level :as sh])
nil
user> (def sh-python (sh/proc "python" "-i"))
#'user/sh-python
user> (future (sh/auto-stream-to-out sh-python :out))
#future[{:status :pending, :val nil} 0x2550e75a]
user> (future (sh/auto-stream-to-out sh-python :err))
#future[{:status :pending, :val nil} 0x51c018e5]
user> (sh/feed-from-string sh-python "1+3\n")
nil
Sure, if it works it works. It does work, right?
It didn't work for me.
I think the point is to wrap whatever the thread-local value of out is, which is going to be a printwriter pointing to cider's streams, not an auto-flushing stringwriter. I can try to make a patch for this.