Task: Improve sys.inspect

67 views
Skip to first unread message

r...@tinyclouds.org

unread,
Feb 25, 2010, 4:33:16 PM2/25/10
to nodejs
For fun and to attract people to hacking Node, I'm going to
occasionally post little tasks to the mailing list. If you do this,
please create a patch against HEAD and post it to this mailing list.
I'll wait a week for someone to do them before doing it myself :)


This is of moderate difficulty and requires only javascript knowledge
but no I/O knowledge.

sys.inspect has improved a lot in the past months, but it seems to
have become so verbose that I'm switching back to JSON.stringify() for
many things. It would be nice if sys.inspect had a more compact
output. Instead of printing [1,2,3] on 5 lines, it could just be
"[1,2,3]". It should make some smart decisions about when to place
things on a single line, and when to split it across multiple lines.

When printing objects on multiple lines, I prefer the Haskell-style
comma in front. I'm trying to promote it's usage in javascript and I'd
like sys.inspect to do this.

Instead of

{
"foo": "bar",
"hello": "world",
"baz": [
1,
2,
3
],
"x": "something long that forces it onto multiple lines"
}

I'd like it to display

{ foo: 'bar'
, hello: 'world'
, 'baz': [1,2,3]
, 'x': 'something long that forces it onto multiple lines'
}

- comma in front
- don't put '{' on it's own line
- don't quote object keys when they match [_a-zA-Z0-9]+
- use single quotes instead of double
- only go onto multiple lines if it can't fit onto one.

If that object was without 'x', then it would be displayed as

{ foo: 'bar', hello: 'world', 'baz': [1,2,3] }

Matthew Ranney

unread,
Feb 25, 2010, 4:57:16 PM2/25/10
to nod...@googlegroups.com
On Thu, Feb 25, 2010 at 1:33 PM, <r...@tinyclouds.org> wrote:
- only go onto multiple lines if it can't fit onto one.

How do you know how wide the output is?  Assume 80 characters?

Ryan Dahl

unread,
Feb 25, 2010, 5:00:47 PM2/25/10
to nod...@googlegroups.com

Since it's pretty dense stuff being outputted, I think it should be
limited to 60 columns.

inimino

unread,
Feb 25, 2010, 5:28:54 PM2/25/10
to nod...@googlegroups.com
On 2010-02-25 14:33, r...@tinyclouds.org wrote:
> sys.inspect has improved a lot in the past months, but it seems to
> have become so verbose that I'm switching back to JSON.stringify() for
> many things. It would be nice if sys.inspect had a more compact
> output. Instead of printing [1,2,3] on 5 lines, it could just be
> "[1,2,3]". It should make some smart decisions about when to place
> things on a single line, and when to split it across multiple lines.

If someone wants to start on this, take a look at pp() (including
the commented-out version) here:

http://boshi.inimino.org/3box/3box/util.js

It does the Haskell-style comma-in-front output, and the commented-
out version does all-on-one-line output (used in a couple IRC bots)
but you'd need to combine these two into a new version that's smart
about where to break lines (maybe by keeping track of property name
and value lengths).

It also handles recursive structures a little differently, by having
a hard limit on nesting, which is usually what I want when debugging
anyway.

--
http://inimino.org/~inimino/blog/

Benjamin Thomas

unread,
Feb 25, 2010, 7:35:49 PM2/25/10
to nod...@googlegroups.com
I have a version of this that is mostly complete: http://gist.github.com/315235

I still have to do:


- don't quote object keys when they match [_a-zA-Z0-9]+
- use single quotes instead of double

But those are trivial.

My question is what do you want to happen with nested objects? I added
some different options to the gist:
http://gist.github.com/315235#file_output_options.txt

Michael Stillwell

unread,
Feb 25, 2010, 7:44:09 PM2/25/10
to nod...@googlegroups.com
On Thu, Feb 25, 2010 at 9:33 PM, <r...@tinyclouds.org> wrote:

> ... It would be nice if sys.inspect had a more compact


> output. Instead of printing [1,2,3] on 5 lines, it could just be
> "[1,2,3]". It should make some smart decisions about when to place
> things on a single line, and when to split it across multiple lines.

Comma in front looks strange to me (I'd prefer that a trailing comma
being permissible on every line like Perl, although that's not going
to happen)--but is there some what to combine sys.debug() and
sys.inspect() into one command? I find myself doing
sys.debug(sys.inspect(...)) frequently, which is does not please me.

Michael

--
http://beebo.org

Ryan Dahl

unread,
Feb 25, 2010, 7:46:09 PM2/25/10
to nod...@googlegroups.com
On Thu, Feb 25, 2010 at 4:35 PM, Benjamin Thomas <bam.t...@gmail.com> wrote:
> I have a version of this that is mostly complete:  http://gist.github.com/315235

Wow, that was quick :)

> I still have to do:
> - don't quote object keys when they match [_a-zA-Z0-9]+
> - use single quotes instead of double
> But those are trivial.
>
> My question is what do you want to happen with nested objects? I added
> some different options to the gist:
> http://gist.github.com/315235#file_output_options.txt

Oh! Thanks for the choice. Definitely 1 or 3. Probably 3.

Benjamin Thomas

unread,
Feb 25, 2010, 9:01:25 PM2/25/10
to nod...@googlegroups.com
Okay, all done!

You can see the test file I used while developing:
http://gist.github.com/315235#file_inspect.js

