Skip to content

Commit

Permalink
Generalize out.clj code to support err
Browse files Browse the repository at this point in the history
Related to #1588
  • Loading branch information
Malabarba committed Feb 29, 2016
1 parent e2b25b2 commit 96a650e
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/cider/nrepl/middleware/out.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,43 @@

(defmacro with-out-binding
"Run body with v bound to the output stream of each msg in msg-seq.
Also run body with v bound to `original-out`."
[[v msg-seq] & body]
`(do (let [~(with-meta v {:tag Writer}) original-out]
Also run body with v bound to `original-out`.
type is either :out or :err."
[[v msg-seq type] & body]
`(do (let [~(with-meta v {:tag Writer}) (case ~type
:out #'original-out
:err #'original-err)]
~@body)
(doseq [{:keys [~'session] :as ~'msg} ~msg-seq]
(let [~(with-meta v {:tag Writer}) (get @~'session #'*out*)]
(let [~(with-meta v {:tag Writer}) (get @~'session
(case ~type
:out #'*out*
:err #'*err*))]
(try (binding [ie/*msg* ~'msg]
~@body)
;; If a channel is faulty, dissoc it.
(catch Exception ~'e
(unsubscribe-session ~'session)))))))

(defn fork-out
(defn forking-printer
"Returns a PrintWriter suitable for binding as *out* or *err*. All
operations are forwarded to all output bindings in the sessions of
messages in addition to the server's usual PrintWriter (saved in
`original-out`)."
[messages]
`original-out` or `original-err`).
type is either :out or :err."
[messages type]
(PrintWriter. (proxy [Writer] []
(close [] (.flush ^Writer this))
(write
([x]
(with-out-binding [out messages]
(.write out x)))
(with-out-binding [printer messages type]
(.write printer x)))
([x ^Integer off ^Integer len]
(with-out-binding [out messages]
(.write out x off len))))
(with-out-binding [printer messages type]
(.write printer x off len))))
(flush []
(with-out-binding [out messages]
(.flush out))))
(with-out-binding [printer messages type]
(.flush printer))))
true))

;;; Known eval sessions
Expand Down

0 comments on commit 96a650e

Please sign in to comment.