Module: Zashoku::Formatter

Defined in:
lib/core/formatter.rb

Constant Summary collapse

PAD =
' '
SPLIT =
'@split@'
BYTE_SUFFIXES =
{
  1_000_000_000_000 => 'T',
  1_000_000_000     => 'G',
  1_000_000         => 'M',
  1_000             => 'K',
  1                 => ''
}.freeze
STRIP_BRACKETS =
/\s*\[.+?\]\s*/
STRIP_EXT =
/(\.mkv)|(\.avi)|(\.mp4)/
STRIP_PARENS =
/\s*\(.+?\)\s*/

Class Method Summary collapse

Class Method Details

.duration(secs) ⇒ Object



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

def self.duration(secs)
  [
    secs >= 3_600 ? secs / 3_600 : nil,
    (secs / 60) % 60,
    secs % 60
  ].map do |i|
    i.round.to_s.rjust(2, '0') if i
  end.compact.join(":")
end

.format_line(string, obj) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/core/formatter.rb', line 112

def self.format_line(string, obj)
  pfs = pre_format(string, obj)
  len = pfs.reduce(0) { |l, s| l + Unicode::DisplayWidth.of(s[0]) } + 1
  buffer = pfs.length > 1 ? (Util::Term.cols - len) / (pfs.length - 1) : 0

  buffer -= (Util::Term.cols - len - buffer)

  pfs.reduce('') do |final, seg|
    buf = pfs.last != seg && buffer.positive? ? (seg[1] || PAD) * buffer : ''
    "#{final}#{seg[0]}#{buf}"
  end #[0..Util::Term.cols - 1]

  #f.chars.reduce('') do |m,v|
  #  m + (Unicode::DisplayWidth.of(m + v) <= Util::Term.cols ? v : '')
  #end
end

.items(format, items) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/core/formatter.rb', line 54

def self.items(format, items)
  items.map do |parent, children|
    [
      format_line(format['parent'], parent.attr),
      children&.map do |child|
        format_line(format['child'], child.attr)
      end || []
    ]
  end.to_h
end

.media_file(path) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/core/formatter.rb', line 46

def self.media_file(path)
   File.basename(path.to_s)
     .gsub(STRIP_BRACKETS) {}
     .gsub(STRIP_EXT, '')
     .tr('_', ' ')
     .gsub(STRIP_PARENS) {}
end

.pre_format(string, obj) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/core/formatter.rb', line 65

def self.pre_format(string, obj)
  split_chars = []
  mode        = :char
  padding     = 0

  string.chars.reduce('') do |str, char|
    str +
      case mode
      when :escape
        mode = :char
        char.to_s
      when :split
        mode = :char
        split_chars << char
        SPLIT
      when :attribute
        mode = :char
        str = obj.key?(char) ? obj[char].to_s : ''
        pad =
          if padding > Unicode::DisplayWidth.of(str)
            PAD * (padding - Unicode::DisplayWidth.of(str))
          else
            ''
          end
        padding = 0
        "#{pad}#{str}"
      when :char
        case char
        when '%'
          mode = :attribute
          ''
        when '@'
          mode = :split
          ''
        when '#'
          padding += 1
          ''
        when '\\'
          mode = :escape
          ''
        else
          char.to_s
        end
      end
  end.split(SPLIT).zip(split_chars)
end

.progress(progress) ⇒ Object



42
43
44
# File 'lib/core/formatter.rb', line 42

def self.progress(progress)
  "#{Integer(progress)}.#{Integer(progress * 10.0) % 10}"
end

.round(n, precision = 10.0) ⇒ Object



21
22
23
# File 'lib/core/formatter.rb', line 21

def self.round(n, precision = 10.0)
  (Float(n) * precision).round / precision
end

.size(bytes) ⇒ Object



35
36
37
38
39
40
# File 'lib/core/formatter.rb', line 35

def self.size(bytes)
  return '0B' if bytes < 1
  suffix = BYTE_SUFFIXES.select { |b, _s| bytes >= b }.max

  "#{round(Float(bytes) / suffix[0])}#{suffix[1]}B"
end