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

Text Wrapping issues #30

Open
PeterTCormack opened this issue Sep 16, 2020 · 6 comments
Open

Text Wrapping issues #30

PeterTCormack opened this issue Sep 16, 2020 · 6 comments

Comments

@PeterTCormack
Copy link

I'm using the most recent version (0.3.0) of safer_rails_console, and Iterm2. When I use the terminal normally, it wraps text to the next line when I get to the edge of the window. However, when I start up the Rails console, it both wraps too early (there is still plenty of white space before the edge of the window), and starts overwriting the current line, instead of going to the next line. If I disable the safer_rails_console gem, it wraps properly.

Here is what it looks like with safer_rails_console:
console wrapping problem

Here is what it looks like without safer_rails_console:
console proper wrapping

@jturkel
Copy link
Member

jturkel commented Sep 24, 2020

Thanks for the bug report! I can't reproduce this using safer_rails_console 0.4.0 + Rails 6.0.3.3 + Ruby 2.6.5 + iTerm2 3.4.0beta8 + bash though:

long-text

Can you narrow the bug down to a particular version of the various libraries/applications in play here? Perhaps this gem's prompt format string is conflicting with something in your Rails application name or shell configuration?

@PeterTCormack
Copy link
Author

@jturkel : Thank you for responding! We're on 0.3.0 of safer_rails_console, and when I removed the version limiter, it still got 0.3.0. How do I get 0.4.0? Is that a beta branch or something of that nature? We're still in Rails 4.7 and Ruby 2.4.9. I'm using iTerm 3.3.12 (the latest non-beta version) + zsh (because Mac switched to that recently).

That's an interesting idea, how would I tell if that were the case?

I did discover something interesting just yesterday. iTerm had my "new window columns" set to 80 by default. I usually ignore that because I change the window size to suit my needs in the moment, so what it starts out as is usually irrelevant. But, the wrapping so early got me to thinking about that. When I set it to the maximum (~1200), this issue wasn't nearly as pronounced, because it didn't cut off my letters quite so early. So, perhaps there's some kind of issue where safer_rails_console doesn't listen to changes in window size?

@jturkel
Copy link
Member

jturkel commented Sep 25, 2020

safer_rails_console really just configures the IRB prompt using the customizations in this file that set its document prompt configuration options. All of the lower level details about how to render lines are handled directly by IRB.

My recommendation for debugging this it to copy the customizations from lib/safer_rails_console/consoles/irb.rb into your ~/.irbrc (inlining any methods or references to Rails configuration) and then test different permutations of the IRB configuration using standard irb with no gems loaded to see if you can narrow down the cause the problem. That should eliminate Rails + safer_rails_console from the equation allowing us to get at the root cause more quickly.

@PeterTCormack
Copy link
Author

@jturkel : Fascinating. Especially curious since disabling safer_rails_console does resolve these issues entirely. If I can scrape together some extra bandwidth, I'll try some of those troubleshooting strategies. Thank you for the insight!

@PeterTCormack
Copy link
Author

@jturkel : One of my colleagues figured out some code to run after the console has started up to resolve the wrapping issues:

IRB.conf[:PROMPT][:RAILS_ENV].each do |_,str|
  str.gsub!("\e[32m", "\[\e[32m\]" => "\[\e[32m\]")
  str.gsub!("\e[0m", "\[\e[0m\]" => "\[\e[0m\]")
end

This seems to disable the colorization of the prompt, but that's a minor price to pay for proper wrapping. I tried a couple of different strategies to force this to automatically happen each time the console was fired up. The first was to add a console entry in my config/application.rb file (as suggested here - scroll down to where they give the "simple" answer ), like this:

console do
  IRB.conf[:PROMPT][:RAILS_ENV].each do |_,str|
    str.gsub!("\e[32m", "\[\e[32m\]" => "\[\e[32m\]")
    str.gsub!("\e[0m", "\[\e[0m\]" => "\[\e[0m\]")
  end
end

However, I saw the following error when I attempted to start my rails console: /Users/cormape/drb-student-los/config/application.rb:104:in block in class:Application': undefined method []' for nil:NilClass (NoMethodError) indicating that the IRB object had not been initialized yet. Indeed, when I changed the console block above to be simply puts "hello", I saw the following:

hello
Loading development environment (Rails 4.2.11.3)
irb: warn: can't alias context from irb_context.
drb-student-los(dev)(writable):001:0>

You can see that the hello came first, then the IRB stuff.

Next, I tried to create and edit a .irbrc file (as suggested here ) as follows:

if (defined? Rails)
  IRB.conf[:PROMPT][:RAILS_ENV].each do |_,str|
    str.gsub!("\e[32m", "\[\e[32m\]" => "\[\e[32m\]")
    str.gsub!("\e[0m", "\[\e[0m\]" => "\[\e[0m\]")
  end
end

That was better, but I still get an error when I start up the console, and the wrapping is still broken:

Loading development environment (Rails 4.2.11.3)
Error : 'load /Users/cormape/.irbrc' : undefined method `each' for nil:NilClass
irb: warn: can't alias context from irb_context.
drb-student-los(dev)(writable):001:0>

Here, we can see that the IRB object is there, and I did some other testing to confirm that the IRB.conf[:PROMPT] is there and is a hash, so it's the `IRB.conf[:PROMPT][:RAILS_ENV] that's returning nil, so there's still some code running after that.

I looked into the file you suggested, and it seems like the problem may lie in the color_text method, which I found in this file. That looked very promising, so I went in there on my local machine and added the \[ and \] in what seemed to be the appropriate places, but no luck. In fact, no matter what I did to that file seemed to have no effect. However, when I added the full block to the irb.rb file itself, that worked:

IRB.conf[:PROMPT][:RAILS_ENV].each do |_,str|
  str.gsub!("\e[32m", "\[\e[32m\]" => "\[\e[32m\]")
  str.gsub!("\e[0m", "\[\e[0m\]" => "\[\e[0m\]")
end

By "worked" I mean that the wrapping worked, but the colors did not. So, I think the real solution might be more subtle ...

@jeffktan
Copy link

jeffktan commented Oct 5, 2020

I think there's some plugin we're running in iTerm2 related to themes.

Could we commit something like this change to the project?
jeffktan/safer_rails_console_0_3_0@5147dd9

(I couldn't just add the [ to the string as ruby kept truncating the 's)

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

No branches or pull requests

3 participants