lib/hieracles/formats/console.rb in hieracles-0.2.0 vs lib/hieracles/formats/console.rb in hieracles-0.2.1

- old
+ new

@@ -1,5 +1,7 @@ +require 'awesome_print' + module Hieracles module Formats # format accepting colors # for display in the terminal class Console < Hieracles::Format @@ -12,28 +14,53 @@ "\e[34m%s\e[0m", "\e[35m%s\e[0m", "\e[36m%s\e[0m", "\e[37m%s\e[0m", "\e[38m%s\e[0m", - "\e[97m%s\e[0m" + "\e[97m%s\e[0m", + "\e[35;1m%s\e[0m" ] def initialize(node) @colors = {} super(node) end - def info(_) + def info(filter) + build_list(@node.info, @node.notifications, filter) + end + + def facts(filter) + build_list(@node.facts, @node.notifications, filter) + end + + def build_list(hash, notifications, filter) back = '' - length = max_key_length(@node.info) + 2 + 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") - @node.info.each do |k, v| + 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 back end + def build_notifications(notifications) + back = "\n" + notifications.each do |v| + back << format("#{COLORS[9]}\n", "*** #{v.source}: #{v.message} ***") + end + back << "\n" + back + end + def files(_) @node.files.join("\n") + "\n" end def paths(_) @@ -50,25 +77,31 @@ end def build_params_line(key, value, filter) output = '' if !filter || Regexp.new(filter).match(key) - first = value.pop - filecolor_index = @colors[first[:file]] - filecolor = COLORS[filecolor_index] - if is_merged? first - output << format("%s #{COLORS[5]} %s\n", "[-]", key, sanitize(first[:merged]) ) - output << format(" #{COLORS[8]}\n", "[#{filecolor_index}] #{key} #{sanitize(first[:value])}" ) - else - output << format("#{filecolor} #{COLORS[5]} %s\n", "[#{filecolor_index}]", key, sanitize(first[:value]) ) - end + output << build_first(key, value.pop) while value.count > 0 - overriden = value.pop - filecolor_index = @colors[overriden[:file]] - output << format(" #{COLORS[8]}\n", "[#{filecolor_index}] #{key} #{overriden[:value]}") + output << build_next(key, value.pop) end end output + end + + def build_first(key, first) + filecolor_index = @colors[first[:file]] + filecolor = COLORS[filecolor_index] + if is_merged? first + format("%s #{COLORS[5]} %s\n", "[-]", key, sanitize(first[:merged]) ) + + format(" #{COLORS[8]}\n", "[#{filecolor_index}] #{key} #{sanitize(first[:value])}" ) + else + format("#{filecolor} #{COLORS[5]} %s\n", "[#{filecolor_index}]", key, sanitize(first[:value]) ) + end + end + + def build_next(key, overriden) + filecolor_index = @colors[overriden[:file]] + format(" #{COLORS[8]}\n", "[#{filecolor_index}] #{key} #{overriden[:value]}") end def build_modules_line(key, value) length = max_key_length(@node.modules) + 3 value_color = '%s'