Class: Renamr::Reporter

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

Overview

Formats and prints output data.

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.



23
24
25
26
# File 'lib/renamr/reporter.rb', line 23

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

Class Method Details

.finalObject



61
62
63
64
65
# File 'lib/renamr/reporter.rb', line 61

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



16
17
18
19
20
21
# File 'lib/renamr/reporter.rb', line 16

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

.stat_outObject



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

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

Instance Method Details

#add(lhs, rhs) ⇒ Object



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

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



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

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