Skip to content

Commit

Permalink
Support options argument in cider.nrepl.middleware.pprint/pr
Browse files Browse the repository at this point in the history
  • Loading branch information
cichli committed Mar 2, 2019
1 parent 0840da1 commit 95f6c41
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions src/cider/nrepl/pprint.clj
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
(ns cider.nrepl.pprint
"Pretty-print related utilities.
All functions here are simple wrappers that ensure a consistent API:
* has one and two params signatures - object to print and a map of print options
* functions return the printed object as a string"
All functions here are simple wrappers compatible with the expectations of
nrepl.middleware.print/wrap-print."
{:added "0.20.0"}
(:refer-clojure :exclude [pr])
(:require
[clojure.pprint :as pp]))

(def ^:private pr-options
[:print-dup
:print-readably
:print-length
:print-level
:print-meta
:print-namespace-maps])

(defn- option->var
[option]
(resolve (symbol "clojure.core" (str "*" (name option) "*"))))

(defn- pr-bindings
[options]
(->> (select-keys options pr-options)
(into {} (keep (fn [[option value]]
(when-let [var (option->var option)]
[var value]))))))

(defn pr
"Equivalent to `clojure.core/pr`. Any options corresponding to dynamic
printing configuration vars in `clojure.core` will, if provided, be bound
accordingly (e.g. `clojure.core/*print-length*` will be used if
`:print-length` is provided)."
([value writer]
(pr value writer nil))
([value writer _]
(if *print-dup*
(print-dup value writer)
(print-method value writer))))
([value writer options]
(with-bindings (pr-bindings options)
(if *print-dup*
(print-dup value writer)
(print-method value writer)))))

(defn pprint
"A simple wrapper around `clojure.pprint/write`.
Its signature is compatible with the expectations of nREPL's wrap-print
middleware."
"A simple wrapper around `clojure.pprint/write`."
([value writer]
(pprint value writer {}))
([value writer options]
Expand Down

0 comments on commit 95f6c41

Please sign in to comment.