Class: Formatter

Inherits:
Object
  • Object
show all
Includes:
ColorText, Constants
Defined in:
lib/log_parser/formatter.rb

Overview

Formats text for output

Constant Summary

Constants included from Constants

Constants::DEFAULT_LOG, Constants::DEFAULT_OPTIONS, Constants::DESCRIPTORS, Constants::INFO_TITLES, Constants::LOG_WARNINGS, Constants::OPTION_DESCRIPTIONS, Constants::OUTPUT_COLORS, Constants::VALIDATION_NAMES, Constants::VALID_ADDRESS, Constants::VALID_IP4, Constants::VALID_IP6, Constants::VALID_LOG, Constants::VALID_PATH, Constants::WARNINGS_JSON, Constants::WARNING_COLORS

Constants included from ColorText

ColorText::COLOR_CODE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ColorText

#colorize, #colorize_if

Constructor Details

#initializeFormatter

Returns a new instance of Formatter.



10
11
12
# File 'lib/log_parser/formatter.rb', line 10

def initialize
  @output = []
end

Instance Attribute Details

#outputObject

Returns the value of attribute output.



8
9
10
# File 'lib/log_parser/formatter.rb', line 8

def output
  @output
end

Instance Method Details

#add_descriptor(item, descriptor) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/log_parser/formatter.rb', line 29

def add_descriptor(item, descriptor)
  if item.is_a?(Integer) && (descriptor != '')
    format('%<item>s %<descriptor>s', item: item,
                                      descriptor: pluralise(descriptor, item))
  else
    item
  end
end

#add_row(row:, descriptor:, add_color: false) ⇒ Object



22
23
24
25
26
27
# File 'lib/log_parser/formatter.rb', line 22

def add_row(row:, descriptor:, add_color: false)
  output.push(row.map.with_index { |item, i|
    colorize_if(add_descriptor(item, descriptor[i]),
                OUTPUT_COLORS[:columns][i], add_color)
  }.join(' '))
end

#add_title(title:, add_color: false) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/log_parser/formatter.rb', line 14

def add_title(title:, add_color: false)
  output.push(colorize_if(title, OUTPUT_COLORS[:title], add_color))
  line_break = colorize_if(('-' * title.length),
                           OUTPUT_COLORS[:line_break], add_color)
  output.unshift(line_break)
  output.push(line_break)
end

#format_full_warnings(warnings:, add_color: false) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/log_parser/formatter.rb', line 74

def format_full_warnings(warnings:, add_color: false)
  warnings_list = warnings.map do |_type, info|
    if info[:warnings].empty?
      color = WARNING_COLORS[:none]
      text = [warning_summary(info[:name], info[:warnings].length)]
    else
      color = WARNING_COLORS[info[:important]]
      text = [warning_summary(info[:name], info[:warnings].length),
              info[:warnings]]
    end
    colorize_if(text.join("\n"), color, add_color)
  end
  warnings_list.flatten.join("\n")
end

#format_info(view_info:, add_color: false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/log_parser/formatter.rb', line 38

def format_info(view_info:, add_color: false)
  @output = []
  add_title(title: view_info[:title], add_color: add_color)
  view_info[:info].sort_by { |page, views| [-views, page] }
                  .each do |row|
                    add_row(row: row,
                            descriptor: view_info[:descriptor],
                            add_color: add_color)
                  end
  @output
end

#format_log_info(log_info:, add_color: false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/log_parser/formatter.rb', line 50

def format_log_info(log_info:, add_color: false)
  files = log_info[:files_read].map { |file| File.absolute_path(file) }
                               .join(",\n")
  format("\n%<file_info>s\n%<logs_read>s\n%<logs_added>s",
         file_info: colorize_if("Files read: #{files}",
                                OUTPUT_COLORS[:log], add_color),
         logs_read: colorize_if("Logs read: #{log_info[:logs_read]}",
                                OUTPUT_COLORS[:log], add_color),
         logs_added: colorize_if("Logs added: #{log_info[:logs_added]}",
                                 OUTPUT_COLORS[:log], add_color))
end

#format_minimal_warnings(warnings:, add_color: false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/log_parser/formatter.rb', line 62

def format_minimal_warnings(warnings:, add_color: false)
  warnings_list = warnings.filter { |_type, info|
    info[:important] && !info[:warnings].empty?
  }.map { |_type, info|
    color = WARNING_COLORS[true]
    text = [warning_summary(info[:name], info[:warnings].length),
            info[:warnings]].join("\n")
    colorize_if(text, color, add_color)
  }
  warnings_list.flatten.join("\n")
end

#format_normal_warnings(warnings:, add_color: false) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/log_parser/formatter.rb', line 89

def format_normal_warnings(warnings:, add_color: false)
  warnings_list = warnings.map do |_type, info|
    if !info[:warnings].empty? && info[:important]
      color = WARNING_COLORS[true]
      text = [warning_summary(info[:name], info[:warnings].length),
              info[:warnings]].join("\n")
    elsif info[:important]
      color = WARNING_COLORS[:none]
      text = [warning_summary(info[:name], info[:warnings].length)].join("\n")
    else
      color_key = info[:warnings].empty? ? :none : false
      color = WARNING_COLORS[color_key]
      text = [warning_summary(info[:name], info[:warnings].length)].join("\n")
    end
    colorize_if(text, color, add_color)
  end
  warnings_list.flatten.join("\n")
end

#format_options(options:, add_color: false) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/log_parser/formatter.rb', line 117

def format_options(options:, add_color: false)
  options = OPTION_DESCRIPTIONS.map do |k, _v|
    OPTION_DESCRIPTIONS[k].call(options[k])
  end
  text = format("\nOptions selected:\n\n%<options>s\n",
                options: options.join(",\n"))
  colorize_if(text, OUTPUT_COLORS[:options], add_color)
end

#pluralise(item, number) ⇒ Object



108
109
110
# File 'lib/log_parser/formatter.rb', line 108

def pluralise(item, number)
  item + (number != 1 ? 's' : '')
end

#warning_summary(name, number) ⇒ Object



112
113
114
115
# File 'lib/log_parser/formatter.rb', line 112

def warning_summary(name, number)
  format('%<name>ss: %<number>d %<warnings>s',
         name: name, number: number, warnings: pluralise('warning', number))
end