module AnsiColor
def self.colorize(text, color: nil, background: nil, style: nil)
def self.colorize(text, color: nil, background: nil, style: nil) codes = [] codes << COLORS[color] if color codes << BACKGROUNDS[background] if background codes << STYLES[style] if style raise ArgumentError, "No valid formatting options given" if codes.empty? "\e[#{codes.join(';')}m#{text}\e[0m" end
def self.demo
def self.demo puts "Text Colors:" COLORS.each_key do |color| puts colorize(" #{color.to_s.ljust(15)}", color: color) end puts "\nBackground Colors:" BACKGROUNDS.each_key do |bg| puts colorize(" #{bg.to_s.ljust(15)}", background: bg) end puts "\nStyles:" STYLES.each_key do |style| puts colorize(" #{style.to_s.ljust(15)}", color: :white, style: style) end puts "\nCombined Example:" puts colorize(" bold green on light_black ", color: :green, background: :light_black, style: :bold) end