diff options
| author | Joel Martin <github@martintribe.org> | 2014-03-24 16:32:24 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-03-24 16:32:24 -0500 |
| commit | 3169070063b2cb877200117ebb384269d73bcb93 (patch) | |
| tree | 23de3db1ea5c37afd21a45b6ed7771f56a08c0c4 /clojure/src/readline.clj | |
| download | mal-3169070063b2cb877200117ebb384269d73bcb93.tar.gz mal-3169070063b2cb877200117ebb384269d73bcb93.zip | |
Current state of mal for Clojure West lighting talk.
Diffstat (limited to 'clojure/src/readline.clj')
| -rw-r--r-- | clojure/src/readline.clj | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/clojure/src/readline.clj b/clojure/src/readline.clj new file mode 100644 index 0000000..dbd4872 --- /dev/null +++ b/clojure/src/readline.clj @@ -0,0 +1,36 @@ +(ns readline + (:require [clojure.string :refer [split]] + [net.n01se.clojure-jna :as jna])) + +(defonce history-loaded (atom nil)) +(def HISTORY-FILE "/home/joelm/.mal-history") + +;; +;; Uncomment one of the following readline libraries +;; + +;; editline (BSD) +#_ +(do + (def readline-call (jna/to-fn String edit/readline)) + (def add-history (jna/to-fn Void edit/add_history)) + (def load-history #(doseq [line (split (slurp %) #"\n")] + (jna/invoke Void edit/add_history line)))) + +;; GNU Readline (GPL) +;; WARNING: distributing your code with GNU readline enabled means you +;; must release your program as GPL +;#_ +(do + (def readline-call (jna/to-fn String readline/readline)) + (def add-history (jna/to-fn Void readline/add_history)) + (def load-history (jna/to-fn Integer readline/read_history))) + +(defn readline [prompt & [lib]] + (if (not @history-loaded) + (load-history HISTORY-FILE)) + (let [line (readline-call prompt)] + (when line + (add-history line) + (spit HISTORY-FILE (str line "\n") :append true)) + line)) |
