Increasing indent level in pprint

248 views
Skip to first unread message

Asim Jalis

unread,
Jul 16, 2011, 3:46:42 PM7/16/11
to clo...@googlegroups.com
Is there an easy way to increase the indent of pprint data structures
from 1 to something like 2 or 4? I've been searching on Google and
going through the docs and don't see anything.

For example, I would like the following command to produce something
closer to "output 2" than to "output 1".

(pprint {:remote-addr "127.0.0.1", :scheme :http, :query-params {},
:session {}, :form-params {}, :request-method :get, :query-string nil,
:content-type nil, :cookies {"ring-session" {:value
"b3493c6e-40c3-441c-a75a-19d1a67e7b8d"}}, :uri "/", :server-name
"localhost", :params {}, :headers {"cache-control" "max-age=0",
"cookie" "ring-session=b3493c6e-40c3-441c-a75a-19d1a67e7b8d",
"accept-charset" "ISO-8859-1,utf-8;q=0.7,*;q=0.3", "accept-language"
"en-US,en;q=0.8", "accept-encoding" "gzip,deflate,sdch", "accept"
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"user-agent" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8)
AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122
Safari/534.30", "connection" "keep-alive", "host" "localhost:8080"},
:content-length nil, :server-port 8080, :character-encoding nil, :body
nil })

OUTPUT 1

{:remote-addr "127.0.0.1",
:scheme :http,
:query-params {},
:session {},
:form-params {},
:request-method :get,
:query-string nil,
:content-type nil,
:cookies
{"ring-session" {:value "b3493c6e-40c3-441c-a75a-19d1a67e7b8d"}},
:uri "/",
:server-name "localhost",
:params {},
:headers
{"user-agent" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8)
AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122
Safari/534.30",
"cookie" "ring-session=b3493c6e-40c3-441c-a75a-19d1a67e7b8d",
"accept-charset" "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
"accept" "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"host" "localhost:8080",
"cache-control" "max-age=0",
"accept-encoding" "gzip,deflate,sdch",
"accept-language" "en-US,en;q=0.8",
"connection" "keep-alive"},
:content-length nil,
:server-port 8080,
:character-encoding nil,
:body nil}

OUTPUT 2

{
:remote-addr "127.0.0.1",
:scheme :http,
:query-params {},
:session {},
:form-params {},
:request-method :get,
:query-string nil,
:content-type nil,
:cookies
{
"ring-session" {:value "b3493c6e-40c3-441c-a75a-19d1a67e7b8d"}
},
:uri "/",
:server-name "localhost",
:params {},
:headers
{
"user-agent" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8)
AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122
Safari/534.30",
"cookie" "ring-session=b3493c6e-40c3-441c-a75a-19d1a67e7b8d",
"accept-charset" "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
"accept"
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"host" "localhost:8080",
"cache-control" "max-age=0",
"accept-encoding" "gzip,deflate,sdch",
"accept-language" "en-US,en;q=0.8",
"connection" "keep-alive"
},
:content-length nil,
:server-port 8080,
:character-encoding nil,
:body nil
}

Sean Corfield

unread,
Jul 16, 2011, 7:23:51 PM7/16/11
to clo...@googlegroups.com
On Sat, Jul 16, 2011 at 12:46 PM, Asim Jalis <asim...@gmail.com> wrote:
> Is there an easy way to increase the indent of pprint data structures
> from 1 to something like 2 or 4? I've been searching on Google and
> going through the docs and don't see anything.
>
> For example, I would like the following command to produce something
> closer to "output 2" than to "output 1".

Just curious, is your background Java or some similar C-style language?

I ask because folks coming from that sort of background tend to want
braces and parentheses on separate lines and fixed indentation whereas
the Lisp crowd tend to want trailing braces and "natural" indentation,
i.e., continuation lines are indented so elements line up:

(somefn arg1
(f arg2))

