Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #151: add --print-width option #152

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 14 additions & 9 deletions src/jet/formats.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand All @@ -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))
Expand Down
8 changes: 5 additions & 3 deletions src/jet/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}}]
Expand Down Expand Up @@ -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
Expand Down