[#571] Use real clojure.pprint, not fipp

This commit is contained in:
Michiel Borkent 2020-09-25 12:04:31 +02:00 committed by GitHub
parent 659353a3fb
commit 706dbf6a97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 53 deletions

View file

@ -225,7 +225,7 @@ From Clojure:
`make-parents`, `output-stream`, `reader`, `resource`, `writer`
- `clojure.java.shell` aliased as `shell`
- `clojure.main`: `demunge`, `repl`, `repl-requires`
- `clojure.pprint`: `pprint` (currently backed by [fipp](https://github.com/brandonbloom/fipp)'s `fipp.edn/pprint`)
- `clojure.pprint`: `pprint`, `cl-format`
- `clojure.set` aliased as `set`
- `clojure.string` aliased as `str`
- `clojure.stacktrace`

View file

@ -16,7 +16,6 @@
org.clojure/data.csv {:mvn/version "1.0.0"},
cheshire/cheshire {:mvn/version "5.10.0"}
org.clojure/data.xml {:mvn/version "0.2.0-alpha6"}
fipp/fipp {:mvn/version "0.6.22"}
clj-commons/clj-yaml {:mvn/version "0.7.2"}
com.cognitect/transit-clj {:mvn/version "1.0.324"}
nrepl/bencode {:mvn/version "1.1.0"}

View file

@ -19,7 +19,6 @@
[borkdude/graal.locking "0.0.2"]
[org.clojure/tools.cli "1.0.194"]
[cheshire "5.10.0"]
[fipp "0.6.22"]
[nrepl/bencode "1.1.0"]
[borkdude/sci.impl.reflector "0.0.1-java11"]]
:profiles {:feature/xml {:source-paths ["feature-xml"]

View file

@ -1,49 +0,0 @@
(ns babashka.impl.clojure.pprint
{:no-doc true}
(:require [fipp.edn :as fipp]
[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
"Substitution for clojure.pprint backed by fipp.edn/pprint."
([edn]
(pprint edn @sci/out))
([edn writer]
(fipp/pprint edn {:writer writer
:width @print-right-margin})))
(defn print-table
"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
in ks. If ks are not specified, use the keys of the first item in rows."
([rows] (print-table (keys (first rows)) rows))
([ks rows]
(binding [*out* @sci/out]
(when (seq rows)
(let [widths (map
(fn [k]
(apply max (count (str k)) (map #(count (str (get % k))) rows)))
ks)
spacers (map #(apply str (repeat % "-")) widths)
fmts (map #(str "%" % "s") widths)
fmt-row (fn [leader divider trailer row]
(str leader
(apply str (interpose divider
(for [[col fmt] (map vector (map #(get row %) ks) fmts)]
(format fmt (str col)))))
trailer))]
(println)
(println (fmt-row "| " " | " " |" (zipmap ks ks)))
(println (fmt-row "|-" "-+-" "-|" (zipmap ks spacers)))
(doseq [row rows]
(println (fmt-row "| " " | " " |" row))))))))
(def pprint-namespace
{'pprint (copy-var pprint pprint-ns)
'print-table (copy-var print-table pprint-ns)
'*print-right-margin* print-right-margin})

View file

@ -0,0 +1,78 @@
(ns babashka.impl.pprint
{:no-doc true}
(:require [clojure.pprint :as pprint]
[sci.core :as sci]
[sci.impl.namespaces :refer [copy-var]]
[sci.impl.vars :as vars]))
(alter-var-root #'pprint/write-option-table
(fn [m]
(zipmap (keys m)
(map find-var (vals m)))))
(def new-table-ize
(fn [t m]
(apply hash-map
(mapcat
#(when-let [v (get t (key %))] [v (val %)])
m))))
(alter-var-root #'pprint/table-ize (constantly new-table-ize))
(alter-meta! #'pprint/write-option-table dissoc :private)
(alter-meta! #'pprint/with-pretty-writer dissoc :private)
(alter-meta! #'pprint/pretty-writer? dissoc :private)
(alter-meta! #'pprint/make-pretty-writer dissoc :private)
(def new-write
(fn [object & kw-args]
(let [options (merge {:stream true} (apply hash-map kw-args))]
(with-bindings (new-table-ize pprint/write-option-table options)
(with-bindings
(if (or (not (= pprint/*print-base* 10)) pprint/*print-radix*)
{#'pr @#'pprint/pr-with-base} {})
(let [optval (if (contains? options :stream)
(:stream options)
true)
base-writer (condp = optval
nil (java.io.StringWriter.)
true *out*
optval)]
(if pprint/*print-pretty*
(pprint/with-pretty-writer base-writer
(pprint/write-out object))
(binding [*out* base-writer]
(pr object)))
(if (nil? optval)
(.toString ^java.io.StringWriter base-writer))))))))
(alter-var-root #'pprint/write (constantly new-write))
(def pprint-ns (vars/->SciNamespace 'clojure.pprint nil))
(def print-right-margin (sci/new-dynamic-var 'print-right-margin 70 {:ns pprint-ns}))
(defn print-table
"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
in ks. If ks are not specified, use the keys of the first item in rows."
([rows] (print-table (keys (first rows)) rows))
([ks rows]
(binding [*out* @sci/out]
(pprint/print-table ks rows))))
(defn pprint
"Pretty print object to the optional output writer. If the writer is not provided,
print the object to the currently bound value of *out*."
([s]
(pprint s @sci/out))
([s writer]
(binding [pprint/*print-right-margin* @print-right-margin]
(pprint/pprint s writer))))
(def pprint-namespace
{'pprint (copy-var pprint pprint-ns)
'print-table (copy-var pprint/print-table pprint-ns)
'*print-right-margin* print-right-margin
'cl-format (copy-var pprint/cl-format pprint-ns)
})

View file

@ -11,7 +11,6 @@
[babashka.impl.clojure.java.io :refer [io-namespace]]
[babashka.impl.clojure.java.shell :refer [shell-namespace]]
[babashka.impl.clojure.main :as clojure-main :refer [demunge]]
[babashka.impl.clojure.pprint :refer [pprint-namespace]]
[babashka.impl.clojure.stacktrace :refer [stacktrace-namespace]]
[babashka.impl.clojure.zip :refer [zip-namespace]]
[babashka.impl.common :as common]
@ -21,6 +20,7 @@
[babashka.impl.error-handler :refer [error-handler]]
[babashka.impl.features :as features]
[babashka.impl.pods :as pods]
[babashka.impl.pprint :refer [pprint-namespace]]
[babashka.impl.protocols :refer [protocols-namespace]]
[babashka.impl.repl :as repl]
[babashka.impl.socket-repl :as socket-repl]