Skip to content

Instantly share code, notes, and snippets.

@vvalgis
Created August 24, 2012 01:17
Show Gist options
  • Save vvalgis/3444393 to your computer and use it in GitHub Desktop.
Save vvalgis/3444393 to your computer and use it in GitHub Desktop.
String mixin for console color output
<<COLORS
black='\033[0;30m'
BLACK='\033[1;30m'
red='\033[0;31m'
RED='\033[1;31m'
green='\033[0;32m'
GREEN='\033[1;32m'
yellow='\033[0;33m'
YELLOW='\033[1;33m'
blue='\033[0;34m'
BLUE='\033[1;34m'
magenta='\033[0;35m'
MAGENTA='\033[1;35m'
cyan='\033[0;36m'
CYAN='\033[1;36m'
white='\033[0;37m'
WHITE='\033[1;37m'
NC='\033[0m'
COLORS
module ConsoleColors
def bold; @bold = 1; self; end
def normal; @bold = 0; self ; end
%w(black red green yellow blue magenta cyan white).each_with_index {|name, i| define_method("in_#{name}".to_sym) { ("\033[#{(@bold ||= 0)};3#{i}m" << self << "\033[0m") } }
end
class String
include ConsoleColors
end
'Color output in red'.in_red
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment