Created
September 25, 2009 13:56
-
-
Save cgrand/193550 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'clojure.main) | |
(defn file-repl [filename] | |
(let [log (java.io.FileWriter. filename) | |
logout (fn [#^java.io.Writer out] | |
(proxy [java.io.Writer] [] | |
(write | |
([x] | |
(.write log x) | |
(.write out x)) | |
([cbuf off len] | |
(.write log cbuf off len) | |
(.write out cbuf off len))) | |
(flush [] | |
(.flush log) | |
(.flush out)) | |
(close [] | |
(.close log) | |
(.close out)))) | |
login (fn [#^java.io.Reader in] | |
(proxy [java.io.Reader] [] | |
(read | |
([] | |
(let [c (.read in)] | |
(when (> -1 c) | |
(.write log (char c))) | |
c)) | |
([cbuf] | |
(let [n (.read in cbuf)] | |
(when (pos? n) | |
(.write log (if (instance? java.nio.CharBuffer cbuf) (.array cbuf) cbuf))) | |
n)) | |
([cbuf off len] | |
(let [n (.read in cbuf off len)] | |
(when (pos? n) | |
(.write log cbuf off n)) | |
n))) | |
(close [] | |
(.close log) | |
(.close in))))] | |
(binding [*in* (clojure.lang.LineNumberingPushbackReader. (login *in*)) | |
*out* (logout *out*) | |
*err* (logout *err*)] | |
(clojure.main/repl)))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment