class Hieracles::Formats::Console

format accepting colors for display in the terminal

Constants

COLORS

Public Class Methods

new(node) click to toggle source
Calls superclass method Hieracles::Format.new
# File lib/hieracles/formats/console.rb, line 23
def initialize(node)
  @colors = {}
  super(node)
end

Public Instance Methods

build_head(without_common) click to toggle source
# File lib/hieracles/formats/console.rb, line 76
def build_head(without_common)
  output = "[-] (merged)\n"
  @node.files(without_common).each_with_index do |f, i|
    output << format("#{COLORS[i]}\n", "[#{i}] #{f}")
    @colors[f] = i
  end
  "#{output}\n"
end
build_line_item(key, value) click to toggle source
# File lib/hieracles/formats/console.rb, line 93
def build_line_item(key, value)
  if value[:overriden]
    format("%s #{COLORS[7]} %s\n", "[-]", key, sanitize(value[:value]) ) +
    build_overriden(key, value[:found_in])
  else
    filecolor_index = @colors[value[:file]]
    filecolor = COLORS[filecolor_index]
    format("#{filecolor} #{COLORS[7]} %s\n", "[#{filecolor_index}]", key, sanitize(value[:value]) )
  end
end
build_list(hash, notifications, filter) click to toggle source
# File lib/hieracles/formats/console.rb, line 36
def build_list(hash, notifications, filter)
  back = ''
  if hash.class.name == 'Array'
    hash.each do |v|
      back << "#{v}\n"
    end
  else
    back << build_notifications(notifications) if notifications
    if filter[0]
      hash.select! { |k, v| Regexp.new(filter[0]).match(k.to_s) }
    end
    length = max_key_length(hash) + 2
    title = format(COLORS[8], "%-#{length}s")
    hash.each do |k, v|
      if v.class.name == 'Hash' || v.class.name == 'Array'
        v = v.ai({ indent: 10, raw: true}).strip
      end
      back << format("#{title} %s\n", k, v)
    end
  end
  back
end
build_modules_line(key, value) click to toggle source
# File lib/hieracles/formats/console.rb, line 113
def build_modules_line(key, value)
  length = max_key_length(@node.modules) + 3
  value_color = '%s'
  value_color = COLORS[0] if /not found/i.match value
  value_color = COLORS[2] if /\(duplicate\)/i.match value
  format("%-#{length}s #{value_color}\n", key, value)
end
build_notifications(notifications) click to toggle source
# File lib/hieracles/formats/console.rb, line 59
def build_notifications(notifications)
  back = "\n"
  notifications.each do |v|
    back << format("#{COLORS[9]}\n", "*** #{v.source}: #{v.message} ***")
  end
  back << "\n"
  back
end
build_overriden(key, found_in) click to toggle source
# File lib/hieracles/formats/console.rb, line 104
def build_overriden(key, found_in)
  back = ''
  found_in.each do |v|
    filecolor_index = @colors[v[:file]]
    back << format("    #{COLORS[8]}\n", "[#{filecolor_index}] #{key} #{v[:value]}")
  end
  back
end
build_params_line(key, value, filter) click to toggle source
# File lib/hieracles/formats/console.rb, line 85
def build_params_line(key, value, filter)
  output = ''
  if !filter || Regexp.new(filter).match(key)
    output << build_line_item(key, value)
  end
  output
end
facts(filter) click to toggle source
# File lib/hieracles/formats/console.rb, line 32
def facts(filter)
  build_list(@node.facts, @node.notifications, filter)
end
files(_) click to toggle source
# File lib/hieracles/formats/console.rb, line 68
def files(_)
  @node.files.join("\n") + "\n"
end
info(filter) click to toggle source
# File lib/hieracles/formats/console.rb, line 28
def info(filter)
  build_list(@node.info, @node.notifications, filter)
end
paths(_) click to toggle source
# File lib/hieracles/formats/console.rb, line 72
def paths(_)
  @node.paths.join("\n") + "\n"
end