diff options
author | Nick Shipp <nick@shipp.ninja> | 2017-05-07 09:04:01 -0400 |
---|---|---|
committer | Nick Shipp <nick@shipp.ninja> | 2017-05-07 09:04:01 -0400 |
commit | c012f55efda29f09179e921cf148d79deb57616e (patch) | |
tree | ff0ad37f22622d51194cab192a2aa4b0106d7ad0 /vim/bundle/slimv/swank-clojure/swank/util/io.clj | |
parent | 4ca8f6608883d230131f8a9e8b6d6c091c516049 (diff) |
Much maturering of vim configs
Diffstat (limited to 'vim/bundle/slimv/swank-clojure/swank/util/io.clj')
-rw-r--r-- | vim/bundle/slimv/swank-clojure/swank/util/io.clj | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/vim/bundle/slimv/swank-clojure/swank/util/io.clj b/vim/bundle/slimv/swank-clojure/swank/util/io.clj new file mode 100644 index 0000000..6247eec --- /dev/null +++ b/vim/bundle/slimv/swank-clojure/swank/util/io.clj @@ -0,0 +1,40 @@ +(ns swank.util.io + (:use [swank util] + [swank.util.concurrent thread]) + (:import [java.io StringWriter Reader PrintWriter])) + +(defn read-chars + ([rdr n] (read-chars rdr n false)) + ([#^Reader rdr n throw-exception] + (let [cbuf (make-array Character/TYPE n)] + (loop [i 0] + (let [size (.read rdr cbuf i (- n i))] + (cond + (neg? size) (if throw-exception + (throw throw-exception) + (String. cbuf 0 i)) + (= (+ i size) n) (String. cbuf) + :else (recur (+ i size)))))))) + +(defn call-on-flush-stream + "Creates a stream that will call a given function when flushed." + ([flushf] + (let [closed? (atom false) + #^PrintWriter stream + (PrintWriter. + (proxy [StringWriter] [] + (close [] (reset! closed? true)) + (flush [] + (let [#^StringWriter me this + len (.. me getBuffer length)] + (when (> len 0) + (flushf (.. me getBuffer (substring 0 len))) + (.. me getBuffer (delete 0 len)))))))] + (dothread + (thread-set-name "Call-on-write Stream") + (continuously + (Thread/sleep 200) + (when-not @closed? + (.flush stream)))) + stream)) + {:tag PrintWriter}) |