Class: Renamr::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/renamr/reporter.rb

Overview

Formats and prints output data. rubocop:disable Style/ClassVars

Constant Summary collapse

@@tim =
Timer.new
@@sta =
{ moved: 0, unaltered: 0, failed: 0 }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Reporter

Returns a new instance of Reporter.



25
26
27
28
# File 'lib/renamr/reporter.rb', line 25

def initialize(dir)
  @dir = dir
  @row = []
end

Class Method Details

.finalObject



63
64
65
66
67
# File 'lib/renamr/reporter.rb', line 63

def self.final
  msg = "#{@@act ? 'Real' : 'Test'}:#{stat_out} in #{@@tim.read}."
  msg = Utils.trim(msg, @@ttl)
  puts "| #{msg}#{' ' * (@@ttl - msg.length)} |\n+-#{'-' * @@ttl}-+"
end

.init(act, wid) ⇒ Object



18
19
20
21
22
23
# File 'lib/renamr/reporter.rb', line 18

def self.init(act, wid)
  @@act = act
  @@tbl = wid
  @@ttl = wid - 4
  @@str = (wid - 7) / 2
end

.stat_outObject



55
56
57
58
59
60
61
# File 'lib/renamr/reporter.rb', line 55

def self.stat_out
  out = ''
  @@sta.each do |k, v|
    out += " #{v} #{k}," if v.positive?
  end
  out.chop
end

Instance Method Details

#add(lhs, rhs) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/renamr/reporter.rb', line 30

def add(lhs, rhs)
  if rhs.is_a?(StandardError)
    tag = :failed
    rhs = "#{rhs.message} (#{rhs.class})"
  elsif rhs == ''
    tag = :unaltered
  else
    tag = :moved
  end
  @@sta[tag] += 1
  @row << [Utils.trim(lhs, @@str), Utils.trim(rhs, @@str)]
end

#doObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/renamr/reporter.rb', line 43

def do
  puts Terminal::Table.new(
    title: Utils.trim(@dir, @@ttl),
    headings: [
      { value: 'src', alignment: :center },
      { value: 'dst', alignment: :center }
    ],
    rows: @row,
    style: { width: @@tbl }
  )
end