-
-
Save shime/4063122 to your computer and use it in GitHub Desktop.
used like puts color("yellow", "Morgan")
This file contains hidden or 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
# this file provides a method for formatting color using ascii escapes | |
def color(color, content) | |
style = {'clear' => 0, | |
'bold' => 1, 'underline' => 4, 'blink' => 5, '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} | |
if style[color] != nil | |
"\033[#{style[color]}m#{content}\033[0m" | |
else | |
content | |
end | |
end |
This file contains hidden or 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 nice colors! | |
COLORS = { | |
'clear' => 0, | |
'bold' => 1, | |
'underline' => 4, | |
'blink' => 5, | |
'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, &blk) | |
if COLORS.keys.include? name | |
# surround self with the funky stuff | |
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