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

Indent has no effect on String #221

Open mkunkel opened 8 years ago

mkunkel commented 8 years ago

While trying to format output, I came across a behavior that I did not expect. I tried to apply indentation to a string, but nothing happened. The readme does not suggest that the indent option is not available on strings.

What I did:

require 'awesome_print'
=> true
ap 'Indented string', indent: 4
"Indented string"
=> nil

What I expected:

require 'awesome_print'
=> true
ap 'Indented string', indent: 4
    "Indented string"
=> nil
waldyr commented 8 years ago

Hey @mkunkel. Thanks for using awesome_print.

I totally agree with you. However it's not clear if you want just a documentation update or a feature request.

gerrywastaken commented 8 years ago

@waldyr doesn't the indent setting just sent the number of spaces to use when indentation is required, like when stepping inside a hash? I could be wrong but I thought indentation was only applied when stepping inside a block of some kind.

For instance you would only see the indent of 4 above if the string were inside something:

irb(main):009:0> ap ['Indented string'], indent: 4
[
    [0] "Indented string"
]
irb(main):010:0> ap ['Indented string'], indent: 8
[
        [0] "Indented string"
]
waldyr commented 8 years ago

@gerrywastaken That is probably the current behavior, but @mkunkel might want to do something like:

irb> ap "test", indent: 4
"    test"
gerrywastaken commented 8 years ago

@waldyr sure but I don't think that's the idea behind the current indentation feature. I believe the idea is to indent things so that it makes it clear when we are stepping into and out of structures. Just like somebody would do when formatting their own code in code editor or an auto beautify function. I'm not sure that indenting at the first level makes much sense for everybody else. I'm not sure what the use case is here, but I suspect it isn't in line with what most expect from awesome print.

The example you gave appears to be changing the printed string itself, which doesn't seem to be what @mkunkel was asking for. I think he was asking for the indent to be applied at the first level. However the point of the indentation is separate multiple levels inside some code. for example this:

{foo: {bar: :tee}}

becomes:

{
    :foo => {
        :bar => :tee
    }
}

If we did indentation at the first level then it would become:

  {
      :foo => {
          :bar => :tee
      }
  }

I'm not sure what benefit that first indent provides. Am I missing something?

Edit if anybody actually wants to create indenting inside a string you could create your own method based off the one in Rails: http://apidock.com/rails/v4.2.1/String/indent%21

waldyr commented 8 years ago

Good point. However If this turns out to be true based on the @mkunkel's request (which, in my opinion, is due making more sense than the feature requested) at least a clarification is needed. So let's see what he was thinking when created this issue. I'm marking as Awaiting response. @gerrywastaken, should we have a timeout for awaiting response tickets?

gerrywastaken commented 8 years ago

@waldyr If we set a timeout, I think a fair one would as long as we took to respond at a minimum. :) Otherwise somebody could rightly make a complaint along the lines of.... "It took you guys a year to get back to me, but then you close this because I didn't get back to you within a month". Thoughts?

Thanks heaps for your work going through tickets btw!

waldyr commented 8 years ago

@gerrywastaken hahaha that's totally true.

Maybe an year from the date of label's creation is good enough.

Thanks heaps for your work going through tickets btw!

You're welcome 👍

mkunkel commented 8 years ago

This was a feature request. This was to be used in https://github.com/olegantonyan/super_awesome_print which uses Awesome Print to output a timestamp, object class, and a trace in addition to the object itself. The reason this feature is desired is to make it so that a user can customize the output of this block. This is particularly helpful when there are multiple lines from the trace showing.

"*** 2016-06-20 13:30:36 -0500 ***"
String < Object
"(pry):6:in `show'"
"/gems/pry-0.10.3/lib/pry/pry_instance.rb:355:in `eval'"
"/gems/pry-0.10.3/lib/pry/pry_instance.rb:355:in `evaluate_ruby'"
"Asia"
"*** END ***"

Could be customized based on user preferences to look more like:

"*** 2016-06-20 13:30:36 -0500 ***"
  String < Object
    "(pry):6:in `show'"
    "/gems/pry-0.10.3/lib/pry/pry_instance.rb:355:in `eval'"
    "/gems/pry-0.10.3/lib/pry/pry_instance.rb:355:in `evaluate_ruby'"
  "Asia"
"*** END ***"