diff --git a/CHANGELOG.md b/CHANGELOG.md index 015965b..07f2d31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ [jet](https://github.com/borkdude/jet): CLI to transform between JSON, EDN, YAML and Transit using Clojure. +## Unreleased + +- [#151](https://github.com/borkdude/jet/issues/151): `--print-width` option to limit width of pretty printed EDN + ## 0.7.27 (2023-08-02) - [#137](https://github.com/borkdude/jet/issues/137): Added missing functions from clojure v1.11: diff --git a/src/jet/formats.clj b/src/jet/formats.clj index 4b8b08d..da2d57d 100644 --- a/src/jet/formats.clj +++ b/src/jet/formats.clj @@ -44,10 +44,12 @@ (and (= :auto colors) (in-terminal?)))) -(defn pprint [x colors uncomma] +(defn pprint [x colors {:keys [uncomma print-width]}] (if colors - (puget/cprint x {:map-delimiter (if uncomma "" ",")}) - (fipp/pprint x))) + (puget/cprint x (cond-> {:map-delimiter (if uncomma "" ",")} + print-width (assoc :width print-width))) + (fipp/pprint x (cond-> {} + print-width (assoc :width print-width))))) (defn json-parser [] (.createParser ^JsonFactory (or factory/*json-factory* @@ -73,12 +75,15 @@ (recur next)))))) str)) -(defn generate-edn [o pretty color uncomma] - (let [edn-str (if pretty (str/trim (with-out-str (pprint o color uncomma))) - (pr-str o))] - (if (and uncomma (not color)) - (uncomma-edn edn-str) - edn-str))) +(defn generate-edn + ([o pretty color uncomma] (generate-edn o pretty color uncomma {})) + ([o pretty color uncomma {:keys [print-width]}] + (let [edn-str (if pretty (str/trim (with-out-str (pprint o color {:uncomma uncomma + :print-width print-width}))) + (pr-str o))] + (if (and uncomma (not color)) + (uncomma-edn edn-str) + edn-str)))) (defn transit-reader [] (transit/reader (ReaderInputStream. *in*) :json)) diff --git a/src/jet/main.clj b/src/jet/main.clj index 7b3c42e..dabc7fc 100644 --- a/src/jet/main.clj +++ b/src/jet/main.clj @@ -152,7 +152,9 @@ :edn-reader-opts {:desc "options passed to the EDN reader." :default "{:default tagged-literal}"} :no-commas {:coerce :boolean - :desc "remove commas from EDN"}}) + :desc "remove commas from EDN"} + :print-width {:coerce :long + :desc "Max print width, when pretty-printing EDN"}}) (def cli-opts {:spec cli-spec @@ -185,7 +187,7 @@ no-pretty version query func thread-first thread-last interactive collect edn-reader-opts - help colors no-commas] + help colors no-commas print-width] :or {from :edn to :edn colors :auto}}] @@ -225,7 +227,7 @@ (case to :edn (some-> input - (formats/generate-edn (not no-pretty) colors no-commas) + (formats/generate-edn (not no-pretty) colors no-commas {:print-width print-width}) println) :json (some-> input