Created
November 13, 2012 00:41
-
-
Save mmcdaris/4063105 to your computer and use it in GitHub Desktop.
"string".style
This file contains 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 | |
# 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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage: