conch icon indicating copy to clipboard operation
conch copied to clipboard

Consider auto-flushing *out* impl instead of System/out

Open gtrak opened this issue 8 years ago • 5 comments

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.

gtrak avatar Aug 05 '16 16:08 gtrak

In #32 I also note my confusion around *err* behavior in CIDER. Would your proposal address that issue?

holtzermann17 avatar Aug 24 '16 11:08 holtzermann17

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

holtzermann17 avatar Aug 24 '16 16:08 holtzermann17

Sure, if it works it works. It does work, right?

Raynes avatar Oct 26 '16 03:10 Raynes

It didn't work for me.

holtzermann17 avatar Oct 26 '16 18:10 holtzermann17

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.

gtrak avatar Oct 26 '16 18:10 gtrak