Colorized output breaks linewrapping with readline
Ok, sunkencity gets the check mark because I ended up using most of his solution, but I had to modify it as follows:
# encoding: utf-8class String def console_red; colorize(self, "\001\e[1m\e[31m\002"); end def console_dark_red; colorize(self, "\001\e[31m\002"); end def console_green; colorize(self, "\001\e[1m\e[32m\002"); end def console_dark_green; colorize(self, "\001\e[32m\002"); end def console_yellow; colorize(self, "\001\e[1m\e[33m\002"); end def console_dark_yellow; colorize(self, "\001\e[33m\002"); end def console_blue; colorize(self, "\001\e[1m\e[34m\002"); end def console_dark_blue; colorize(self, "\001\e[34m\002"); end def console_purple; colorize(self, "\001\e[1m\e[35m\002"); end def console_def; colorize(self, "\001\e[1m\002"); end def console_bold; colorize(self, "\001\e[1m\002"); end def console_blink; colorize(self, "\001\e[5m\002"); end def colorize(text, color_code) "#{color_code}#{text}\001\e[0m\002" endend
Each sequence needs to be wrapped in \001..\002 so that Readline knows to ignore non printing characters.
I always throw this string extension in when I need to colorize strings for console. The problem in your code seems to be the terminator, there should be just one zero "\e[0m".
# encoding: utf-8class String def console_red; colorize(self, "\e[1m\e[31m"); end def console_dark_red; colorize(self, "\e[31m"); end def console_green; colorize(self, "\e[1m\e[32m"); end def console_dark_green; colorize(self, "\e[32m"); end def console_yellow; colorize(self, "\e[1m\e[33m"); end def console_dark_yellow; colorize(self, "\e[33m"); end def console_blue; colorize(self, "\e[1m\e[34m"); end def console_dark_blue; colorize(self, "\e[34m"); end def console_purple; colorize(self, "\e[1m\e[35m"); end def console_def; colorize(self, "\e[1m"); end def console_bold; colorize(self, "\e[1m"); end def console_blink; colorize(self, "\e[5m"); end def colorize(text, color_code) "#{color_code}#{text}\e[0m" endendputs "foo\nbar".console_dark_red
This problem is not ruby-specific - it occurs in bash too. If you put in a bash shell
PS1="\e[01;32mThis prompt is green and bold\e[00m > "
you will see the same result as above. But if you put in
PS1="\[\e[01;32m\]This prompt is green and bold\[\e[00m\] > "
you will get the result you wanted.