class Prettys::Colorizer

Constants

CHROMATIC_COLOR_NAMES
COLORS
COLOR_NAMES

Public Instance Methods

colorize(options = {}) click to toggle source
# File lib/colorizer.rb, line 41
def colorize(options = {})
  options = add_default_options(options)
  unless [:background, :foreground].include?(options[:type])
    raise(ArgumentError, "Type must be a :background or :foreground")
  end
  marked_strings = Matcher.marked_strings(options[:string], options[:pattern])
  marked_strings.map do |ms| 
    if ms[:marked]
      escaped_string({
        string: ms[:string],
        pattern: options[:pattern],
        type: options[:type],
        bold: options[:bold],
        color: options[:color]
      })
    else
      ms[:string]
    end
  end.join
end
end_of_escape_sequence() click to toggle source
# File lib/colorizer.rb, line 33
def end_of_escape_sequence
  "\e[0m"
end
escape_sequence(options) click to toggle source
# File lib/colorizer.rb, line 26
def escape_sequence(options)
  options = add_default_options(options)
  color_code = COLORS[options[:color]]
  color_code += 10 if options[:type] == :background
  "\e[#{color_code.to_s};#{(options[:bold] ? 1 : 2).to_s}m"
end
escaped_string(options) click to toggle source
# File lib/colorizer.rb, line 37
def escaped_string(options)
  escape_sequence(options) + options[:string] + end_of_escape_sequence
end

Private Instance Methods

add_default_options(options) click to toggle source
# File lib/colorizer.rb, line 64
def add_default_options(options)
  options[:type] = :foreground unless options[:type]
  options[:bold] = false unless options[:bold]
  options[:color] = COLOR_NAMES[1] unless options[:color]
  return options
end