I'm working with colorizing some output using readline in Ruby, but I am not having any luck getting line wrapping to work properly. For example:
"\e[01;32mThis prompt is green and bold\e[00m > "
The desired result would be:
This prompt is green and bold > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
What I actually get is:
aaaaaaaaaaa is green and bold > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
If I remove the color codes, line wrapping works correctly. I know with bash, this can happen if the color codes are incorrectly terminated, but I have tried everything I can think of, including a few different gems, and the behavior is the same. It also occurs on multiple systems with different versions of Readline. This particular project is using rb-readline
as opposed to C readline
.
This problem is not ruby-specific - it occurs in bash too. If you put in a bash shell
you will see the same result as above. But if you put in
you will get the result you wanted.
Ok, sunkencity gets the check mark because I ended up using most of his solution, but I had to modify it as follows:
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".