Last active
December 22, 2015 20:29
-
-
Save jtomschroeder/6526723 to your computer and use it in GitHub Desktop.
Colored string output with ANSI escape sequences - http://ascii-table.com/ansi-escape-sequences.php
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
class String | |
def colorize(color_code) | |
"\e[#{color_code}m#{self}\e[0m" | |
end | |
color_escapes = {"black" => 30, "red" => 31, "green" => 32, "yellow" => 33, "blue" => 34, "magenta" => 35, "cyan" => 36, "white" => 37} | |
color_escapes.each do |key, value| | |
define_method(key) { colorize(value) } | |
end | |
color_escapes.each do |key, value| | |
define_method("bg_" + key) { colorize(value + 10) } | |
end | |
end | |
puts "Hello, World!".red | |
puts "Hello, World!".black.bg_white |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment