Skip to content

Instantly share code, notes, and snippets.

@mmcdaris
Created November 13, 2012 00:41
Show Gist options
  • Save mmcdaris/4063105 to your computer and use it in GitHub Desktop.
Save mmcdaris/4063105 to your computer and use it in GitHub Desktop.
"string".style
class String
# extend String with Style!
STYLE = {'none' => 0,
'bold' => 1,
'underline' => 4,
'blink' => 5, # does not work if blinking text has been disabled
'hide' => 8,
'black' => 30,
'red' => 31,
'green' => 32,
'yellow' => 33,
'blue' => 34,
'magenta' => 35,
'cyan' => 36,
'white' => 37,
'bgred' => 41,
'bggreen' => 42,
'bgyellow' => 43,
'bgblue' => 44,
'bgmagenta' => 45,
'bgcyan' => 46,
'bgwhite' => 47}
def method_missing(name, *args, &block)
if STYLE.keys.include? "#{name}"
"\033[#{STYLE["#{name}"]}m#{self}\033[0m"
else
# someone called a method that has
# nothing to do with colors and doesn't exist
# call original method_missing
super
end
end
end
@mmcdaris
Copy link
Author

usage:

puts "Like a Boss".magenta

@shime
Copy link

shime commented Nov 13, 2012

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment