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

Option to enable more compact layout for small objects and arrays #56

Open
softwarecreations opened this issue Mar 15, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@softwarecreations
Copy link

softwarecreations commented Mar 15, 2022

I've noticed lately that node.js and mongosh are pretty good at not wasting lines when objects have few properties in them.
Would be really nice if pretty-print-json could present the stylers arrays on a single line.
So using 1 line instead of the 5 lines it currently uses to show the stylers arrays:

    styles: [
        {
          elementType: "geometry",
          stylers: [ { color:'#242f3e' } ],
        },
        {
          elementType: "labels.text.stroke",
          stylers: [ { color:'#242f3e' } ],
        },
        {
          elementType: "labels.text.fill",
          stylers: [ { color:'#746855' } ],
        },

Instead of the current behavior.

image

It's very easy to see the number of elements are in an array, or keys in an object. Then doing a sanity check on the line length to determine if it should stay on a single line or spread over multiple lines. So it's very easy to produce a space efficient presentation.

One way to easily achieve this is if we could pass our own formatting function, so that the lib calls our function with the object(s) and we we format it in some way that the lib understands (spaces on left and newlines, etc) and return it.

Note I've used:

  • single quotes for improved readability vs double quotes (which weren't necessary because the strings didn't contain single quotes)
  • Spaces between symbols except the colon, when there are multiple keys on a single line I prefer no space around the colon, like
    { foo:1, bar:2, baz:3 }

The great thing, is with a callback etc we can style stuff how we like.

@dpilafian
Copy link
Member

The default JSON.stringify() output can definitely result in unsightly wasted lines.

The idea of a "formatting function" sounds good, but it could be really tricky to create such a function. Do you have an example function?

@dpilafian dpilafian added the enhancement New feature or request label Mar 16, 2022
@softwarecreations
Copy link
Author

softwarecreations commented Mar 20, 2022

Hi there, sorry for taking a few days to get back to you. Thanks for your positive response!

I've actually just had a quick look at the code now for the first time to see what it's doing; and it looks like adding the functionality that I have in mind will be reasonable.

I'll need to play around with it and see what I come up with, I have some ideas. I've done a fair amount of this sort of thing before.

It could be days or weeks til I've got some time to work on it; but I'm definitely interested.

If you or anyone else decides to work on any such functionality, please keep this issue updated so that nobody wastes their time :)

Chat soon

PS: If having this issue open with no defined ETA on closing it bothers you, then feel free to close it, I won't be offended.
Now that I know you're open to the idea I'll try some ideas and either show or PR when I have time.

@dpilafian dpilafian changed the title Feature request: Option to enable more compact layout for small objects and arrays Option to enable more compact layout for small objects and arrays Mar 21, 2022
@BobPritchett
Copy link

I've been looking for a solution for this too... I think a very simple rule could be to evaluate every { } block and see if it could be rendered on one line without going over the line-break threshold.

While this is simplistic, since the common use case for this is small fields (and probably arrays of small objects) it would likely work well enough. Smarter rules might even be worse; I often have an array of many small items with one or two objects with longer/extra properties; when I format them manually I just use multiple lines for those. Readability is enhanced by seeing more on the screen, more than be perfect consistency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants