nonsequitur / inf-ruby

218 stars 69 forks source link

fix ruby-print-result for pry #174

Closed bo-tato closed 1 year ago

bo-tato commented 1 year ago

In ruby-print-result-value it searches for " => " to find the result. That is what irb prints:

3.2.2 :001 > 2 + 2
 => 4 

But pry doesn't include the leading space:

[1] pry(main)> 2 + 2                                                                                    
=> 4

I simply removed the leading space from the search pattern and just had it search for "=> ", so it works with pry also

dgutov commented 1 year ago

Hi!

I suggest we change this to regexp "[ \n]=> " instead for a little more strictness.

Which version of Pry are you testing with, BTW? It works okay here, but I have an older version installed.

dgutov commented 1 year ago

Thanks!

bo-tato commented 1 year ago

ok good idea, I changed regex to "[ \n]=> " and double checked it's still working on pry and irb. I'm on pry 0.14.2 but probably it is cause of my removing intermediate prompts cause of https://github.com/nonsequitur/inf-ruby/issues/49 by default when I send "2 + 2" it prints:

[2] pry(main)> 
[2] pry(main)* [2] pry(main)* => 4

and ruby-print-result finds it as its on the same line. After I add to my pryrc: Pry.config.prompt.prompt_procs[1] = proc { "" } if ENV["INSIDE_EMACS"] to get rid of those intermediate prompts when I send block or region from ruby buffer then it prints:

[19] pry(main)> 
=> 4
[20] pry(main)> 

And now "=>" is at start of the line. If you run pry outside of emacs it really always is at the start of the line:

[1] pry(main)> 2.times do                                                                               
[1] pry(main)*   puts "test"                                                                            
[1] pry(main)* end                                                                                      
test
test
=> 2
[2] pry(main)> 

It's just the current buggy behaviour of displaying the intermediate prompts and repeated on the same line also with "=>" was making ruby-print-result work for pry

dgutov commented 1 year ago

It's just the current buggy behaviour of displaying the intermediate prompts and repeated on the same line also with "=>" was making ruby-print-result work for pry

It's the same for Irb, I think. Anyway, with the option of matching \n it should work even after (and if) we resolve that problem.

bo-tato commented 1 year ago

Running irb outside emacs it seems irb really does have the extra space over pry, ie it prints "\n => 4"

bo-tato commented 1 year ago

ah maybe my comment about irb in the other issue was misleading:

The only problem with it to address is that ruby-print-result expects the output to be prefixed with " => ", which is no longer the case with irb in --inf-ruby-mode

I meant with that option irb just doesn't prefix output with "=>" at all:

$ irb --inf-ruby-mode
irb(main):001:0> 2 + 2
4
dgutov commented 1 year ago

That probably depends on the version: 1.3.0 doesn't, at least not here.

bo-tato commented 1 year ago

Oh that is interesting it looks like they made a minor output change, on a box with irb 1.3.5 it's like you say, with "=>" at start of line. I'm using irb 1.6.2 where it displays with a extra space before "=>"

snowman commented 1 year ago

Just a note, if you run either irb/pry, neither outputs =>, try to uninstall awesome_print: $ gem uninstall --verbose awesome_print

dgutov commented 1 year ago

@snowman I suppose it should be enough to remove AwesomePrint.irb! from your .irbrc. Or hide it behind an if ENV['INSIDE_EMACS'].

We could also try disabling it dynamically, but that's by definition not very reliable.

bo-tato commented 1 year ago

@snowman are you sure? I don't have awesome_print installed anywhere:

$ gem list | grep print
prettyprint (default: 0.1.1)

and I just tried on two servers that just have system package manager installed ruby and no gems besides the default that came with ruby, and irb/pry output =>. irb seems to only output => when it's run with a tty, pry outputs it regardless whether it's run with tty or not.

Edit: My bad, I misunderstood you as saying AwesomePrint is what adds the => marker when you're saying that AwesomePrint removes it