awesome-print / awesome_print

Pretty print your Ruby objects with style -- in full color and with proper indentation
http://github.com/michaeldv/awesome_print
MIT License
4.07k stars 454 forks source link

needs a :dont_align_values option (nested hashes currently look not-awesome) #317

Open jpwynn opened 7 years ago

jpwynn commented 7 years ago

Seems unusable when you have nested hashes... the 'child' hash is actually displayed to the LEFT of the parent hash IF hash keys get long and descriptive:

h = { :why_int_the_world_does => "AP do", :this => { :non_sense => "with nested hashes?" } }

{
    :why_in_the_world_does => "AP do",
                     :this => {
        :non_sense => "with nested hashes?"
    }
}

That's very hard to decipher indents when you display a hash with say 20 keys and 3 or 4 nesting levels!

IMO it needs an option to simply indent each KEY level of a hash by N spaces and let the VALUE fall where it may, right after the key, with no attempt at aligning the values

{
    :it_really_needs_an_option => "to turn off the value centering alignment",
    :like_this => {
        :so_that => "nested hashes ALWAYS look right"
    }
}

perhaps call it 'simple_indent'

Pysis868 commented 7 years ago

Problem

I too, would also like this feature. After a brief align search into the codebase, I found these relevant files:

Now 'base' would probably be used only in the case of strings, not hashes like my current use case concerns, but I am not sure, and don't expect that to be necessary for my use case.

When digging into the hash file, I found the functions printable_hash, ruby19_syntax, and pre_ruby19_syntax have a width argument that most likely controls this behavior, which comes from the left_width function call inside of printable_hash.

Possible Solution

I was thinking :justify_hash => false to disable this behavior. I'm sure there are other combinations for the option key name to quickly think of as well.

This way, you can still have indentation, but don't require text layout justification with that extra spacing.

For my example, I wanted this:

2.1.5 (main):0 > ap({one: 1, two: 2, three: 3})
{
  :one   => 1,
  :three => 3,
  :two   => 2
}
=> nil
2.1.5 (main):0 > ap({one: 1, two: 2, three: 3}, {justify_hash: false})
{
  :one => 1,
  :three => 3,
  :two => 2
}
=> nil

where my use of the option in the second example denotes my requested behavior, where the '=>' symbols are not aligned with more than 1 preceding space, with the same affecting the values as well.

Etc.

Related