Back
repl (clj)
(source)function
(repl & options)
Generic, reusable, read-eval-print loop. By default, reads from *in*,
writes to *out*, and prints exception summaries to *err*. If you use the
default :read hook, *in* must either be an instance of
LineNumberingPushbackReader or duplicate its behavior of both supporting
.unread and collapsing CR, LF, and CRLF into a single \newline. Options
are sequential keyword-value pairs. Available options and their defaults:
- :init, function of no arguments, initialization hook called with
bindings for set!-able vars in place.
default: #()
- :need-prompt, function of no arguments, called before each
read-eval-print except the first, the user will be prompted if it
returns true.
default: (if (instance? LineNumberingPushbackReader *in*)
#(.atLineStart *in*)
#(identity true))
- :prompt, function of no arguments, prompts for more input.
default: repl-prompt
- :flush, function of no arguments, flushes output
default: flush
- :read, function of two arguments, reads from *in*:
- returns its first argument to request a fresh prompt
- depending on need-prompt, this may cause the repl to prompt
before reading again
- returns its second argument to request an exit from the repl
- else returns the next object read from the input stream
default: repl-read
- :eval, function of one argument, returns the evaluation of its
argument
default: eval
- :print, function of one argument, prints its argument to the output
default: prn
- :caught, function of one argument, a throwable, called when
read, eval, or print throws an exception or error
default: repl-caught
Examples
clojure
(ns clojure.test-clojure.repl.deps
(:use clojure.test)
(:require [clojure.string :as str]
[clojure.repl.deps :as deps]
[clojure.main :as main]))
;(deftest test-no-add-libs-outside-repl
; (try
; (deps/add-lib 'org.clojure/data.json {:mvn/version "2.4.0"})
; (is false "add-libs outside repl should throw")
; (catch Throwable t (str/includes? (ex-message t) "add-libs")))
;
; (with-dynamic-loader
; (binding [*repl* true]
; (is (some #{'org.clojure/data.json} (deps/add-lib 'org.clojure/data.json {:mvn/version "2.4.0"})))))
; )
clojure
(ns clojure.test-clojure.main
(:use clojure.test
[clojure.test-helper :only [platform-newlines]])
(:require [clojure.main :as main]))
(defn run-repl-and-return-err
"Run repl, swallowing stdout and returing stderr."
[in-str]
(with-err-str
(with-out-str
(with-in-str in-str
(main/repl)))))
;argh - test fragility, please fix
#_(deftest repl-exception-safety
(testing "catches and prints exception on bad equals"
(is (re-matches #"java\.lang\.NullPointerException\r?\n"
(run-repl-and-return-err
"(proxy [Object] [] (equals [o] (.toString nil)))")))))
djblue/portal
(require '[babashka.deps :as deps])
(require '[clojure.main :as main])
(main/repl)
djblue/portal
(require '[babashka.deps :as deps])
(require '[clojure.main :as main])
(main/repl)
funcool/beicon
(require '[clojure.java.shell :as shell])
(require '[rebel-readline.core]
'[rebel-readline.clojure.main]
'[rebel-readline.clojure.line-reader]
'[rebel-readline.clojure.service.local]
'[rebel-readline.cljs.service.local]
'[rebel-readline.cljs.repl])
(require '[cljs.build.api :as api]
'[cljs.repl :as repl]
'[cljs.repl.node :as node])
(defmethod task "repl:jvm"
[args]
(rebel-readline.core/with-line-reader
(rebel-readline.clojure.line-reader/create
(rebel-readline.clojure.service.local/create))
(clojure.main/repl
:prompt (fn []) ;; prompt is handled by line-reader
:read (rebel-readline.clojure.main/create-repl-read))))
(defmethod task "repl:node"
[args]
(rebel-readline.core/with-line-reader
(rebel-readline.clojure.line-reader/create
(rebel-readline.cljs.service.local/create))
(cljs.repl/repl
(node/repl-env)
:prompt (fn []) ;; prompt is handled by line-reader
:read (rebel-readline.cljs.repl/create-repl-read)
:output-dir "out"
:cache-analysis false)))
NoahTheDuke/splint
(ns user
(:require
[clj-java-decompiler.core :as decompiler]
[clojure.main :as main]
[clojure.tools.namespace.repl :as tns]
[criterium.core :as criterium]
[noahtheduke.splint.dev]
[potemkin :refer [import-vars]]
[taoensso.tufte :as tufte]))
(apply require main/repl-requires)
(import-vars
[clojure.tools.namespace.repl
refresh-all]
[clj-java-decompiler.core
decompile]
[criterium.core
quick-bench
bench]
[taoensso.tufte
p
profile])
funcool/potok
(require '[clojure.java.shell :as shell]
'[clojure.main])
(require '[rebel-readline.core]
'[rebel-readline.clojure.main]
'[rebel-readline.clojure.line-reader]
'[rebel-readline.clojure.service.local]
'[rebel-readline.cljs.service.local]
'[rebel-readline.cljs.repl])
(require '[cljs.build.api :as api]
'[cljs.repl :as repl]
'[cljs.repl.node :as node])
(defmethod task "repl:node"
[args]
(rebel-readline.core/with-line-reader
(rebel-readline.clojure.line-reader/create
(rebel-readline.cljs.service.local/create))
(cljs.repl/repl
(node/repl-env)
:prompt (fn []) ;; prompt is handled by line-reader
:read (rebel-readline.cljs.repl/create-repl-read)
:output-dir "out"
:cache-analysis false)))
(defmethod task "repl:jvm"
[args]
(rebel-readline.core/with-line-reader
(rebel-readline.clojure.line-reader/create
(rebel-readline.clojure.service.local/create))
(clojure.main/repl
:prompt (fn []) ;; prompt is handled by line-reader
:read (rebel-readline.clojure.main/create-repl-read))))