If I got that right the (f... should line up with arg1 above it.

It took me a while to get used to the "Clojure way" of code/data
layout but now I tend to avoid commas and just go with the flow of how
pprint or my IDE wants to format stuff and I like the concise
consistency.

Sorry that doesn't answer your question but I hope it helps explain
why the default pprint output looks the way it does?
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
Railo Technologies, Inc. -- http://www.getrailo.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

Asim Jalis

unread,
Jul 16, 2011, 10:05:58 PM7/16/11
to clo...@googlegroups.com
The position of the braces might be a red herring here. I was mostly
interested in figuring out how to increase the indentation level from
1 to something larger. Even an indentation step of 2 for each level
would be easier on the eye than 1.

> --
> 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

Sean Corfield

unread,
Jul 16, 2011, 11:39:01 PM7/16/11
to clo...@googlegroups.com
On Sat, Jul 16, 2011 at 7:05 PM, Asim Jalis <asim...@gmail.com> wrote:
> The position of the braces might be a red herring here. I was mostly
> interested in figuring out how to increase the indentation level from
> 1 to something larger. Even an indentation step of 2 for each level
> would be easier on the eye than 1.

My point was that the "natural" Lisp/Clojure indentation is to match
the items above so for:

{:something

the natural indentation is 1 and for:

(foo a

the natural indentation is 5: '(', 'f', 'o', 'o', ' '.

Indentation is not some fixed quantity you can change - it's dependent
on the structure of the data/code and the length of the symbols.

Asim Jalis

unread,
Jul 17, 2011, 1:52:24 AM7/17/11
to clo...@googlegroups.com, clo...@googlegroups.com
Okay. I see what you mean.

Tom Faulhaber

unread,
Jul 19, 2011, 3:03:42 AM7/19/11
to Clojure
Sean's remark is right for writing code, but not really relevant for
pretty printed data structures. The pretty printer will either avoid
"(foo a" followed by a line break or fill that line full. (By default,
for lists it breaks the lines and for vectors it fills them.)

While there's no way to just set the indent, you can get this effect
with a little work.

The pretty printer allows customization of the output format using
dispatch functions.

By default it uses simple-dispatch, defined here:
https://github.com/clojure/clojure/blob/d1e39b1ec7fc65907b13458d7ec70b0839f3f85e/src/clj/clojure/pprint/dispatch.clj#L65

I have created a variant of simple dispatch that does the two
character indent (as per your example above). I through a little
project on github: https://github.com/tomfaulhaber/pprint-indent. The
README is basically a copy of your second example run in my repl. The
source is in https://github.com/tomfaulhaber/pprint-indent/blob/master/src/indent/indent.clj
for the curious.

Feel free to modify to taste. :)





On Jul 16, 10:52 pm, Asim Jalis <asimja...@gmail.com> wrote:
> Okay. I see what you mean.
>
> On Jul 16, 2011, at 8:39 PM, Sean Corfield <seancorfi...@gmail.com> wrote:
>
>
>
>
>
>
>
> > On Sat, Jul 16, 2011 at 7:05 PM, Asim Jalis <asimja...@gmail.com> wrote:
> >> The position of the braces might be a red herring here. I was mostly
> >> interested in figuring out how to increase the indentation level from
> >> 1 to something larger. Even an indentation step of 2 for each level
> >> would be easier on the eye than 1.
>
> > My point was that the "natural" Lisp/Clojure indentation is to match
> > the items above so for:
>
> > {:something
>
> > the natural indentation is 1 and for:
>
> > (foo a
>
> > the natural indentation is 5: '(', 'f', 'o', 'o', ' '.
>
> > Indentation is not some fixed quantity you can change - it's dependent
> > on the structure of the data/code and the length of the symbols.
> > --
> > Sean A Corfield -- (904) 302-SEAN
> > An Architect's View --http://corfield.org/
> > World Singles, LLC. --http://worldsingles.com/
> > Railo Technologies, Inc. --http://www.getrailo.com/

Sean Corfield

unread,
Jul 19, 2011, 4:19:39 PM7/19/11
to clo...@googlegroups.com
On Tue, Jul 19, 2011 at 12:03 AM, Tom Faulhaber <tomfau...@gmail.com> wrote:
> Sean's remark is right for writing code, but not really relevant for
> pretty printed data structures. The pretty printer will either avoid
> "(foo a" followed by a line break or fill that line full. (By default,
> for lists it breaks the lines and for vectors it fills them.)

Interesting. I didn't realize it took a different approach.

Out of curiosity, what is the thinking behind the difference between
lists and vectors?


--
Sean A Corfield -- (904) 302-SEAN

An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

Railo Technologies, Inc. -- http://www.getrailo.com/

Tom Faulhaber

unread,
Jul 19, 2011, 11:41:50 PM7/19/11
to Clojure
Hmmm, looking back at the code, I see that I mis-remembered the fact
that lists and vectors were different. They both (along with maps)
will break rather than fill. Arrays and sets both fill rather than
break.

I'm not sure how much logic there is around this. It just fit my
intuition about how the different structures would be most likely used
when I wrote it. It still seems about right. But if folks think it
should be different, it's easy to change.

On Jul 19, 1:19 pm, Sean Corfield <seancorfi...@gmail.com> wrote:
> On Tue, Jul 19, 2011 at 12:03 AM, Tom Faulhaber <tomfaulha...@gmail.com> wrote:
> > Sean's remark is right for writing code, but not really relevant for
> > pretty printed data structures. The pretty printer will either avoid
> > "(foo a" followed by a line break or fill that line full. (By default,
> > for lists it breaks the lines and for vectors it fills them.)
>
> Interesting. I didn't realize it took a different approach.
>
> Out of curiosity, what is the thinking behind the difference between
> lists and vectors?
> --
> Sean A Corfield -- (904) 302-SEAN
> An Architect's View --http://corfield.org/
> World Singles, LLC. --http://worldsingles.com/
> Railo Technologies, Inc. --http://www.getrailo.com/
Reply all
Reply to author
Forward
0 new messages