Created
May 30, 2010 06:22
-
-
Save rkumar/418831 to your computer and use it in GitHub Desktop.
color.rb - coloring on terminal or console
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby -w | |
| # A module that helps in printing colored output onto the console. | |
| # Preferable to use highline gem. For small usage, you can just cut | |
| # paste this module into your program and include it. | |
| # Most of this is stolen from the highline project. | |
| module Color | |
| CLEAR = "\e[0m" | |
| # An alias for CLEAR. | |
| RESET = CLEAR | |
| # | |
| # I create a color map with foreground and background colors. | |
| # Background color names are on_black, on_blue etc | |
| Colormap = {} | |
| # populate foreground color ansi codes | |
| %w[black red green yellow blue magenta cyan white].each_with_index {|c,i| Colormap[c] = "\e[3#{i}m" } | |
| # populate background color ansi codes | |
| %w[black red green yellow blue magenta cyan white].each_with_index {|c,i| Colormap["on_#{c}"] = "\e[4#{i}m" } | |
| # populate attribute color ansi codes | |
| %w[clear bold dark XXX underline blink YYY reverse concealed].each_with_index {|c,i| Colormap[c] = "\e[#{i}m"} | |
| ## | |
| # this prints given string with given colors/attributes, clearing at end | |
| # @examples | |
| # For example: | |
| # print color("Hello there black on yellow\n", "black", "on_yellow" ) | |
| # print color("Hello there black reverse on yellow\n", "black", "on_yellow", "reverse") | |
| # print color("Hello there black bold on yellow\n", "black", "on_yellow", "bold") | |
| def color(string, *colors) | |
| colors.map! do |e| | |
| Colormap[e] | |
| end | |
| "#{colors.flatten.join}#{string}#{CLEAR}" | |
| end | |
| ## | |
| # print text with no color. | |
| def nocolor(text) | |
| "\x1b[0m#{text}" | |
| end | |
| end # end of module | |
| if __FILE__ == $0 | |
| include Color | |
| print "Importing categories " | |
| print color "FAILED\n", "red", 'underline' | |
| print "Importing tags " | |
| print color "DONE\n","green", 'bold' | |
| text = "hellow" | |
| %w[black red green yellow blue magenta cyan white].each_with_index {|c,i| | |
| print color " #{c} #{i} text\n ", c | |
| } | |
| print "STARTING COLORS NOW\n" | |
| print color("Hello there red\n", "red") | |
| print color("Hello there white\n ", "white") | |
| print color("Hello there white bold\n", "white", "bold") | |
| print color("Hello there white reverse\n", "white", "reverse") | |
| print color("Hello there black on yellow\n", "black", "on_yellow" ) | |
| print color("Hello there black reverse on yellow\n", "black", "on_yellow", "reverse") | |
| print color("Hello there black bold on yellow\n", "black", "on_yellow", "bold") | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment