           .readLine
           clojure.string/trim-newline))
 
-(defn println-console [console & args]
+(defn cprint [console & args]
+  (binding [*out* (:out console)]
+    (apply print args)
+    (.flush *out*)))
+
+(defn cprintln [console & args]
   (binding [*out* (:out console)]
     (apply println args)))
 

  (let [{:keys [repl console]} system
        {:keys [error etype result]} (eval-in-repl repl line)]
    (if error
      (do (println-console console "ETYPE:" etype)
          (println-console console "ERROR:" error))
      (println-console console result))))

(defmulti run-line (fn [system line] line))

(defmethod run-line "%b" [system line]
  (let [console (:console system)]
    (loop [buffer [line]]
      (print-err "buffer" buffer)
      (if (= (last buffer) "%r")
        (let [command (->> buffer
                           drop-first-and-last
                           (clojure.string/join "\n"))]
          (remove-n-from-history console (count buffer))
          (add-to-history console command)
          (eval-and-print system command))
        (recur (->> (read-console-line console)
                    (conj buffer)))))))

(defmethod run-line "exit" [system _]
  (component/stop system)

(defn new-repl [command]
  (map->Repl {:command command}))

(defn format-response [response]
  (clojure.set/rename-keys response {"result" :result
                                     "error" :error
