Skip to content

Instantly share code, notes, and snippets.

@jtomschroeder
Last active December 22, 2015 20:29
Show Gist options
  • Save jtomschroeder/6526723 to your computer and use it in GitHub Desktop.
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
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