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"
]

Instance Method Summary collapse

Methods included from Utils

#deep_merge!, #deep_sort, #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

Returns a new instance of Console.



20
21
22
23
# File 'lib/hieracles/formats/console.rb', line 20

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

Instance Method Details

#build_head(without_common) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/hieracles/formats/console.rb', line 43

def build_head(without_common)
  output = ''
  @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_modules_line(key, value) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/hieracles/formats/console.rb', line 74

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hieracles/formats/console.rb', line 52

def build_params_line(key, value, filter)
  output = ''
  if !filter || Regexp.new(filter).match(key)
    first = value.shift
    filecolor_index = @colors[first[:file]]
    filecolor = COLORS[filecolor_index]
    output << format("#{filecolor} #{COLORS[5]} %s\n",
                     "[#{filecolor_index}]",
                      key,
                      first[:value].to_s.gsub('%', '%%')
                    )
    while value.count > 0
      overriden = value.shift
      filecolor_index = @colors[overriden[:file]]
      output << format("    #{COLORS[8]}\n",
                       "[#{filecolor_index}] #{key} #{overriden[:value]}"
                      )
    end
  end
  output
end

#files(_) ⇒ Object



35
36
37
# File 'lib/hieracles/formats/console.rb', line 35

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

#info(_) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/hieracles/formats/console.rb', line 25

def info(_)
  back = ''
  length = max_key_length(@node.info) + 2
  title = format(COLORS[8], "%-#{length}s")
  @node.info.each do |k, v|
    back << format("#{title} %s\n", k, v)
  end
  back
end

#paths(_) ⇒ Object



39
40
41
# File 'lib/hieracles/formats/console.rb', line 39

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