Class: Transcode::Reporter

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

Overview

Formats and prints output data.

Instance Method Summary collapse

Constructor Details

#initialize(act, tit, wid) ⇒ Reporter

Returns a new instance of Reporter.



12
13
14
15
16
17
18
19
20
21
# File 'lib/transcode/reporter.rb', line 12

def initialize(act, tit, wid)
  @act = act
  @tit = tit
  @tbl = wid
  @ttl = @tbl - 4
  @str = (@tbl - 7) / 2
  @row = []
  @tim = Timer.new
  @sta = { converted: 0, failed: 0 }
end

Instance Method Details

#add(file, res, aud = 0, sub = 0, tit = 0) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/transcode/reporter.rb', line 23

def add(file, res, aud = 0, sub = 0, tit = 0)
  row = [Utils.trim(File.basename(file), @str)]
  if aud != 0 || sub != 0
    (row << [
      { value: aud, alignment: :right },
      { value: sub, alignment: :right },
      { value: tit, alignment: :right }
    ]).flatten!
  end
  @row << row
  @sta[res ? :converted : :failed] += 1
end

#doObject



57
58
59
60
61
# File 'lib/transcode/reporter.rb', line 57

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

#headObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/transcode/reporter.rb', line 36

def head
  head = [{ value: 'file', alignment: :center }]
  if @row.first.size == 4
    (head << [
      { value: 'audio', alignment: :center },
      { value: 'subtitles', alignment: :center },
      { value: 'titles', alignment: :center }
    ]).flatten!
  end
  head
end

#statObject



63
64
65
66
67
68
69
# File 'lib/transcode/reporter.rb', line 63

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

#tableObject



48
49
50
51
52
53
54
55
# File 'lib/transcode/reporter.rb', line 48

def table
  Terminal::Table.new(
    title: Utils.trim(@tit, @ttl),
    headings: head,
    rows: @row,
    style: { width: @tbl }
  )
end