brandonblack
5/24/2017 - 12:27 AM

Because nobody likes monkey patches.

Because nobody likes monkey patches.

# Description: chainable color classes that don't hack the string class
#
#  Example:
#    ColorString.new('foo').bold.red.underline
#
class ColorString < String
  def black;         self.class.new("\e[30m#{self}\e[0m") end
  def red;           self.class.new("\e[31m#{self}\e[0m") end
  def green;         self.class.new("\e[32m#{self}\e[0m") end
  def brown;         self.class.new("\e[33m#{self}\e[0m") end
  def blue;          self.class.new("\e[34m#{self}\e[0m") end
  def magenta;       self.class.new("\e[35m#{self}\e[0m") end
  def cyan;          self.class.new("\e[36m#{self}\e[0m") end
  def gray;          self.class.new("\e[37m#{self}\e[0m") end

  def bg_black;      self.class.new("\e[40m#{self}\e[0m") end
  def bg_red;        self.class.new("\e[41m#{self}\e[0m") end
  def bg_green;      self.class.new("\e[42m#{self}\e[0m") end
  def bg_brown;      self.class.new("\e[43m#{self}\e[0m") end
  def bg_blue;       self.class.new("\e[44m#{self}\e[0m") end
  def bg_magenta;    self.class.new("\e[45m#{self}\e[0m") end
  def bg_cyan;       self.class.new("\e[46m#{self}\e[0m") end
  def bg_gray;       self.class.new("\e[47m#{self}\e[0m") end

  def bold;          self.class.new("\e[1m#{self}\e[22m") end
  def italic;        self.class.new("\e[3m#{self}\e[23m") end
  def underline;     self.class.new("\e[4m#{self}\e[24m") end
  def blink;         self.class.new("\e[5m#{self}\e[25m") end
  def reverse_color; self.class.new("\e[7m#{self}\e[27m") end
end