[leiningen] [PATCH 1/1] pprint: add --not-pretty argument

  • From: Rob Browning <rlb@xxxxxxxxxxxxxxxx>
  • To: leiningen@xxxxxxxxxxxxx
  • Date: Sat, 23 Jun 2018 12:53:21 -0500

Add a pprint --not-pretty argument that causes the values to be output
via print instead of pprint, which is useful for cases like this:

  lein pprint --not-pretty :version > version-for-other-tools
---

 I suppose we could also support an inverse --pretty argument so that
 you have some way to require pretty printing, i.e.

   lein pprint $opts_from_somewhere --pretty ...

 lein-pprint/src/leiningen/pprint.clj | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)

diff --git a/lein-pprint/src/leiningen/pprint.clj 
b/lein-pprint/src/leiningen/pprint.clj
index a05e5438..8623e197 100644
--- a/lein-pprint/src/leiningen/pprint.clj
+++ b/lein-pprint/src/leiningen/pprint.clj
@@ -1,14 +1,30 @@
 (ns leiningen.pprint
-  (:require [clojure.pprint :as pprint]))
+  (:require [clojure.pprint :as pprint]
+            [leiningen.core :refer [abort]]))
 
 (defn ^:no-project-needed pprint
-  "Pretty-print a representation of the project map."
+  "Usage: pprint [--not-pretty] [--] [selector...]
+
+  When no selectors are specified, pretty-prints a representation of
+  the entire project map.  Otherwise pretty-prints the item(s)
+  retrieved by (get-in project selector) when reading a selector
+  produces something sequential, or (get project selector) when it
+  doesn't.  If \"--not-pretty\" is specified, doesn't pretty-print,
+  just prints."
   [project & keys]
-  (if (seq keys)
-    (doseq [kstr keys]
-      (let [k (read-string kstr)]
-        (pprint/pprint (if (sequential? k)
-                         (get-in project k)
-                         (get project k)))))
-    (pprint/pprint project))
+  (let [[pretty? keys] (loop [args args
+                              pretty? true]
+                         (if-let [[arg & args] (seq args)]
+                           (case arg
+                             "--" [pretty? args]
+                             "--no-pretty" (recur false args)
+                             (abort "Unrecognized argument" (pr-str arg)))))
+        show (if pretty? pprint/pprint println)]
+    (if (seq keys)
+      (doseq [kstr keys]
+        (let [k (read-string kstr)]
+          (show (if (sequential? k)
+                  (get-in project k)
+                  (get project k)))))
+      (show project)))
   (flush))
-- 
2.15.1


Other related posts: