need help on `pprint/write` code with better readability

85 views
Skip to first unread message

Jiyin Yiyong

unread,
Oct 23, 2016, 7:28:23 AM10/23/16
to Clojure
I'm using `write` function to generate code very heavily. But small part of the code are hard to read.
So I digged into the options and increased `right-margin` to make it a little better. Here's the changes:



But it turned out the algorithm is doing layout like always appending code in a line, until it reaches right margin.
So if `right-margin` is too big, all the code are in a single line, which is hard to read.
if `right-margin` is too small, then all the code in a column, which is also hard to read.
I think it's not smart enough to make all of my code readable enough, but majority of that is fine.

Is there any solution to improvement the readability at this moment?
Especially for such scenarios:



Thanks.

Alex Miller

unread,
Oct 23, 2016, 3:08:16 PM10/23/16
to Clojure
Try something like this:

(require '[clojure.pprint :as pprint])
(defn print-code [o]
  (binding [pprint/*print-right-margin* 100
            pprint/*print-miser-width* 60]
    (pprint/with-pprint-dispatch pprint/code-dispatch
      (pprint/pprint o))))

Or one of the "pretty printer" libraries like fipp or zprint.

jiyinyiyong

unread,
Oct 23, 2016, 10:42:34 PM10/23/16
to Clojure
What does miser-width mean since you set it to 60?

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/ZB-nSiFH3uw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thomas Heller

unread,
Oct 24, 2016, 4:35:51 AM10/24/16
to Clojure
Try https://github.com/weavejester/cljfmt 

It is specifically written for clj code and not general pprinter.

/thomas

jiyinyiyong

unread,
Oct 24, 2016, 5:47:51 AM10/24/16
to Clojure
Cool library! But i just changed to fipp this morning. I think I will try it much later. fipp is really fast that it succeeded clojure.pprint/write.

--
Reply all
Reply to author
Forward
0 new messages