From ec23abfdaecab07342c2941da66aa73874f6c8c8 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 21 Mar 2024 18:31:28 +0100 Subject: [PATCH 1/2] Fix #151: add --print-width option --- CHANGELOG.md | 4 ++++ src/jet/formats.clj | 12 +++++++----- src/jet/main.clj | 8 +++++--- 3 files changed, 16 insertions(+), 8 deletions(-) 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..9e76ee4 100644 --- a/src/jet/formats.clj +++ b/src/jet/formats.clj @@ -44,10 +44,11 @@ (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 {:map-delimiter (if uncomma "" ",") + :width print-width}) + (fipp/pprint x {:width print-width}))) (defn json-parser [] (.createParser ^JsonFactory (or factory/*json-factory* @@ -73,8 +74,9 @@ (recur next)))))) str)) -(defn generate-edn [o pretty color uncomma] - (let [edn-str (if pretty (str/trim (with-out-str (pprint o color uncomma))) +(defn generate-edn [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) 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 From 172c8d974c4a4c726d61585b521b75222c2fa563 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 21 Mar 2024 18:37:34 +0100 Subject: [PATCH 2/2] fix --- src/jet/formats.clj | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/jet/formats.clj b/src/jet/formats.clj index 9e76ee4..da2d57d 100644 --- a/src/jet/formats.clj +++ b/src/jet/formats.clj @@ -46,9 +46,10 @@ (defn pprint [x colors {:keys [uncomma print-width]}] (if colors - (puget/cprint x {:map-delimiter (if uncomma "" ",") - :width print-width}) - (fipp/pprint x {:width print-width}))) + (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* @@ -74,13 +75,15 @@ (recur next)))))) str)) -(defn generate-edn [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 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))