Skip to content

Instantly share code, notes, and snippets.

@cgrand
Created September 25, 2009 13:56
Show Gist options
  • Save cgrand/193550 to your computer and use it in GitHub Desktop.
Save cgrand/193550 to your computer and use it in GitHub Desktop.
(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