Class: Hieracles::Formats::Console

Inherits:
Hieracles::Format show all
Includes:
Utils
Defined in:
lib/hieracles/formats/console.rb

Overview

format accepting colors for display in the terminal

Constant Summary collapse

COLORS =
[
  "\e[31m%s\e[0m",
  "\e[32m%s\e[0m",
  "\e[33m%s\e[0m",
  "\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[35;1m%s\e[0m"
]

Instance Method Summary collapse

Methods included from Utils

#deep_sort, #local_merge!, #max_key_length, #sym_keys, #to_deep_hash, #to_shallow_hash

Methods inherited from Hieracles::Format

#allparams, #modules, #params, #show_params

Constructor Details

#initialize(node) ⇒ Console



23
24
25
26
# File 'lib/hieracles/formats/console.rb', line 23

def initialize(node)
  @colors = {}
  super(node)
end

Instance Method Details

#build_first(key, first) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/hieracles/formats/console.rb', line 90

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

#build_head(without_common) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/hieracles/formats/console.rb', line 70

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_list(hash, notifications, filter) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hieracles/formats/console.rb', line 36

def build_list(hash, notifications, filter)
  back = ''
  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
  back
end

#build_modules_line(key, value) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/hieracles/formats/console.rb', line 106

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_next(key, overriden) ⇒ Object



101
102
103
104
# File 'lib/hieracles/formats/console.rb', line 101

def build_next(key, overriden)
  filecolor_index = @colors[overriden[:file]]
  format("    #{COLORS[8]}\n", "[#{filecolor_index}] #{key} #{overriden[:value]}")
end

#build_notifications(notifications) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/hieracles/formats/console.rb', line 53

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_params_line(key, value, filter) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/hieracles/formats/console.rb', line 79

def build_params_line(key, value, filter)
  output = ''
  if !filter || Regexp.new(filter).match(key)
    output << build_first(key, value.pop)
    while value.count > 0
      output << build_next(key, value.pop)
    end
  end
  output
end

#facts(filter) ⇒ Object



32
33
34
# File 'lib/hieracles/formats/console.rb', line 32

def facts(filter)
  build_list(@node.facts, @node.notifications, filter)
end

#files(_) ⇒ Object



62
63
64
# File 'lib/hieracles/formats/console.rb', line 62

def files(_)
  @node.files.join("\n") + "\n"
end

#info(filter) ⇒ Object



28
29
30
# File 'lib/hieracles/formats/console.rb', line 28

def info(filter)
  build_list(@node.info, @node.notifications, filter)
end

#paths(_) ⇒ Object



66
67
68
# File 'lib/hieracles/formats/console.rb', line 66

def paths(_)
  @node.paths.join("\n") + "\n"
end