module GitCompound::Logger::Colors

Colors module

Public Class Methods

included(parent_class) click to toggle source
# File lib/git_compound/logger/colors.rb, line 6
def self.included(parent_class)
  parent_class.extend ClassMethods
  parent_class.class_eval { create_color_methods }
end

Public Instance Methods

colorize(params) click to toggle source
# File lib/git_compound/logger/colors.rb, line 11
def colorize(params)
  return self if colors_unavailable?
  scan_colors.inject('') do |str, match|
    set_colors(match, params)
    str << "\033[#{match[0]};#{match[1]};#{match[2]}m#{match[3]}\033[0m"
  end
end

Private Instance Methods

bgcolor(color) click to toggle source
# File lib/git_compound/logger/colors.rb, line 57
def bgcolor(color)
  self.class.colors[color] + 40 if self.class.colors[color]
end
color(color) click to toggle source
# File lib/git_compound/logger/colors.rb, line 53
def color(color)
  self.class.colors[color] + 30 if self.class.colors[color]
end
colors_unavailable?() click to toggle source
# File lib/git_compound/logger/colors.rb, line 21
def colors_unavailable?
  self.class.disable_colors || RUBY_VERSION < '2.0.0' || RUBY_PLATFORM =~ /win32/
end
default_colors(match) click to toggle source
# File lib/git_compound/logger/colors.rb, line 47
def default_colors(match)
  match[0] ||= mode(:default)
  match[1] ||= color(:default)
  match[2] ||= bgcolor(:default)
end
mode(mode) click to toggle source
# File lib/git_compound/logger/colors.rb, line 61
def mode(mode)
  self.class.modes[mode] if self.class.modes[mode]
end
scan_colors() click to toggle source
# File lib/git_compound/logger/colors.rb, line 25
def scan_colors
  scan(/\033\[([0-9;]+)m(.+?)\033\[0m|([^\033]+)/m).map do |match|
    colors = (match[0] || '').split(';')
    Array.new(4).tap do |array|
      array[0], array[1], array[2] = colors if colors.length == 3
      array[3] = match[1] || match[2]
    end
  end
end
set_colors(match, params) click to toggle source
# File lib/git_compound/logger/colors.rb, line 35
def set_colors(match, params)
  default_colors(match)

  mode = mode(params[:mode])
  color = color(params[:color])
  bgcolor = bgcolor(params[:bgcolor])

  match[0] = mode    if mode
  match[1] = color   if color
  match[2] = bgcolor if bgcolor
end