class String

Monkey-Patch String

Extending Ruby's String type to allow colorization via ANSI control codes

Constants

ANSI_COLORS
ANSI_MODES

Public Instance Methods

colorize(fg = :normal, *options) click to toggle source
# File lib/gyro/log.rb, line 54
def colorize(fg = :normal, *options)
  fg_idx = ANSI_COLORS.index(fg)
  fg_code = fg_idx.nil? ? ANSI_MODES.index(fg_idx) : 30 + fg_idx
  other_codes = options.map do |opt|
    bg = ANSI_COLORS.index(opt)
    bg.nil? ? ANSI_MODES.index(opt) : 40 + bg
  end
  codes = [fg_code, *other_codes].compact.join(';')
  "\e[#{codes}m#{self}\e[0m"
end