Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

irb

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.

like image 619
patrick Avatar asked Jan 24 '12 18:01

patrick


1 Answers

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.

like image 133
Eugene Avatar answered Nov 04 '22 01:11

Eugene