awesome-print/awesome_print

Implement sap (super awesome print)

najamelan opened this issue · 4 comments

def read_line_number( filename, number )
  return nil if number < 1
  line = File.readlines( filename )[ number-1 ]
  line ? line.strip : nil
end

def sap arg
  puts read_line_number(
    caller_locations.first.absolute_path,
    caller_locations.first.lineno
  ).gsub!(
    /sap +/,
    ''
  ) + ' ⇨ ' + arg.ai
end

sap Object.methods.include? :extend

output:

Object.methods.include? :extend ⇨ true

Super awesome for debugging and experimenting...

Edit: @gerrywastaken modified the formatting of the code for easier reading.

Btw, sap could take a block to, but that would be a bit more work to implement...

So basically, show both the value and the code that lead to that value in the output?

I like the idea and I was also wanting something related where the filename and line numbers are printed out for every ap call if you enable a config option.

But both run into similar complications, such as when somebody uses a wrapper method which makes a call to sap. Instead of showing the code outside the wrapper method, every call just shows the sap call inside the wrapper method.

Then again, maybe it's not a terrible idea to use something like this for a first version and then, have somebody else fix the issue I mentioned via another PR.

Either way, this would need tests and a PR as I currently don't have time to work on this idea. There are also errors if this is called from within Pry.

@waldyr What do you think about this idea?

I realize that there is some work to implement this, but I am on a really tight schedule right now, so I can't spare a few hours to do this properly. I thought I share the idea because it's very useful and someone might get excited over this, and even like this the code already served me in debugging last night and this morning, so it could already help some people, but I thought this could have it's place in awesome print.

I can propose some improvements, lap outputs filename and number:

def lap arg

    loc = caller_locations.first

    puts read_line_number( loc.absolute_path, loc.lineno ).

        gsub!( /sap +/, "#{Pathname(loc.path).basename}:#{loc.lineno} - " ) + ' ⇨ ' + arg.ai

end

Outputs:
test.rb:14 - Object.methods.include? :extend ⇨ true

Enable wrappers (take a depth option):

def sap arg, depth: 0

  loc = caller_locations[ depth ]

  puts read_line_number( loc.absolute_path, loc.lineno ).
     gsub!( /sap +/,  ''  ) + ' ⇨ ' + arg.ai

end

I don't see the relevance of using this from pry since it's meant to work with files. irb and pry normally show you every line of code, and it's output anyway, no?

I don't see the relevance of using this from pry since it's meant to work with files. irb and pry normally show you every line of code, and it's output anyway, no?

Correct, I just think the current failure isn't all that helpful to end users. We should clearly state that it doesn't work inside a repl where there is no corresponding file/line number.

Enable wrappers (take a depth option)

The "/sap +/" pattern would also need to change there.

I thought I share the idea because it's very useful and someone might get excited over this

No worries, I'll leave it open till somebody has time to spend on it or gets motivated enough to send a PR. Maybe me, but certainly not now and even when I next have time for AP I have some other big things to fix.