0

Is there a way to use pprint instead of regular print in clojure sublimeREPL

For the moment i had to wrap my code like that to do so:

(clojure.pprint/pprint (for [i (range 10)] {i {:times2 (* i 2) :times3 (* i 3)}}))
=>    ({0 {:times2 0, :times3 0}}
       {1 {:times2 2, :times3 3}}
       {2 {:times2 4, :times3 6}}
       {3 {:times2 6, :times3 9}}
       {4 {:times2 8, :times3 12}}
       {5 {:times2 10, :times3 15}}
       {6 {:times2 12, :times3 18}}
       {7 {:times2 14, :times3 21}}
       {8 {:times2 16, :times3 24}}
       {9 {:times2 18, :times3 27}})

sorry for the dummy example

MattDMo
  • 100,794
  • 21
  • 241
  • 231
szymanowski
  • 1,359
  • 1
  • 14
  • 25

1 Answers1

2

You can do it with simple nrepl middleware, for example:

(defn pprint-middleware [h]
  (fn [{:keys [code op] :as msg}]
    (if (and (= op "eval") (not (empty? code)))
      (->> #(str "(clojure.pprint/pprint " % ")")
           (update-in msg [:code])
           h)
      (h msg))))

The easiest way to add it to your repl is to config repl-options in youe project.clj:

:repl-options {:nrepl-middleware [nrepl.utils/pprint-middleware]})

Here is the full project.clj example:

(defproject sample "0.1.0"
  :dependencies [[org.clojure/clojure "1.4.0"]]
  :repl-options {:nrepl-middleware [sample.utils/pprint-middleware]})
Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
  • silly question: where do i place this function in my project? – szymanowski Dec 17 '13 at 16:33
  • i've tried to place pprint-middleware in my "utils" namespace, then i've modified :repl-options {:nrepl-middleware [pprint-middleware]} to :repl-options {:nrepl-middleware [utils/pprint-middleware]} but it i've got an error when launch the repl – szymanowski Dec 17 '13 at 16:36
  • Try updating your `lein` with `lein upgrade`. Old versions have no support for `repl-options`. – Leonid Beschastny Dec 17 '13 at 17:47
  • it doesn't seems to change anything, here's the error message: CompilerException java.lang.ClassNotFoundException: reply.exports, compiling:(/private/var/folders/3w/l39tm8z56cs2ypk389nv_7lh0000gn/T/form-init5267732350145682329.clj:1:8446) Error loading namespace; falling back to user NullPointerException clojure.pprint/column-writer/fn--7358 (column_writer.clj:78) – szymanowski Dec 17 '13 at 18:02