ku1ik / rainbow

Ruby gem for colorizing printed text on ANSI terminals
MIT License
813 stars 68 forks source link

Doesn't seem to work with printf #1

Closed stewartjohnson closed 14 years ago

stewartjohnson commented 14 years ago

When I'm using rainbow to add colour to strings that are displayed with printf, it seems to mess up the alignments. Example using irb below. Is there anything I can do to work around this?

stewart@host:~$ irb
ruby-1.9.2-head > require "rainbow" 
 => true
ruby-1.9.2-head > printf("%20s\n", "foo")
                 foo # this is indented by 17 chars (correctly)
 => nil
ruby-1.9.2-head > printf("%20s\n", "foo".foreground(:red))
        foo # this is coloured, but only indented by 8 chars
 => nil 
ruby-1.9.2-head > printf("%20s\n", "foo".foreground(:red).background(:white))
   foo # this is coloured, but it's only indented by 3 chars
 => nil
ku1ik commented 14 years ago

I don't think there is anything I can change in rainbow to resolve this issue. Looks like printf is counting also ANSI escape codes as normal characters when doing alignment.

The only solution I can think of now is to monkey-patch Kernel#printf, but this sounds like a dangerous thing to do. I won't do this in rainbow, but If you're brave you can try it by yourself :)