Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gtrak opened this issue Aug 5, 2016 · 5 comments
Open

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

gtrak opened this issue Aug 5, 2016 · 5 comments
Assignees

Comments

@gtrak
Copy link

gtrak commented Aug 5, 2016

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.

@holtzermann17
Copy link
Contributor

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

@holtzermann17
Copy link
Contributor

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

@Raynes
Copy link
Owner

Raynes commented Oct 26, 2016

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

@holtzermann17
Copy link
Contributor

It didn't work for me.

@gtrak
Copy link
Author

gtrak commented Oct 26, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants