aboutsummaryrefslogtreecommitdiff
path: root/clojure/src/readline.clj
blob: a510e13b57f269fe7e9b82303313a14ec898734d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(ns readline
    (:require [clojure.string :refer [split]]
              [net.n01se.clojure-jna :as jna]))

(defonce history-loaded (atom nil))
(def HISTORY-FILE (str (System/getProperty "user.home") "/.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]]
  (when (not @history-loaded)
    (reset! history-loaded true)
    (load-history HISTORY-FILE))
  (let [line (readline-call prompt)]
    (when line
      (add-history line)
      (spit HISTORY-FILE (str line "\n") :append true))
    line))