And the patch (with updated docs/api.txt and test/simple/test-sys.js):
http://gist.github.com/315235#file_0001_make_the_output_of_sys.inspect_a_lot_more_compact.patch

I decided I liked splitting onto multiple lines after 50 characters,
but if you really want 60, it is pretty easy to change: line 90 of
the patch.

I also went ahead and made so it by default only recurses twice. To
undo that default remove lines 114 and 115 of the patch.

Tell me if you find any problems.

Ryan Dahl

unread,
Feb 25, 2010, 9:30:34 PM2/25/10
to nod...@googlegroups.com
On Thu, Feb 25, 2010 at 6:01 PM, Benjamin Thomas <bam.t...@gmail.com> wrote:
> Okay, all done!

Great! applied in de1521413ef53bd63c87a9698eb3611790bc5a56

Ryan Dahl

unread,
Feb 26, 2010, 4:52:52 PM2/26/10
to nod...@googlegroups.com
On Thu, Feb 25, 2010 at 6:01 PM, Benjamin Thomas <bam.t...@gmail.com> wrote:
> Tell me if you find any problems.

I found a bug or two: http://gist.github.com/316205
the deep multiline objects don't get put on the same line as the comma
they follow.

Benjamin Thomas

unread,
Feb 27, 2010, 3:46:04 AM2/27/10
to nod...@googlegroups.com
On Fri, Feb 26, 2010 at 2:52 PM, Ryan Dahl <coldre...@gmail.com> wrote:
> I found a bug or two: http://gist.github.com/316205
> the deep multiline objects don't get put on the same line as the comma
> they follow.

I fixed the output from 'classes' variable but I am not clear on what
is wrong with the 'constants' output.

This gist has the patch, the script I ran (just x.js from your gist)
and the new output:
http://gist.github.com/316574

The issue was only with the case where multiline objects were elements
in an array. In that case we don't want to put the object on a new
line because the previous line doesn't have a key. Not that you
really cared to know what the problem was :)

Benjamin Thomas

unread,
Feb 27, 2010, 4:16:44 AM2/27/10
to nod...@googlegroups.com
On Sat, Feb 27, 2010 at 1:46 AM, Benjamin Thomas <bam.t...@gmail.com> wrote:
> This gist has the patch, the script I ran (just x.js from your gist)
> and the new output:
> http://gist.github.com/316574

Also now would be a convenient time to decide if you like the default
of only recursing twice--now that you have had a chance to try it out
for a few days. If you want me to change it back to recursing
indefinitely just tell me and I'll update the patch.

Benjamin Thomas

unread,
Feb 27, 2010, 4:19:41 AM2/27/10
to nod...@googlegroups.com
On Thu, Feb 25, 2010 at 5:44 PM, Michael Stillwell <m...@beebo.org> wrote:
> Comma in front looks strange to me (I'd prefer that a trailing comma
> being permissible on every line like Perl, although that's not going
> to happen)--but is there some what to combine sys.debug() and
> sys.inspect() into one command?  I find myself doing
> sys.debug(sys.inspect(...)) frequently, which is does not please me.

Michael: If you do

sys.p(...)

It is similar to what you ask for,

sys.error(sys.inspect(...));

I agree the comma in front style does look a little strange at first,
but after I got used to it, I think it makes it really easy to scan
through the output.

Michael Stillwell

unread,
Feb 27, 2010, 7:40:33 AM2/27/10
to nod...@googlegroups.com
On Sat, Feb 27, 2010 at 9:19 AM, Benjamin Thomas <bam.t...@gmail.com> wrote:
> On Thu, Feb 25, 2010 at 5:44 PM, Michael Stillwell <m...@beebo.org> wrote:
>> ... is there some what to combine sys.debug() and

>> sys.inspect() into one command?  I find myself doing
>> sys.debug(sys.inspect(...)) frequently, which does not please me.

>
> Michael: If you do
>
> sys.p(...)
>
> It is similar to what you ask for,
>
> sys.error(sys.inspect(...));

Ah, thanks! Is there any reason this is not documented at

http://nodejs.org/api.html#_system_module

? Actually, sys.error() (async output to stderr?) doesn't seem to be
documented either.


Michael

--
http://beebo.org

Gabriel Farrell

unread,
Feb 27, 2010, 4:12:24 PM2/27/10
to nod...@googlegroups.com

I'm used to the way JSON is usually pretty printed, and this is the
first time I've seen the comma-in-front style. It looks awfully
strange to me, as well, but I guess I'll get used to it.

Benjamin Thomas

unread,
Mar 1, 2010, 4:31:53 PM3/1/10
to nod...@googlegroups.com
On Sat, Feb 27, 2010 at 1:46 AM, Benjamin Thomas <bam.t...@gmail.com> wrote:
> I fixed the output from 'classes' variable but I am not clear on what
> is wrong with the 'constants' output.
>
> This gist has the patch, the script I ran (just x.js from your gist)
> and the new output:
> http://gist.github.com/316574
>
> The issue was only with the case where multiline objects were elements
> in an array.  In that case we don't want to put the object on a new
> line because the previous line doesn't have a key.  Not that you
> really cared to know what the problem was :)

Is this not working for you? Find other problems? I haven't heard any
responses...

Ryan Dahl

unread,
Mar 1, 2010, 4:45:15 PM3/1/10
to nod...@googlegroups.com

Sorry, got lost in my queue. Works beautifully, thank you! Committed in 6034701.

Reply all
Reply to author
Forward
0 new messages