Why does my IRB prompt with ANSI color codes mess up page up/down behavior with copy/paste?
Asked Answered
H

2

8

I added to my .irbrc:

IRB.conf[:PROMPT].reverse_merge!(:RAILS_ENV => {:PROMPT_I=>"#{current_app} #{rails_env} #{prompt} ", :PROMPT_N=>"#{current_app} #{rails_env} #{prompt} ", :PROMPT_S=>nil, :PROMPT_C=>"?> ", :RETURN=>"=> %s\n"}) 
IRB.conf[:PROMPT_MODE] = :RAILS_ENV

If I do something like:

current_app = "\e[31mfoo_bar_app\e[0m"
rails_env = "\e[32m#{RAILS_ENV}\e[0m"

then the prompt shows up beautifully colorized, but if I copy some text into my copybuffer and paste it, if I do page-up/page-down to go to the beginning/end of the current text entered, my cursor like jumps to the middle of the text for page-up, and for page-down it jumps way out to the right into an area of blank spaces where nothing had been typed, then my cursor position is totally screwed up.

Is there a way I can correct this? I would really like a colorized prompt.

Heptode answered 24/1, 2012 at 18:49 Comment(1)
which terminal-setup are you using? e.g. terminal emulator and shell?Veolaver
G
3

I'd be willing to bet this is similar to the question I asked in Colorized output breaks linewrapping with readline

Try this:

current_app = "\001\e[31mfoo_bar_app\e[0m\002"
rails_env = "\001\e[32m#{RAILS_ENV}\e[0m\002"

Basically, your prompt is not ignoring non-printing characters which causes weird things to happen.

Gatt answered 23/3, 2012 at 13:25 Comment(1)
Only the control codes which set the color should be surrounded by \001 and \002, not the text which is to be printed in color. Otherwise you have a problem in the other direction.Behest
M
0

Yes, only color code need to be in \001 and \002 like this

\001\e[37m\002[%~] #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} - r#{release}\001\e[0m\002production
Medication answered 13/11, 2014 at 19:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.