lib/haveapi/cli/output_formatter.rb in haveapi-client-0.20.0 vs lib/haveapi/cli/output_formatter.rb in haveapi-client-0.21.0
- old
+ new
@@ -1,14 +1,14 @@
module HaveAPI::CLI
class OutputFormatter
- def self.format(*args)
- f = new(*args)
- f.format
+ def self.to_s(*)
+ f = new(*)
+ f.to_s
end
- def self.print(*args, **kwargs)
- f = new(*args, **kwargs)
+ def self.print(*, **)
+ f = new(*, **)
f.print
end
def initialize(objects, cols = nil, header: true, sort: nil, layout: nil, empty: '-')
@objects = objects
@@ -16,35 +16,33 @@
@sort = sort
@layout = layout
@empty = empty
if @layout.nil?
- if many?
- @layout = :columns
+ @layout = if many?
+ :columns
- else
- @layout = :rows
- end
+ else
+ :rows
+ end
end
if cols
@cols = parse_cols(cols)
- else
- if @objects.is_a?(::Array) # A list of items
- @cols ||= parse_cols(@objects.first.keys)
+ elsif @objects.is_a?(::Array)
+ @cols ||= parse_cols(@objects.first.keys) # A list of items
- elsif @objects.is_a?(::Hash) # Single item
- @cols ||= parse_cols(@objects.keys)
+ elsif @objects.is_a?(::Hash) # Single item
+ @cols ||= parse_cols(@objects.keys)
- else
- fail "unsupported type #{@objects.class}"
- end
+ else
+ raise "unsupported type #{@objects.class}"
end
end
- def format
+ def to_s
@out = ''
generate
@out
end
@@ -52,10 +50,11 @@
@out = nil
generate
end
protected
+
def parse_cols(cols)
ret = []
cols.each do |c|
base = {
@@ -63,63 +62,64 @@
}
if c.is_a?(::String) || c.is_a?(::Symbol)
base.update({
name: c,
- label: c.to_s.upcase,
+ label: c.to_s.upcase
})
ret << base
elsif c.is_a?(::Hash)
base.update(c)
ret << base
else
- fail "unsupported column type #{c.class}"
+ raise "unsupported column type #{c.class}"
end
end
ret
end
def generate
return if @cols.empty?
+
prepare
case @layout
when :columns
columns
when :rows
rows
else
- fail "unsupported layout '#{@layout}'"
+ raise "unsupported layout '#{@layout}'"
end
end
# Each object is printed on one line, it's parameters aligned into columns.
def columns
i = 0
formatters = @cols.map do |c|
ret = case c[:align].to_sym
- when :right
- "%#{col_width(i, c)}s"
+ when :right
+ "%#{col_width(i, c)}s"
- else
- "%-#{col_width(i, c)}s"
- end
+ else
+ "%-#{col_width(i, c)}s"
+ end
i += 1
ret
end.join(' ')
- line sprintf(formatters, * @cols.map { |c| c[:label] }) if @header
+ line format(formatters, * @cols.map { |c| c[:label] }) if @header
@str_objects.each do |o|
- line sprintf(formatters, *o)
+ line format(formatters, *o)
end
end
# Each object is printed on multiple lines, one parameter per line.
def rows
@@ -129,26 +129,28 @@
@cols.each_index do |i|
c = @cols[i]
if o[i].is_a?(::String) && o[i].index("\n")
lines = o[i].split("\n")
- v = ([lines.first] + lines[1..-1].map { |l| (' ' * (w+3)) + l }).join("\n")
+ v = ([lines.first] + lines[1..].map { |l| (' ' * (w + 3)) + l }).join("\n")
else
v = o[i]
end
- line sprintf("%#{w}s: %s", c[:label], v)
+ # rubocop:disable Lint/FormatParameterMismatch
+ line format("%#{w}s: %s", c[:label], v)
+ # rubocop:enable Lint/FormatParameterMismatch
end
line
end
end
def line(str = '')
if @out
- @out += str + "\n"
+ @out += "#{str}\n"
else
puts str
end
end
@@ -158,11 +160,11 @@
each_object do |o|
arr = []
@cols.each do |c|
- v = o[ c[:name] ]
+ v = o[c[:name]]
str = (c[:display] ? c[:display].call(v) : v)
str = @empty if !str || (str.is_a?(::String) && str.empty?)
arr << str
end
@@ -170,19 +172,20 @@
@str_objects << arr
end
if @sort
col_i = @cols.index { |c| c[:name] == @sort }
- fail "unknown column '#{@sort}'" unless col_i
+ raise "unknown column '#{@sort}'" unless col_i
@str_objects.sort! do |a, b|
a_i = a[col_i]
b_i = b[col_i]
next 0 if a_i == @empty && b_i == @empty
next -1 if a_i == @empty && b_i != @empty
next 1 if a_i != @empty && b_i == @empty
+
a_i <=> b_i
end
end
@str_objects
@@ -209,12 +212,12 @@
end
w + 1
end
- def each_object
+ def each_object(&)
if @objects.is_a?(::Array)
- @objects.each { |v| yield(v) }
+ @objects.each(&)
else
yield(@objects)
end
end