Permalink
Browse files

Remove some jline-related duplication

  • Loading branch information...
1 parent 8e167d0 commit 6dc54e214e15cc661b1d35fdfaa65ed0095ba791 @trptcolin committed Apr 7, 2013
Showing with 16 additions and 67 deletions.
  1. +2 −3 src/clj/reply/eval_modes/standalone.clj
  2. +7 −61 src/clj/reply/reader/jline.clj
  3. +7 −3 src/clj/reply/reader/simple_jline.clj
@@ -4,6 +4,7 @@
[reply.exit :as exit]
[reply.initialization :as initialization]
[reply.reader.jline :as jline]
+ [reply.reader.simple-jline :as simple-jline]
[reply.signals :as signals]))
(def reply-read
@@ -12,9 +13,7 @@
(binding [*ns* (eval-state/get-ns)]
(let [result (jline/read prompt exit)]
(when-let [reader @jline/jline-reader]
- (.clear (.getCursorBuffer reader))
- (.restore (.getTerminal reader))
- (.shutdown reader)
+ (simple-jline/shutdown reader)
(reset! jline/jline-reader nil)
(reset! jline/jline-pushback-reader nil))
result))))
@@ -2,6 +2,7 @@
(:refer-clojure :exclude [read])
(:require [reply.reader.jline.completion :as jline.completion]
[reply.eval-state :as eval-state]
+ [reply.reader.simple-jline :as simple-jline]
[clojure.main])
(:import [java.io File IOException PrintStream ByteArrayOutputStream
FileInputStream FileDescriptor]
@@ -14,41 +15,6 @@
(def jline-reader (atom nil))
(def jline-pushback-reader (atom nil))
-(defn print-interruption []
- (when-not (#{"none" "off" "false"}
- (.getProperty (Configuration/getProperties) "jline.terminal"))
- (print "^C")
- (flush)))
-
-(defn- make-history-file [history-path]
- (if history-path
- (let [history-file (File. history-path)]
- (if (.getParentFile history-file)
- history-file
- (File. "." history-path)))
- (File. (System/getProperty "user.home")
- ".jline-reply.history")))
-
-(defn make-reader [{:keys [input-stream output-stream history-file]
- :as options}]
- (when (= "dumb" (System/getenv "TERM"))
- (.setProperty (Configuration/getProperties) "jline.terminal" "none"))
- (let [reader (ConsoleReader. (or input-stream
- (FileInputStream. FileDescriptor/in))
- (or output-stream System/out))
- history (FileHistory. (make-history-file history-file))
- completer (jline.completion/make-completer
- reply.initialization/eval-in-user-ns
- #()
- (str (ns-name *ns*)))]
- (.setBlinkMatchingParen (.getKeys reader) true)
- (.setHandleUserInterrupt reader true)
- (doto reader
- (.setHistory history)
- (.setExpandEvents false)
- (.setPaginationEnabled true)
- (.addCompleter completer))))
-
(def prompt-end "=> ")
(defmulti get-prompt type)
@@ -75,7 +41,7 @@
(when-not (System/getenv "JLINE_LOGGING")
(Log/setOutput (PrintStream. (ByteArrayOutputStream.))))
; since construction is side-effect-y
- (reset! jline-reader (make-reader options))
+ (reset! jline-reader (simple-jline/setup-console-reader options))
; since this depends on jline-reader
(reset! jline-pushback-reader
(CustomizableBufferLineNumberingPushbackReader.
@@ -84,38 +50,18 @@
:set-empty-prompt set-empty-prompt})
1)))
-(defn reset-reader []
- (when @jline-reader
- (-> (.getCursorBuffer @jline-reader)
- (.clear))))
-
-(defn resume-reader []
- (when @jline-reader
- (.reset (.getTerminal @jline-reader))
- (.redrawLine @jline-reader)
- (.flush @jline-reader)))
-
-(defn shutdown-reader []
- (when @jline-reader
- (.restore (.getTerminal @jline-reader))))
-
(defn prepare-for-read [eval-fn ns]
- (when-not @jline-reader (setup-reader! {}))
- (.flush (.getHistory @jline-reader))
+ (simple-jline/prepare-for-next-read {:reader @jline-reader})
(.setPrompt @jline-reader (@prompt-fn ns))
(eval-state/set-ns ns)
- (.removeCompleter @jline-reader (first (.getCompleters @jline-reader)))
(.addCompleter @jline-reader
- (jline.completion/make-completer
- eval-fn
- (fn []
- (.redrawLine @jline-reader)
- (.flush @jline-reader))
- (str (ns-name ns)))))
+ ((simple-jline/make-completer (str (ns-name ns)) eval-fn)
+ @jline-reader)))
(defmacro with-jline-in [& body]
`(do
(try
+ (setup-reader! {})
(prepare-for-read reply.initialization/eval-in-user-ns
(eval-state/get-ns))
(binding [*in* @jline-pushback-reader]
@@ -125,7 +71,7 @@
(catch Throwable e#
(if (#{IOException InterruptedException}
(type (clojure.main/repl-exception e#)))
- (do (reset-reader) nil)
+ (do (simple-jline/reset-reader @jline-reader) nil)
(throw e#))))))
(defn read [request-prompt request-exit]
@@ -16,9 +16,14 @@
(File. "." history-path)))
(File. (System/getProperty "user.home") ".jline-reply.history")))
+
+(defn reset-reader [reader]
+ (when reader
+ (.clear (.getCursorBuffer reader))))
+
(defn shutdown [{:keys [reader] :as state}]
(when reader
- (.clear (.getCursorBuffer reader))
+ (reset-reader reader)
(.restore (.getTerminal reader))
(.shutdown reader)))
@@ -35,7 +40,7 @@
(when-not (System/getenv "JLINE_LOGGING")
(Log/setOutput (PrintStream. (null-output-stream)))))
-(defn- prepare-for-next-read [{:keys [reader] :as state}]
+(defn prepare-for-next-read [{:keys [reader] :as state}]
(.flush (.getHistory reader))
(.removeCompleter reader (first (.getCompleters reader))))
@@ -49,7 +54,6 @@
:as state}]
(let [reader (ConsoleReader. input-stream output-stream)
history (FileHistory. (make-history-file history-file))
- ;; TODO: rip out this default completer, make it a no-op
completer (if completer-factory
(completer-factory reader)
nil)]

0 comments on commit 6dc54e2

Please sign in to comment.