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] }
- only go onto multiple lines if it can't fit onto one.
Since it's pretty dense stuff being outputted, I think it should be
limited to 60 columns.
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.
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
> ... 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
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.
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.
Great! applied in de1521413ef53bd63c87a9698eb3611790bc5a56
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 :)
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.
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.
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
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.
Is this not working for you? Find other problems? I haven't heard any
responses...
Sorry, got lost in my queue. Works beautifully, thank you! Committed in 6034701.