Skip to content

Commit 658194a

Browse files
vspinubbatsov
authored andcommitted
[Fix #561] Don't auto-flush out printer
1 parent 5fb8ca8 commit 658194a

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

src/cider/nrepl/middleware/out.clj

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
(:require
1313
[cider.nrepl.middleware.util.error-handling :refer [with-safe-transport]])
1414
(:import
15-
[java.io PrintWriter Writer PrintStream OutputStream]))
15+
[java.io PrintWriter Writer PrintStream OutputStream]
16+
[java.util TimerTask Timer]))
1617

1718
(if (find-ns 'clojure.tools.nrepl)
1819
(require
@@ -73,26 +74,31 @@
7374
bindings in the sessions of messages, in addition to the server's
7475
usual PrintWriter (saved in `original-output`).
7576
76-
`type` is either :out or :err."
77-
[type]
78-
(let [printer (case type
79-
:out 'clojure.core/*out*
80-
:err 'clojure.core/*err*)]
81-
(PrintStream. (proxy [OutputStream] []
82-
(close [] (.flush ^OutputStream this))
83-
(write
84-
([int-or-bytes]
85-
(.write @(resolve printer)
86-
(if (instance? Integer int-or-bytes)
87-
int-or-bytes
88-
(String. int-or-bytes))))
89-
([bytes ^Integer off ^Integer len]
90-
(let [byte-range (byte-array
91-
(take len (drop off bytes)))]
92-
(.write @(resolve printer) (String. byte-range)))))
93-
(flush []
94-
(.flush @(resolve printer))))
95-
true)))
77+
`printer` is the printer var, either #'clojure.core/*out* or
78+
#'clojure.core/*err*."
79+
[printer]
80+
(let [delay 100
81+
print-timer (Timer.)
82+
print-flusher (proxy [TimerTask] []
83+
(run []
84+
(.flush ^PrintWriter @printer)))]
85+
(.scheduleAtFixedRate print-timer print-flusher delay delay)
86+
(PrintStream.
87+
(proxy [OutputStream] []
88+
(close []
89+
(.cancel print-flusher)
90+
(.cancel print-timer)
91+
(.flush ^OutputStream this))
92+
(write
93+
([int-or-bytes]
94+
(if (instance? Integer int-or-bytes)
95+
(.write ^PrintWriter @printer ^Integer int-or-bytes)
96+
(.write ^PrintWriter @printer (String. ^"[B" int-or-bytes))))
97+
([^"[B" bytes ^Integer off ^Integer len]
98+
(let [byte-range (byte-array (take len (drop off bytes)))]
99+
(.write ^PrintWriter @printer (String. byte-range)))))
100+
(flush []
101+
(.flush ^PrintWriter @printer))))))
96102

97103
;;; Known eval sessions
98104
(def tracked-sessions-map
@@ -105,8 +111,8 @@
105111
err-writer (forking-printer (vals new-state) :err)]
106112
(alter-var-root #'*out* (constantly out-writer))
107113
(alter-var-root #'*err* (constantly err-writer))
108-
(System/setOut (print-stream :out))
109-
(System/setErr (print-stream :err))))
114+
(System/setOut (print-stream #'*out*))
115+
(System/setErr (print-stream #'*err*))))
110116

111117
(add-watch tracked-sessions-map :update-out tracked-sessions-map-watch)
112118

0 commit comments

Comments
 (0)