Skip to content

Instantly share code, notes, and snippets.

@hawx
Created December 16, 2010 17:24
Show Gist options
  • Save hawx/743687 to your computer and use it in GitHub Desktop.
Save hawx/743687 to your computer and use it in GitHub Desktop.
Add colour to your output.
# Usage:
#
# puts green("I'm green")
# puts red("I'm red")
# puts bold("I'm bold")
#
def colour(text, code)
"#{code}#{text}\e[0m"
end
{
"bold" => "1",
"underline" => "4",
"blink" => "5",
"white" => "37",
"green" => "32",
"red" => "31",
"magenta" => "35",
"yellow" => "33",
"blue" => "34",
"grey" => "90"
}.each do |name, code|
define_method(name) do |text|
colour(text, "\e[#{code}m")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment