module Zashoku::Formatter
Constants
- BYTE_SUFFIXES
- PAD
- SPLIT
- STRIP_BRACKETS
- STRIP_EXT
- STRIP_PARENS
Public Class Methods
duration(secs)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/core/formatter.rb, line 42 def self.progress(progress) "#{Integer(progress)}.#{Integer(progress * 10.0) % 10}" end
round(n, precision = 10.0)
click to toggle source
# File lib/core/formatter.rb, line 21 def self.round(n, precision = 10.0) (Float(n) * precision).round / precision end
size(bytes)
click to toggle source
# 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