Skip to content

Instantly share code, notes, and snippets.

@uwo
Last active July 4, 2018 22:20
Show Gist options
  • Save uwo/c5ca5aa57e156364dd687cb0538836c6 to your computer and use it in GitHub Desktop.
Save uwo/c5ca5aa57e156364dd687cb0538836c6 to your computer and use it in GitHub Desktop.
pformat & pspit
.nrepl-port
.cpcache

deps.edn

{:deps {gist-uwo/pformat {:git/url "https://gist.github.com/uwo/d702e3ceabd783ea7787afecdb0d470c"
                          :sha "9fe4cc9714865ac49adca56e73467ea8252bd4a5"}}}
{:paths ["."]
:deps
{org.clojure/clojure {:mvn/version "1.9.0"}}}
(ns pformat
(:require [clojure.spec.alpha :as s]))
(def defaults
{:print-right-margin 72
:print-namespace-maps false
:pprint-dispatch clojure.pprint/simple-dispatch})
(defn pformat
([data] (pformat data defaults))
([data opts]
(let [{:keys [print-right-margin
pprint-dispatch
print-namespace-maps]}
(merge defaults opts)]
(clojure.pprint/with-pprint-dispatch pprint-dispatch
(with-out-str
(binding [clojure.pprint/*print-right-margin* print-right-margin
*print-namespace-maps* print-namespace-maps]
(clojure.pprint/pprint data)))))))
(defn pspit
([filename data] (spit filename (pformat data)))
([filename data opts] (spit filename (pformat data opts))))
(s/def ::opts
(s/keys :opt-un [::print-right-margin
::print-namespace-maps
::pprint-dispatch]))
(s/fdef pformat
:args (s/cat :data any?
:opts (s/? ::opts)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment