|
12 | 12 | (:require
|
13 | 13 | [cider.nrepl.middleware.util.error-handling :refer [with-safe-transport]])
|
14 | 14 | (:import
|
15 |
| - [java.io PrintWriter Writer PrintStream OutputStream])) |
| 15 | + [java.io PrintWriter Writer PrintStream OutputStream] |
| 16 | + [java.util TimerTask Timer])) |
16 | 17 |
|
17 | 18 | (if (find-ns 'clojure.tools.nrepl)
|
18 | 19 | (require
|
|
73 | 74 | bindings in the sessions of messages, in addition to the server's
|
74 | 75 | usual PrintWriter (saved in `original-output`).
|
75 | 76 |
|
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)))))) |
96 | 102 |
|
97 | 103 | ;;; Known eval sessions
|
98 | 104 | (def tracked-sessions-map
|
|
105 | 111 | err-writer (forking-printer (vals new-state) :err)]
|
106 | 112 | (alter-var-root #'*out* (constantly out-writer))
|
107 | 113 | (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*)))) |
110 | 116 |
|
111 | 117 | (add-watch tracked-sessions-map :update-out tracked-sessions-map-watch)
|
112 | 118 |
|
|
0 commit comments