Skip to content

Instantly share code, notes, and snippets.

@shime
Forked from mmcdaris/colors.rb
Created November 13, 2012 00:48
Show Gist options
  • Save shime/4063122 to your computer and use it in GitHub Desktop.
Save shime/4063122 to your computer and use it in GitHub Desktop.
used like puts color("yellow", "Morgan")
# 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
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