Back
get-pretty-writer (clj)
(source)function
(get-pretty-writer writer)
Returns the java.io.Writer passed in wrapped in a pretty writer proxy, unless it's
already a pretty writer. Generally, it is unnecessary to call this function, since pprint,
write, and cl-format all call it if they need to. However if you want the state to be
preserved across calls, you will want to wrap them with this.
For example, when you want to generate column-aware output with multiple calls to cl-format,
do it like in this example:
(defn print-table [aseq column-width]
(binding [*out* (get-pretty-writer *out*)]
(doseq [row aseq]
(doseq [col row]
(cl-format true "~4D~7,vT" col column-width))
(prn))))
Now when you run:
user> (print-table (map #(vector % (* % %) (* % % %)) (range 1 11)) 8)
It prints a table of squares and cubes for the numbers from 1 to 10:
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
Examples
clojure/core.typed
(deftest cl-format-test
(is-tc-e #(cl-format nil "abc")
:requires [[clojure.pprint :refer [cl-format]]])
(is-tc-e #(cl-format nil "abc" 'a :foo "bar")
:requires [[clojure.pprint :refer [cl-format]]])
(is-tc-e #(cl-format true "abc" 'a :foo "bar")
:requires [[clojure.pprint :refer [cl-format]]])
(is-tc-e #(let [so System/out
_ (assert so)
o (java.io.OutputStreamWriter. so)]
(assert o)
(cl-format (java.io.BufferedWriter. o) "abc"))
:requires [[clojure.pprint :refer [cl-format]]])
(is-tc-err #(cl-format "abc" "abc")
:requires [[clojure.pprint :refer [cl-format]]])
(is-tc-err #(cl-format true true)
:requires [[clojure.pprint :refer [cl-format]]]))
(deftest fresh-line-test
(is-tc-e #(fresh-line)
:requires [[clojure.pprint :refer [fresh-line]]]))
(deftest get-pretty-writer-test
(is-tc-e #(let [so System/out
_ (assert so)
o (java.io.OutputStreamWriter. so)]
(assert o)
(get-pretty-writer o))
[-> java.io.Writer]
:requires [[clojure.pprint :refer [get-pretty-writer]]]))
typedclojure/typedclojure
(deftest cl-format-test
(is-tc-e #(cl-format nil "abc")
:requires [[clojure.pprint :refer [cl-format]]])
(is-tc-e #(cl-format nil "abc" 'a :foo "bar")
:requires [[clojure.pprint :refer [cl-format]]])
(is-tc-e #(cl-format true "abc" 'a :foo "bar")
:requires [[clojure.pprint :refer [cl-format]]])
(is-tc-e #(let [so System/out
_ (assert so)
o (java.io.OutputStreamWriter. so)]
(assert o)
(cl-format (java.io.BufferedWriter. o) "abc"))
:requires [[clojure.pprint :refer [cl-format]]])
(is-tc-err #(cl-format "abc" "abc")
:requires [[clojure.pprint :refer [cl-format]]])
(is-tc-err #(cl-format true true)
:requires [[clojure.pprint :refer [cl-format]]]))
(deftest fresh-line-test
(is-tc-e #(fresh-line)
:requires [[clojure.pprint :refer [fresh-line]]]))
(deftest get-pretty-writer-test
(is-tc-e #(let [so System/out
_ (assert so)
o (java.io.OutputStreamWriter. so)]
(assert o)
(get-pretty-writer o))
[-> java.io.Writer]
:requires [[clojure.pprint :refer [get-pretty-writer]]]))