[#405] add clojure.pprint/*print-right-margin*
This commit is contained in:
parent
b7525c1616
commit
67c9a34e7a
2 changed files with 26 additions and 10 deletions
|
|
@ -1,20 +1,27 @@
|
||||||
(ns babashka.impl.clojure.pprint
|
(ns babashka.impl.clojure.pprint
|
||||||
{:no-doc true}
|
{:no-doc true}
|
||||||
(:require [fipp.edn :as fipp]
|
(:require [fipp.edn :as fipp]
|
||||||
[sci.core :as sci]))
|
[sci.core :as sci]
|
||||||
|
[sci.impl.namespaces :refer [copy-var]]
|
||||||
|
[sci.impl.vars :as vars]))
|
||||||
|
|
||||||
|
(def pprint-ns (vars/->SciNamespace 'clojure.pprint nil))
|
||||||
|
|
||||||
|
(def print-right-margin (sci/new-dynamic-var 'print-right-margin 70 {:ns pprint-ns}))
|
||||||
|
|
||||||
(defn pprint
|
(defn pprint
|
||||||
|
"Substitution for clojure.pprint backed by fipp.edn/pprint."
|
||||||
([edn]
|
([edn]
|
||||||
(binding [*out* @sci/out]
|
(pprint edn @sci/out))
|
||||||
(fipp/pprint edn)))
|
|
||||||
([edn writer]
|
([edn writer]
|
||||||
(fipp/pprint edn {:writer writer})))
|
(fipp/pprint edn {:writer writer
|
||||||
|
:width @print-right-margin})))
|
||||||
|
|
||||||
(defn print-table
|
(defn print-table
|
||||||
"Prints a collection of maps in a textual table. Prints table headings
|
"Prints a collection of maps in a textual table. Prints table headings
|
||||||
ks, and then a line of output for each row, corresponding to the keys
|
ks, and then a line of output for each row, corresponding to the keys
|
||||||
in ks. If ks are not specified, use the keys of the first item in rows."
|
in ks. If ks are not specified, use the keys of the first item in rows."
|
||||||
{:added "1.3"}
|
([rows] (print-table (keys (first rows)) rows))
|
||||||
([ks rows]
|
([ks rows]
|
||||||
(binding [*out* @sci/out]
|
(binding [*out* @sci/out]
|
||||||
(when (seq rows)
|
(when (seq rows)
|
||||||
|
|
@ -34,9 +41,9 @@
|
||||||
(println (fmt-row "| " " | " " |" (zipmap ks ks)))
|
(println (fmt-row "| " " | " " |" (zipmap ks ks)))
|
||||||
(println (fmt-row "|-" "-+-" "-|" (zipmap ks spacers)))
|
(println (fmt-row "|-" "-+-" "-|" (zipmap ks spacers)))
|
||||||
(doseq [row rows]
|
(doseq [row rows]
|
||||||
(println (fmt-row "| " " | " " |" row)))))))
|
(println (fmt-row "| " " | " " |" row))))))))
|
||||||
([rows] (print-table (keys (first rows)) rows)))
|
|
||||||
|
|
||||||
(def pprint-namespace
|
(def pprint-namespace
|
||||||
{'pprint pprint
|
{'pprint (copy-var pprint pprint-ns)
|
||||||
'print-table print-table})
|
'print-table (copy-var print-table pprint-ns)
|
||||||
|
'*print-right-margin* print-right-margin})
|
||||||
|
|
|
||||||
|
|
@ -411,7 +411,16 @@
|
||||||
|
|
||||||
(deftest pprint-test
|
(deftest pprint-test
|
||||||
(testing "writer"
|
(testing "writer"
|
||||||
(is (string? (bb nil "(let [sw (java.io.StringWriter.)] (clojure.pprint/pprint (range 10) sw) (str sw))")))))
|
(is (string? (bb nil "(let [sw (java.io.StringWriter.)] (clojure.pprint/pprint (range 10) sw) (str sw))"))))
|
||||||
|
(testing "*print-right-margin*"
|
||||||
|
(is (= "(0\n 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9)\n" (bb nil "
|
||||||
|
(let [sw (java.io.StringWriter.)]
|
||||||
|
(binding [clojure.pprint/*print-right-margin* 5]
|
||||||
|
(clojure.pprint/pprint (range 10) sw)) (str sw))")))
|
||||||
|
(is (= "(0 1 2 3 4 5 6 7 8 9)\n" (bb nil "
|
||||||
|
(let [sw (java.io.StringWriter.)]
|
||||||
|
(binding [clojure.pprint/*print-right-margin* 50]
|
||||||
|
(clojure.pprint/pprint (range 10) sw)) (str sw))")))))
|
||||||
|
|
||||||
(deftest read-string-test
|
(deftest read-string-test
|
||||||
(testing "namespaced keyword via alias"
|
(testing "namespaced keyword via alias"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue