ruby color output content to the console

When the console program output, in order to distinguish the severity of the output information, can be used colors , symbols , etc. to do identification.

ruby also supports setting color output content, such as running the following code:

The following is to Baidu, the discovery of many blog writing are the same, so the source but can not write. . .

puts "\033[1mBackground Colors...\033[0m\n"
puts "   \033[40m\033[37mBlack (40), White Text\033[0m\n"
puts "   \033[41mRed (41)\033[0m\n"
puts "   \033[42mGreen (42)\033[0m\n"
puts "   \033[43mYellow (43)\033[0m\n"
puts "   \033[44mBlue (44)\033[0m\n"
puts "   \033[45mMagenta (45)\033[0m\n"
puts "   \033[46mCyan (46)\033[0m\n"
puts "   \033[47mWhite (47)\033[0m\n"

Console to see this result:

We study the output of content: " \ 033 [47m White (47) \ 033 [0m \ the n- ", found that the focus is \ 033 [47m and \ 033 [0m \ the n- , the rest is the output.

Based on this principle, we can write the common invocation:

def puts_with_style(style_index, *contents)
  contents.each do |content|
    puts "\033[#{style_index}m#{content}\033[0m\n"
  end
  nil
end

Made under cyclic test calls, it supports up to 107 patterns, test run the following code:

108.times do |index|
  puts_with_style(index, index, "   color")
end

Guess this is probably rule the console output, try the next golang, I found that the way is also possible. I guess that python and other languages ​​as well, you can try try.

Guess you like

Origin www.cnblogs.com/linying1991/p/12021828.html