Class: ElchScan::Formatter::Plain

Inherits:
Base
  • Object
show all
Defined in:
lib/elch_scan/formatter/plain.rb

Instance Method Summary collapse

Methods inherited from Base

#c, #initialize, #logger

Constructor Details

This class inherits a constructor from ElchScan::Formatter::Base

Instance Method Details

#colorize_name(str) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/elch_scan/formatter/plain.rb', line 25

def colorize_name str
  m = str.match(/(.*?)(?:\s*)(\(.*?\))(?:\s*)(\[[0-9]+\])?(\s*)?/)
  return str if !m
  c("#{m[1]}").tap do |r|
    r << m[4] if m[4]
    r << c(" #{m[2]}", :blue) if m[2]
    r << c(" #{m[3]}", :magenta) if m[3]
  end
end

#format(results) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/elch_scan/formatter/plain.rb', line 4

def format results
  return ["no results"] if results.empty?
  # name, resolution, languages
  table = [[], [], [], []]

  results.each do |name, movie|
    table[0] << name
    table[1] << (movie.source.resolution rescue "?")
    table[2] << (movie.nfo! do |n|
      n["fileinfo"][0]["streamdetails"][0]["audio"].map do |s|
        s.try(:[], "language").try(:[], 0)
      end.reject(&:nil?)
    end.presence || [""]).join(", ")

    size = movie.source.size rescue nil
    table[3] << (size ? ActiveSupport::NumberHelper::NumberToHumanSizeConverter.convert(size, {}) : "?")
  end

  [nil] + render_table(table, ["Name", "Resolution", "Audio languages", "Movie size"])
end

#render_table(table, headers = []) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/elch_scan/formatter/plain.rb', line 35

def render_table table, headers = []
  [].tap do |r|
    col_sizes = table.map{|col| col.map(&:to_s).map(&:length).max }
    headers.map(&:length).each_with_index do |length, header|
      col_sizes[header] = [col_sizes[header], length].max
    end

    # header
    if headers.any?
      r << [].tap do |line|
        col_sizes.count.times do |col|
          line << "#{c headers[col].ljust(col_sizes[col])}"
        end
      end.join(c " | ", :red)
      r << c("".ljust(col_sizes.sum + ((col_sizes.count - 1) * 3), "-"), :red)
    end

    # prettify movie sizes
    table[3].map! do |size|
      size = size.ljust(col_sizes[3])
      m = size.match(/(.*?) ([^\s]*)(\s*)/)
      color = case(m[2])
        when "B", "KB" then :magenta
        when "MB" then :green
        else :red
      end
      c("#{m[3]}#{m[1]} #{m[2]}", color)
    end

    # records
    table[0].count.times do |row|
      r << [].tap do |line|
        col_sizes.count.times do |col|
          line << "#{c colorize_name("#{table[col][row]}".encode!('UTF-8','UTF8-MAC').ljust(col_sizes[col]))}"
        end
      end.join(c " | ", :red)
    end
  end
end