Skip to content

Instantly share code, notes, and snippets.

@MichaelEvans
Forked from nhocki/colors.rb
Created February 11, 2014 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelEvans/8942167 to your computer and use it in GitHub Desktop.
Save MichaelEvans/8942167 to your computer and use it in GitHub Desktop.
module Colors
def colorize(text, color_code)
"\033[#{color_code}m#{text}\033[0m"
end
{
:black => 30,
:red => 31,
:green => 32,
:yellow => 33,
:blue => 34,
:magenta => 35,
:cyan => 36,
:white => 37
}.each do |key, color_code|
define_method key do |text|
colorize(text, color_code)
end
end
end
# green "Hello"
# => "\e[32mHello\e[0m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment