Colorized output breaks linewrapping with readline

前端 未结 3 674
迷失自我
迷失自我 2020-12-16 03:52

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;         


        
3条回答
  •  温柔的废话
    2020-12-16 04:42

    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-8
    class 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" end
    end
    
    puts "foo\nbar".console_dark_red
    

提交回复
热议问题