Skip to content

Instantly share code, notes, and snippets.

@abhishekkr
Created September 3, 2012 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhishekkr/3610174 to your computer and use it in GitHub Desktop.
Save abhishekkr/3610174 to your computer and use it in GitHub Desktop.
Making Terminal Ruby Apps ~ have a colored terminal output adding to verbosity
# get colored terminal output using these String methods
class String
def colortxt(txt, term_color_code)
"\e[#{term_color_code}m#{txt}\e[0m"
end
def bold; colortxt(self, 1); end
def thin; colortxt(self, 2); end
def underline; colortxt(self, 4); end
def blink; colortxt(self, 5); end
def highlight; colortxt(self, 7); end
def hidden; colortxt(self, 8); end
def strikethrough; colortxt(self, 9); end
def black; colortxt(self, 30); end
def red; colortxt(self, 31); end
def green; colortxt(self, 32); end
def yellow; colortxt(self, 33); end
def blue; colortxt(self, 34); end
def magenta; colortxt(self, 35); end
def cyan; colortxt(self, 36); end
def white; colortxt(self, 37); end
def default; colortxt(self, 39); end
def black_bg; colortxt(self, 40); end
def red_bg; colortxt(self, 41); end
def green_bg; colortxt(self, 42); end
def yellow_bg; colortxt(self, 43); end
def blue_bg; colortxt(self, 44); end
def magenta_bg; colortxt(self, 45); end
def cyan_bg; colortxt(self, 46); end
def white_bg; colortxt(self, 47); end
def default_bg; colortxt(self, 49); end
def grey; colortxt(self, 90); end
def bright_red; colortxt(self, 91); end
def bright_green; colortxt(self, 92); end
def bright_yellow; colortxt(self, 93); end
def bright_blue; colortxt(self, 94); end
def bright_magenta; colortxt(self, 95); end
def bright_cyan; colortxt(self, 96); end
def bright_white; colortxt(self, 97); end
def grey_bg; colortxt(self, 100); end
def bright_red_bg; colortxt(self, 101); end
def bright_green_bg; colortxt(self, 102); end
def bright_yellow_bg; colortxt(self, 103); end
def bright_blue_bg; colortxt(self, 104); end
def bright_magent_bg; colortxt(self, 105); end
def bright_cyan_bg; colortxt(self, 106); end
def bright_white_bg; colortxt(self, 107); end
end
#!/usr/bin/env ruby
require_relative 'term_color.rb'
def term_color_demo
%w{bold thin underline blink highlight hidden strikethrough black red green yellow blue magenta cyan white default black_bg red_bg green_bg yellow_bg blue_bg magenta_bg cyan_bg white_bg default_bg grey bright_red bright_green bright_yellow bright_blue bright_magenta bright_cyan bright_white grey_bg bright_red_bg bright_green_bg bright_yellow_bg bright_blue_bg bright_magent_bg bright_cyan_bg bright_white_bg}.each do |term|
puts "#{term}".send term
end
puts "Bold Red".bold.red
puts "Underlined Thin Blue".underline.thin.blue
end
term_color_demo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment